diff --git a/main.go b/main.go index 07ab8da..f64fc2a 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "log" "net/http" "os" + "os/signal" + "syscall" "time" messages "git.tornberg.me/go-cart-actor/proto" @@ -193,6 +195,17 @@ func main() { mux.HandleFunc("GET /save", app.HandleSave) mux.Handle("/metrics", promhttp.Handler()) - http.ListenAndServe(":8080", mux) + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGTERM) + done := make(chan bool, 1) + go func() { + sig := <-sigs + fmt.Println("Shutting down due to signal:", sig) + app.Save() + done <- true + }() + + go http.ListenAndServe(":8080", mux) + <-done }