From 417cd8763f6943e61f323dd38375cefab9e1ec29 Mon Sep 17 00:00:00 2001 From: Gardient Date: Sun, 11 Nov 2018 00:01:20 +0200 Subject: [PATCH] more graceful shutdown options --- httpserver.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/httpserver.go b/httpserver.go index fe6147b..4b4daeb 100644 --- a/httpserver.go +++ b/httpserver.go @@ -9,18 +9,32 @@ import ( "time" ) -// StartHTTPServer cct -func StartHTTPServer(address string, handler http.HandlerFunc) { +// StartHTTPServerHandler cct +func StartHTTPServerHandler(address string, handler http.Handler) { + mux := http.NewServeMux() + mux.Handle("/", handler) + srv := &http.Server{Addr: address, Handler: mux} + + startServer(srv) +} + +// StartHTTPServerHandlerFunc cct +func StartHTTPServerHandlerFunc(address string, handler http.HandlerFunc) { + mux := http.NewServeMux() + mux.HandleFunc("/", handler) + srv := &http.Server{Addr: address} + http.HandleFunc("/", handler) + + startServer(srv) +} + +func startServer(srv *http.Server) { stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt) - srv := &http.Server{Addr: address} - - http.HandleFunc("/", handler) - go func() { - log.Print("listening on http://" + address) + log.Print("listening on http://" + srv.Addr) if err := srv.ListenAndServe(); err != nil { log.Fatal(err)