From fdcd0a059dc4251e934bb03f0531f281b195c393 Mon Sep 17 00:00:00 2001 From: Cedric Bail Date: Tue, 24 Oct 2023 14:30:42 -0600 Subject: [PATCH 1/2] Add a README file. --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e696f3a --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# graceful + +[![Run Tests](https://github.com/gin-contrib/graceful/actions/workflows/go.yml/badge.svg?branch=master)](https://github.com/gin-contrib/graceful/actions/workflows/go.yml) +[![codecov](https://codecov.io/gh/gin-contrib/graceful/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/graceful) +[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/graceful)](https://goreportcard.com/report/github.com/gin-contrib/graceful) +[![GoDoc](https://godoc.org/github.com/gin-contrib/graceful?status.svg)](https://godoc.org/github.com/gin-contrib/graceful) +[![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin) + +Gin wrapper to enable graceful termination when shutting down a process + +## Example + +```go +package main + +import ( + "context" + "net/http" + "os/signal" + "syscall" + + "github.com/gin-contrib/graceful" + "github.com/gin-gonic/gin" +) + +func main() { + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() + + router, err := graceful.Default() + if err != nil { + panic(err) + } + defer router.Close() + + router.GET("/", func(c *gin.Context) { + c.String(http.StatusOK, "Welcome Gin Server") + }) + + if err := router.RunWithContext(ctx); err != nil && err != context.Canceled { + panic(err) + } +} +``` + From 9927b424f0e0b79d6242bc9a03dd4e9643a1f566 Mon Sep 17 00:00:00 2001 From: Cedric Bail Date: Tue, 24 Oct 2023 14:30:51 -0600 Subject: [PATCH 2/2] Improve documentation on some variable and type. --- graceful.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/graceful.go b/graceful.go index 1bfa414..8a0d3d5 100644 --- a/graceful.go +++ b/graceful.go @@ -10,7 +10,7 @@ import ( "github.com/gin-gonic/gin" ) -// Graceful is a wrapper around a gin.Engine that provides graceful shutdown +// Graceful is a wrapper around a [gin.Engine] that provides graceful shutdown type Graceful struct { *gin.Engine @@ -23,7 +23,10 @@ type Graceful struct { cleanup []cleanup } +// ErrAlreadyStarted is returned when trying to start a router that has already been started var ErrAlreadyStarted = errors.New("already started router") + +// ErrNotStarted is returned when trying to stop a router that has not been started var ErrNotStarted = errors.New("router not started") type listenAndServe func() error