Replies: 2 comments
-
Probably one option is to spin things up from the chi router, but curious if there’s a way from the web.NewService usage? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Graceful shutdown happens at the I've added graceful shutdown to one of examples: https://github.com/swaggest/rest/blob/master/_examples/advanced-generic-openapi31/main.go. //go:build go1.18
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
var srv http.Server
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
log.Println("Shutting down...")
// We received an interrupt signal, shut down with up to 10 seconds of waiting for current requests to finish.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
// Error from closing listeners, or context timeout:
log.Printf("HTTP server Shutdown: %v", err)
}
log.Println("Shutdown complete")
close(idleConnsClosed)
}()
srv.Handler = NewRouter()
srv.Addr = "localhost:8012"
log.Println("http://localhost:8012/docs")
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
// Error starting or closing listener:
log.Fatalf("HTTP server ListenAndServe: %v", err)
}
<-idleConnsClosed
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, what’s the best way to gracefully shutdown the server on, say, a sigint? I know how to gracefully shutdown chi, is it easy to get access to the underlying chi router?
Beta Was this translation helpful? Give feedback.
All reactions