add https listen and serve

master
kevin 4 years ago
parent afffc1048b
commit c9ec22d5f4

@ -12,7 +12,8 @@ import (
func StartHttp(host string, port int, handler http.Handler) error { func StartHttp(host string, port int, handler http.Handler) error {
addr := fmt.Sprintf("%s:%d", host, port) addr := fmt.Sprintf("%s:%d", host, port)
server := buildHttpServer(addr, handler) 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 { 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 { if server, err := buildHttpsServer(addr, handler, certFile, keyFile); err != nil {
return err return err
} else { } 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 { func buildHttpServer(addr string, handler http.Handler) *http.Server {
return &http.Server{Addr: addr, Handler: handler} return &http.Server{Addr: addr, Handler: handler}
} }
@ -49,3 +44,9 @@ func buildHttpsServer(addr string, handler http.Handler, certFile, keyFile strin
TLSConfig: &config, TLSConfig: &config,
}, nil }, nil
} }
func gracefulOnShutdown(srv *http.Server) {
proc.AddWrapUpListener(func() {
srv.Shutdown(context.Background())
})
}

Loading…
Cancel
Save