Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 1.21 KB

README.md

File metadata and controls

43 lines (33 loc) · 1.21 KB

graceful

Run Tests codecov Go Report Card GoDoc

Gin wrapper to enable graceful termination when shutting down a process

Example

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