Skip to content

Commit

Permalink
Handle SIGTERM and Ctrl+C (SIGINT) gracefully (#144)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
agners authored Sep 7, 2023
1 parent 6e1343e commit a3d3a8c
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net"
"net/http"
"os"
"os/signal"
"syscall"
"text/template"
"time"

Expand Down Expand Up @@ -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
}
}
}

0 comments on commit a3d3a8c

Please sign in to comment.