Implemented graceful shutdown

This commit is contained in:
konrad 2017-12-01 12:46:47 +01:00 committed by kolaente
parent a97216fbd1
commit 2644141953
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 22 additions and 1 deletions

23
main.go
View File

@ -5,6 +5,10 @@ import (
"git.mowie.cc/konrad/Library/routes"
"fmt"
"os"
"time"
"os/signal"
"context"
)
// UserLogin Object to recive user credentials in JSON format
@ -30,5 +34,22 @@ func main() {
// Start the webserver
e := routes.NewEcho()
routes.RegisterRoutes(e)
e.Start(models.Config.Interface)
// Start server
go func() {
if err := e.Start(models.Config.Interface); err != nil {
e.Logger.Info("shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 10 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
fmt.Println("Shutting down...")
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}