From a54361d249fafcfb57e1e478c50cbd7588e98aa7 Mon Sep 17 00:00:00 2001 From: Leufolium Date: Thu, 23 May 2024 23:59:14 +0800 Subject: [PATCH] by Robin at 20240523 --- app/mix/cmd_offline/main.go | 2 +- library/httpserver/httpserver.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/mix/cmd_offline/main.go b/app/mix/cmd_offline/main.go index e409a6a1..3a5ea5f6 100644 --- a/app/mix/cmd_offline/main.go +++ b/app/mix/cmd_offline/main.go @@ -114,7 +114,7 @@ func main() { Addr: fmt.Sprintf("%s:%d", GetIp(), cfg.OfflineServer.App.Port), Handler: router, } - httpserver.StartHttpServer(srv, cfg.OfflineServer) + httpserver.StartOfflineHttpServer(srv, cfg.OfflineServer) } func PrintAndExit(msg string) { diff --git a/library/httpserver/httpserver.go b/library/httpserver/httpserver.go index ee0871bd..a7c86255 100644 --- a/library/httpserver/httpserver.go +++ b/library/httpserver/httpserver.go @@ -40,3 +40,31 @@ func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig) { logger.Info("Server exited") } + +func StartOfflineHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig) { + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + logger.Fatal("listen: %v", err) + } + }() + + logger.Info("Server %s start at %s", cfg.App.AppName, srv.Addr) + + quit := make(chan os.Signal) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + <-quit + logger.Info("Shutdown Server ...") + + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + defer cancel() + if err := srv.Shutdown(ctx); err != nil { + log.Fatal("Server Shutdown:", err) + } + + select { + case <-ctx.Done(): + logger.Info("timeout of 1 seconds.") + } + + logger.Info("Server exited") +}