web.go (AdGuardHome-0.104.1) | : | web.go (AdGuardHome-0.104.3) | ||
---|---|---|---|---|
package home | package home | |||
import ( | import ( | |||
"context" | "context" | |||
"crypto/tls" | "crypto/tls" | |||
golog "log" | golog "log" | |||
"net" | "net" | |||
"net/http" | "net/http" | |||
"strconv" | "strconv" | |||
"strings" | ||||
"sync" | "sync" | |||
"github.com/AdguardTeam/AdGuardHome/internal/util" | "github.com/AdguardTeam/AdGuardHome/internal/util" | |||
"github.com/AdguardTeam/golibs/log" | "github.com/AdguardTeam/golibs/log" | |||
"github.com/NYTimes/gziphandler" | "github.com/NYTimes/gziphandler" | |||
"github.com/gobuffalo/packr" | "github.com/gobuffalo/packr" | |||
) | ) | |||
type WebConfig struct { | type WebConfig struct { | |||
firstRun bool | firstRun bool | |||
skipping to change at line 144 | skipping to change at line 145 | |||
// this loop is used as an ability to change listening host and/or port | // this loop is used as an ability to change listening host and/or port | |||
for !web.httpsServer.shutdown { | for !web.httpsServer.shutdown { | |||
printHTTPAddresses("http") | printHTTPAddresses("http") | |||
// we need to have new instance, because after Shutdown() the Ser ver is not usable | // we need to have new instance, because after Shutdown() the Ser ver is not usable | |||
address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.c onf.BindPort)) | address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.c onf.BindPort)) | |||
web.httpServer = &http.Server{ | web.httpServer = &http.Server{ | |||
ErrorLog: web.errLogger, | ErrorLog: web.errLogger, | |||
Addr: address, | Addr: address, | |||
Handler: filterPPROF(http.DefaultServeMux), | ||||
} | } | |||
err := web.httpServer.ListenAndServe() | err := web.httpServer.ListenAndServe() | |||
if err != http.ErrServerClosed { | if err != http.ErrServerClosed { | |||
cleanupAlways() | cleanupAlways() | |||
log.Fatal(err) | log.Fatal(err) | |||
} | } | |||
// We use ErrServerClosed as a sign that we need to rebind on new address, so go back to the start of the loop | // We use ErrServerClosed as a sign that we need to rebind on new address, so go back to the start of the loop | |||
} | } | |||
} | } | |||
// TODO(a.garipov): We currently have to use this, because everything registers | ||||
// its HTTP handlers in http.DefaultServeMux. In the future, refactor our HTTP | ||||
// API initialization process and stop using the gosh darn http.DefaultServeMux | ||||
// for anything at all. Gosh darn global variables. | ||||
func filterPPROF(h http.Handler) (filtered http.Handler) { | ||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
if strings.HasPrefix(r.URL.Path, "/debug/pprof") { | ||||
http.NotFound(w, r) | ||||
return | ||||
} | ||||
h.ServeHTTP(w, r) | ||||
}) | ||||
} | ||||
// Close - stop HTTP server, possibly waiting for all active connections to be c losed | // Close - stop HTTP server, possibly waiting for all active connections to be c losed | |||
func (web *Web) Close() { | func (web *Web) Close() { | |||
log.Info("Stopping HTTP server...") | log.Info("Stopping HTTP server...") | |||
web.httpsServer.cond.L.Lock() | web.httpsServer.cond.L.Lock() | |||
web.httpsServer.shutdown = true | web.httpsServer.shutdown = true | |||
web.httpsServer.cond.L.Unlock() | web.httpsServer.cond.L.Unlock() | |||
if web.httpsServer.server != nil { | if web.httpsServer.server != nil { | |||
_ = web.httpsServer.server.Shutdown(context.TODO()) | _ = web.httpsServer.server.Shutdown(context.TODO()) | |||
} | } | |||
if web.httpServer != nil { | if web.httpServer != nil { | |||
End of changes. 3 change blocks. | ||||
0 lines changed or deleted | 18 lines changed or added |