Skip to content

Instantly share code, notes, and snippets.

@hmhuan
Created October 4, 2024 11:11
Show Gist options
  • Save hmhuan/f5f26cd1142732c5e31aa80ebf9a9349 to your computer and use it in GitHub Desktop.
Save hmhuan/f5f26cd1142732c5e31aa80ebf9a9349 to your computer and use it in GitHub Desktop.
if configs.GetENV() == configs.ReleaseEnv {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
address := fmt.Sprintf("%s:%s", configs.GetHost(), configs.GetPort())
server := &http.Server{Addr: address, Handler: router}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Failed to listening and serve: %s\n", err)
}
}()
log.Printf("Starting server on %s\n", address)
<-ctx.Done()
// Restore default behavior on the termination signal and allow any further cleanup
stop()
log.Println("Shutting down gracefully...")
// Create a timeout context for the shutdown process
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Attempt to gracefully shut down the server
if err := server.Shutdown(shutdownCtx); err != nil {
log.Fatal("Server forced to shutdown:", err)
}
os.Exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment