Created
October 4, 2024 11:11
-
-
Save hmhuan/f5f26cd1142732c5e31aa80ebf9a9349 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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