From 0d81ec2e596a915df640284c8f31fbbe534ff505 Mon Sep 17 00:00:00 2001 From: Sergey Kibish Date: Sat, 24 Feb 2018 19:35:44 +0200 Subject: [PATCH] Rewrite shutdown for authorizer --- cmd/authorizer/main.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/cmd/authorizer/main.go b/cmd/authorizer/main.go index c7f10eb..e55eb44 100644 --- a/cmd/authorizer/main.go +++ b/cmd/authorizer/main.go @@ -2,8 +2,10 @@ package main import ( "log" + "net/http" "os" "os/signal" + "syscall" "github.com/skibish/trashdiena/pkg/config" @@ -25,22 +27,24 @@ func main() { sc := slack.New(c.ClientID, c.ClientSecret, c.RedirectURL) sg := storage.New(fbase) - a := api.New(sc, sg) - go func() { - log.Fatal(a.Start(c.APIPort)) - }() - - // handle all the gracefull shutdowns - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, os.Interrupt) - select { - case <-sigs: + // shutdown gracefully + go func() { + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) + <-sigs log.Println("Performing shutdown...") - if a != nil { - a.Shutdown() + if err := a.Shutdown(); err != nil { + log.Printf("Failed to shutdown server: %v", err) } - log.Println("Exiting...") + }() + + log.Printf("Authorizer is ready to listen on port: %s", c.APIPort) + if err := a.Start(c.APIPort); err != http.ErrServerClosed { + log.Printf("Server failed: %v", err) + os.Exit(1) } + + log.Println("Exiting...") }