Add handling of SIGINT and SIGTERM

Handle exit signals so that deferred calls are executed. It's mainly
to unlink the socket before exiting.
This commit is contained in:
Krystian Chachuła 2024-07-01 19:51:12 +02:00 committed by Conrad Hoffmann
parent 42b36e3421
commit 7969df1a38

View file

@ -8,7 +8,9 @@ import (
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/caldav"
@ -189,6 +191,7 @@ func main() {
log.Info().Str("address", addr).Msg("starting server")
log.Debug().Msg("debug output enabled")
go func() {
if (cert != "") && (key != "") {
err = server.ServeTLS(ln, cert, key)
} else {
@ -197,4 +200,15 @@ func main() {
if err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("ListenAndServe() error")
}
}()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
for sig := range sigCh {
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
server.Shutdown(context.Background())
return
}
}
}