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) + } +} +``` + 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