From a3d3a8c4dcc0ff7c64715bccb73fbb1d39805d30 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Thu, 7 Sep 2023 11:04:36 +0200 Subject: [PATCH] Handle SIGTERM and Ctrl+C (SIGINT) gracefully (#144) This makes sure that the landing page container exits with exit code 0. This avoids Watchdog error messages in the Supervisor logs. While at it, also handle and log errors in case http.ListenAndServe fails. --- main.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 1f6c4f8..e64239d 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "net" "net/http" "os" + "os/signal" + "syscall" "text/template" "time" @@ -60,6 +62,23 @@ func main() { http.Handle("/static/Roboto-Regular.woff2", staticFiles) - log.Print("Start webserver on http://0.0.0.0:80") - http.ListenAndServe(":80", nil) + // Run webserver + go func() { + log.Print("Start webserver on http://0.0.0.0:80") + if err := http.ListenAndServe(":80", nil); err != nil { + log.Fatal(err) + } + }() + + signalChannel := make(chan os.Signal, 2) + signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM) + for { + sig := <-signalChannel + switch sig { + case os.Interrupt: + return + case syscall.SIGTERM: + return + } + } }