Pull request: 2343 http server

Merge in DNS/adguard-home from 2343-http-server to master

Closes .

Squashed commit of the following:

commit f4ebfc129484fc3489409069b3580eb70d71cc74
Merge: b13ec7002 36c7735b8
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Nov 25 15:37:27 2020 +0300

    Merge branch 'master' into 2343-http-server

commit b13ec70024f24f6b68b13a1ec6f27c89535feaf8
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Nov 25 15:31:36 2020 +0300

    all: record changes

commit ce44aac9d43e32db3f68746dec7a4f21b0a9dea4
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Nov 25 14:00:45 2020 +0300

    home: set http servers timeouts

commit 7f3e7385d1df39b39713b8ec443da5d9374d0bc8
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Nov 24 19:58:56 2020 +0300

    home: replace default ServeMux with custom one.
This commit is contained in:
Eugene Burkov 2020-11-25 15:50:59 +03:00
parent 36c7735b85
commit b4a35fa887
8 changed files with 62 additions and 34 deletions
internal/home

View file

@ -8,6 +8,7 @@ import (
"net/http"
"strconv"
"sync"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/golibs/log"
@ -15,11 +16,37 @@ import (
"github.com/gobuffalo/packr"
)
const (
// ReadTimeout is the maximum duration for reading the entire request,
// including the body.
ReadTimeout = 10 * time.Second
// ReadHeaderTimeout is the amount of time allowed to read request
// headers.
ReadHeaderTimeout = 10 * time.Second
// WriteTimeout is the maximum duration before timing out writes of the
// response.
WriteTimeout = 10 * time.Second
)
type WebConfig struct {
firstRun bool
BindHost string
BindPort int
PortHTTPS int
// ReadTimeout is an option to pass to http.Server for setting an
// appropriate field.
ReadTimeout time.Duration
// ReadHeaderTimeout is an option to pass to http.Server for setting an
// appropriate field.
ReadHeaderTimeout time.Duration
// WriteTimeout is an option to pass to http.Server for setting an
// appropriate field.
WriteTimeout time.Duration
}
// HTTPSServer - HTTPS Server
@ -66,12 +93,12 @@ func CreateWeb(conf *WebConfig) *Web {
box := packr.NewBox("../../build/static")
// if not configured, redirect / to /install.html, otherwise redirect /install.html to /
http.Handle("/", postInstallHandler(optionalAuthHandler(gziphandler.GzipHandler(http.FileServer(box)))))
Context.mux.Handle("/", postInstallHandler(optionalAuthHandler(gziphandler.GzipHandler(http.FileServer(box)))))
// add handlers for /install paths, we only need them when we're not configured yet
if conf.firstRun {
log.Info("This is the first launch of AdGuard Home, redirecting everything to /install.html ")
http.Handle("/install.html", preInstallHandler(http.FileServer(box)))
Context.mux.Handle("/install.html", preInstallHandler(http.FileServer(box)))
w.registerInstallHandlers()
} else {
registerControlHandlers()
@ -139,9 +166,12 @@ func (web *Web) Start() {
// we need to have new instance, because after Shutdown() the Server is not usable
address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.BindPort))
web.httpServer = &http.Server{
ErrorLog: web.errLogger,
Addr: address,
Handler: withMiddlewares(http.DefaultServeMux, filterPProf, limitRequestBody),
ErrorLog: web.errLogger,
Addr: address,
Handler: withMiddlewares(Context.mux, limitRequestBody),
ReadTimeout: web.conf.ReadTimeout,
ReadHeaderTimeout: web.conf.ReadHeaderTimeout,
WriteTimeout: web.conf.WriteTimeout,
}
err := web.httpServer.ListenAndServe()
if err != http.ErrServerClosed {
@ -198,6 +228,10 @@ func (web *Web) tlsServerLoop() {
RootCAs: Context.tlsRoots,
CipherSuites: Context.tlsCiphers,
},
Handler: Context.mux,
ReadTimeout: web.conf.ReadTimeout,
ReadHeaderTimeout: web.conf.ReadHeaderTimeout,
WriteTimeout: web.conf.WriteTimeout,
}
printHTTPAddresses("https")