Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/add readme #7

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
}
}
```

5 changes: 4 additions & 1 deletion graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Loading