From c9ec22d5f4e0e3564939c365427ce42c8cad63b2 Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 5 Nov 2020 11:56:40 +0800 Subject: [PATCH] add https listen and serve --- rest/internal/starter.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/rest/internal/starter.go b/rest/internal/starter.go index 1fcff7f3..735775e9 100644 --- a/rest/internal/starter.go +++ b/rest/internal/starter.go @@ -12,7 +12,8 @@ import ( func StartHttp(host string, port int, handler http.Handler) error { addr := fmt.Sprintf("%s:%d", host, port) server := buildHttpServer(addr, handler) - return StartServer(server) + gracefulOnShutdown(server) + return server.ListenAndServe() } func StartHttps(host string, port int, certFile, keyFile string, handler http.Handler) error { @@ -20,18 +21,12 @@ func StartHttps(host string, port int, certFile, keyFile string, handler http.Ha if server, err := buildHttpsServer(addr, handler, certFile, keyFile); err != nil { return err } else { - return StartServer(server) + gracefulOnShutdown(server) + // certFile and keyFile are set in buildHttpsServer + return server.ListenAndServeTLS("", "") } } -func StartServer(srv *http.Server) error { - proc.AddWrapUpListener(func() { - srv.Shutdown(context.Background()) - }) - - return srv.ListenAndServe() -} - func buildHttpServer(addr string, handler http.Handler) *http.Server { return &http.Server{Addr: addr, Handler: handler} } @@ -49,3 +44,9 @@ func buildHttpsServer(addr string, handler http.Handler, certFile, keyFile strin TLSConfig: &config, }, nil } + +func gracefulOnShutdown(srv *http.Server) { + proc.AddWrapUpListener(func() { + srv.Shutdown(context.Background()) + }) +}