Skip to content

Commit

Permalink
feat: Implement graceful shutdown
Browse files Browse the repository at this point in the history
Close #20
  • Loading branch information
howjmay committed Sep 22, 2020
1 parent f96bfb1 commit ee3afd4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"
"time"

"github.com/DropKit/DropKit-Adapter/logger"
routes "github.com/DropKit/DropKit-Adapter/router"
"github.com/DropKit/DropKit-Adapter/services"
"github.com/ethereum/go-ethereum/log"
"github.com/spf13/viper"
)

Expand All @@ -25,6 +32,19 @@ func init() {
}

func main() {
router := routes.SetupRouter()
router.Run(":" + viper.GetString(`DROPKIT.PORT`))
// router := routes.SetupRouter()
_, srv := routes.SetupRouter()
// router.Run(":" + viper.GetString(`DROPKIT.PORT`))

shutdown := make(chan os.Signal)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
<-shutdown
log.Info("Shutdown Server")

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Error("Forced to shutdown server")
}

}
18 changes: 16 additions & 2 deletions router/routers.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
package routes

import (
"log"
"net/http"

"github.com/DropKit/DropKit-Adapter/controller/db"
"github.com/DropKit/DropKit-Adapter/controller/health"
"github.com/DropKit/DropKit-Adapter/controller/payment"
"github.com/DropKit/DropKit-Adapter/controller/permission"
"github.com/DropKit/DropKit-Adapter/controller/role"
"github.com/DropKit/DropKit-Adapter/controller/user"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)

// SetupRouter Create a new router object
func SetupRouter() *gin.Engine {
func SetupRouter() (*gin.Engine, *http.Server) {
r := gin.Default()
srv := &http.Server{
Addr: viper.GetString(`DROPKIT.PORT`),
Handler: r,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()

healthRoute := r.Group("/health")
{
Expand Down Expand Up @@ -68,5 +81,6 @@ func SetupRouter() *gin.Engine {
{
roleRoute.POST("/create", role.CreateColumnRole)
}
return r

return r, srv
}

0 comments on commit ee3afd4

Please sign in to comment.