// listen to OS signals and gracefully shutdown HTTP server stopped := make(chan struct{}) go func() { sigint := make(chan os.Signal, 1) signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) <-sigint ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := srv.Shutdown(ctx); err != nil { log.Printf("HTTP Server Shutdown Error: %v", err) } close(stopped) }() log.Printf("Starting HTTP server on %s", cfg.HTTPAddr) // start HTTP server if err := srv.ListenAndServe(); err != http.ErrServerClosed { log.Fatalf("HTTP server ListenAndServe Error: %v", err) } <-stopped