add graceful shutdown

pull/6/head
Namekuji 2022-12-10 08:16:34 -05:00
rodzic 3b297c3e01
commit 9ee523465b
1 zmienionych plików z 19 dodań i 1 usunięć

Wyświetl plik

@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"os"
"os/signal"
"time"
"github.com/go-playground/validator/v10"
@ -137,7 +138,24 @@ func main() {
e.Static("/assets", "audon-fe/dist/assets")
e.File("/*", "audon-fe/dist/index.html")
e.Logger.Debug(e.Start(":8100"))
// use anonymous func to support graceful shutdown
go func() {
if err := e.Start(":1323"); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal("shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
e.Logger.Print("Attempting graceful shutdown")
defer shutdownCancel()
if err := e.Shutdown(shutdownCtx); err != nil {
e.Logger.Fatal(err)
}
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {