-
Beta Was this translation helpful? Give feedback.
Answered by
aldas
Jun 10, 2026
Replies: 1 comment
-
|
Please see https://echo.labstack.com/docs/cookbook/graceful-shutdown func main() {
e := echo.New()
e.GET("/", func(c *echo.Context) error {
time.Sleep(5 * time.Second)
return c.JSON(http.StatusOK, "OK")
})
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
sc := echo.StartConfig{
Address: ":1323",
GracefulTimeout: 5 * time.Second,
}
if err := sc.Start(ctx, e); err != nil {
e.Logger.Error("failed to start server", "error", err)
}
}or func main() {
// Setup
e := echo.New()
e.GET("/", func(c *echo.Context) error {
time.Sleep(5 * time.Second)
return c.JSON(http.StatusOK, "OK")
})
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
s := http.Server{Addr: ":1323", Handler: e}
// Start server
go func() {
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
e.Logger.Error("failed to start server", "error", err)
}
}()
// Wait for interrupt signal to gracefully shut down the server with a timeout of 10 seconds.
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
e.Logger.Error("failed to stop server", "error", err)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
essmoncif
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Please see https://echo.labstack.com/docs/cookbook/graceful-shutdown
or