53 lines
961 B
Go
53 lines
961 B
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
)
|
|
|
|
// 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)
|
|
|
|
go func() {
|
|
log.Print("listening on http://" + srv.Addr)
|
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}()
|
|
|
|
<-stop
|
|
|
|
log.Print("Shutting down...")
|
|
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
srv.Shutdown(ctx)
|
|
|
|
log.Print("Shut Down")
|
|
}
|