add http gracefully shutdown
This commit is contained in:
parent
930e2432bc
commit
f2ef2956cc
28
main.go
28
main.go
|
@ -13,10 +13,12 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -565,11 +567,31 @@ func main() {
|
||||||
|
|
||||||
http.Handle(*metricsPath, promhttp.HandlerFor(ovpnAdmin.promRegistry, promhttp.HandlerOpts{}))
|
http.Handle(*metricsPath, promhttp.HandlerFor(ovpnAdmin.promRegistry, promhttp.HandlerOpts{}))
|
||||||
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(w, "pong")
|
fmt.Fprint(w, "pong")
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Printf("Bind: http://%s:%s\n", *listenHost, *listenPort)
|
done := make(chan os.Signal, 1)
|
||||||
log.Fatal(http.ListenAndServe(*listenHost+":"+*listenPort, nil))
|
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
bind := net.JoinHostPort(*listenHost, *listenPort)
|
||||||
|
srv := &http.Server{Addr: bind, Handler: nil}
|
||||||
|
go func() {
|
||||||
|
if err := srv.ListenAndServe(); err != nil {
|
||||||
|
log.Fatalf("listen: %s\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
log.Printf("http server started on http://%s\n", bind)
|
||||||
|
|
||||||
|
<-done
|
||||||
|
log.Print("http server stopped...")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer func() {
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
if err := srv.Shutdown(ctx); err != nil {
|
||||||
|
log.Fatalf("Server Shutdown Failed:%+v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CacheControlWrapper(h http.Handler) http.Handler {
|
func CacheControlWrapper(h http.Handler) http.Handler {
|
||||||
|
|
Loading…
Reference in New Issue