Skip to content

Commit

Permalink
docs: add readme (#7)
Browse files Browse the repository at this point in the history
* Add a README file.

* Improve documentation on some variable and type.
  • Loading branch information
cedric-appdirect authored Nov 25, 2023
1 parent e56ed94 commit da57448
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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

0 comments on commit da57448

Please sign in to comment.