Last active
August 11, 2025 13:40
-
-
Save vyachin/a5ca79030a1a28722183ba2d3340feab to your computer and use it in GitHub Desktop.
golang web server with graceful shutdown and recovery after panic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "log" | |
| "net/http" | |
| "os/signal" | |
| "runtime/debug" | |
| "syscall" | |
| "golang.org/x/sync/errgroup" | |
| ) | |
| func main() { | |
| ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | |
| defer stop() | |
| mux := http.NewServeMux() | |
| mux.HandleFunc("POST /", func(w http.ResponseWriter, r *http.Request) { | |
| _, err := w.Write([]byte("Hello World")) | |
| if err != nil { | |
| panic(err) | |
| } | |
| }) | |
| server := &http.Server{ | |
| Addr: ":8080", | |
| Handler: recoverPanic(mux), | |
| } | |
| errGroup, errCtx := errgroup.WithContext(ctx) | |
| errGroup.Go(func() error { | |
| log.Println("Server starting on :8080") | |
| return server.ListenAndServe() | |
| }) | |
| errGroup.Go(func() error { | |
| <-errCtx.Done() | |
| return server.Shutdown(context.Background()) | |
| }) | |
| err := errGroup.Wait() | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| } | |
| func recoverPanic(next http.Handler) http.Handler { | |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| defer func() { | |
| if err := recover(); err != nil { | |
| // Log the panic and stack trace | |
| log.Printf("Panic recovered: %v\nStack trace:\n%s", err, debug.Stack()) | |
| // Send an appropriate HTTP error response | |
| http.Error(w, "Internal Server Error", http.StatusInternalServerError) | |
| } | |
| }() | |
| next.ServeHTTP(w, r) | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment