package main import ( "context" "log" "os" "os/signal" "syscall" "time" ) func main() { s := NewServer(8080) serverCtx, serverStopCtx := context.WithCancel(context.Background()) sig := make(chan os.Signal, 1) signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) go func() { <-sig shutdownCtx, _ := context.WithTimeout(serverCtx, 30*time.Second) go func() { <-shutdownCtx.Done() if shutdownCtx.Err() == context.DeadlineExceeded { log.Println("Shutdown timeout, force exit") } }() err := s.Shutdown(shutdownCtx) if err != nil { log.Fatal(err) } serverStopCtx() }() err := s.Run() if err != nil { log.Fatal(err) } <-serverCtx.Done() }