diff --git a/config/controller.go b/config/controller.go index d0c9f5e58..db1fe2f7a 100644 --- a/config/controller.go +++ b/config/controller.go @@ -28,6 +28,11 @@ func (c *controller) Startup(fn func() error) { c.parser.cfg.Startup = append(c.parser.cfg.Startup, fn) } +// Shutdown registers a function to execute when the server exits. +func (c *controller) Shutdown(fn func() error) { + c.parser.cfg.Shutdown = append(c.parser.cfg.Shutdown, fn) +} + // Root returns the server root file path. func (c *controller) Root() string { if c.parser.cfg.Root == "" { diff --git a/middleware/middleware.go b/middleware/middleware.go index 666322cc2..2aceda136 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -71,6 +71,9 @@ type ( // Startup registers a function to execute when the server starts. Startup(func() error) + // Shutdown registers a function to execute when the server exits. + Shutdown(func() error) + // Root returns the file path from which the server is serving. Root() string diff --git a/server/server.go b/server/server.go index d416e0ae5..9a1fe894e 100644 --- a/server/server.go +++ b/server/server.go @@ -78,7 +78,6 @@ func (s *Server) Serve() error { http2.ConfigureServer(server, nil) // TODO: This may not be necessary after HTTP/2 merged into std lib // Execute shutdown commands on exit - // TODO: Is graceful net/http shutdown necessary? go func() { interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt, os.Kill) // TODO: syscall.SIGQUIT? (Ctrl+\, Unix-only) @@ -103,7 +102,7 @@ func (s *Server) Serve() error { func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer func() { if rec := recover(); rec != nil { - log.Printf("[PANIC] '%s': %s", r.URL.String(), rec) + s.Log("[PANIC] '%s': %s", r.URL.String(), rec) } }() s.stack(w, r)