import (
"net"
"github.com/KarpelesLab/shutdown"
)
func main() {
shutdown.SetupSignals()
// do the things you want to do
go launchHttp()
shutdown.Wait()
}
func launchHttp() {
l, err := net.Listen("tcp", ":80")
if err != nil {
shutdown.Fatalf("failed to listen for the http server: %w", err)
return
}
// cleanup of opened listen socket
shutdown.Defer(func() {
l.Close()
})
// ...
}
Go runs function(s) f in background, and if any of those functions
return an error, it'll be handled as a fatal error and cause shutdown
to trigger as if Fatalf was called
SetupSignals will listen for interruptions (Ctrl-C or kill), and trigger a shutdown if
a signal is received. If a second signal is sent (shutdown is taking too long), this will
call os.Exit(1)