home: imp code

This commit is contained in:
Stanislav Chzhen 2024-12-04 18:33:35 +03:00
parent 035477513f
commit f3af1bf3dd
2 changed files with 24 additions and 25 deletions

View file

@ -20,7 +20,6 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghnet" "github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/version" "github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil" "github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/quic-go/quic-go/http3" "github.com/quic-go/quic-go/http3"
) )
@ -344,9 +343,9 @@ func copyInstallSettings(dst, src *configuration) {
// shutdownTimeout is the timeout for shutting HTTP server down operation. // shutdownTimeout is the timeout for shutting HTTP server down operation.
const shutdownTimeout = 5 * time.Second const shutdownTimeout = 5 * time.Second
// shutdownSrv shuts srv down and prints error messages to the log. // shutdownSrv shuts down srv and logs the error, if any. l must not be nil.
func shutdownSrv(ctx context.Context, srv *http.Server) { func shutdownSrv(ctx context.Context, l *slog.Logger, srv *http.Server) {
defer log.OnPanic("") defer slogutil.RecoverAndLog(ctx, l)
if srv == nil { if srv == nil {
return return
@ -357,19 +356,19 @@ func shutdownSrv(ctx context.Context, srv *http.Server) {
return return
} }
const msgFmt = "shutting down http server %q: %s" lvl := slog.LevelDebug
if errors.Is(err, context.Canceled) { if !errors.Is(err, context.Canceled) {
log.Debug(msgFmt, srv.Addr, err) lvl = slog.LevelError
} else {
log.Error(msgFmt, srv.Addr, err)
} }
l.Log(ctx, lvl, "shutting down http server", "addr", srv.Addr, slogutil.KeyError, err)
} }
// shutdownSrv3 shuts srv down and prints error messages to the log. // shutdownSrv3 shuts down srv and logs the error, if any. l must not be nil.
// //
// TODO(a.garipov): Think of a good way to merge with [shutdownSrv]. // TODO(a.garipov): Think of a good way to merge with [shutdownSrv].
func shutdownSrv3(srv *http3.Server) { func shutdownSrv3(ctx context.Context, l *slog.Logger, srv *http3.Server) {
defer log.OnPanic("") defer slogutil.RecoverAndLog(ctx, l)
if srv == nil { if srv == nil {
return return
@ -380,12 +379,12 @@ func shutdownSrv3(srv *http3.Server) {
return return
} }
const msgFmt = "shutting down http/3 server %q: %s" lvl := slog.LevelDebug
if errors.Is(err, context.Canceled) { if !errors.Is(err, context.Canceled) {
log.Debug(msgFmt, srv.Addr, err) lvl = slog.LevelError
} else {
log.Error(msgFmt, srv.Addr, err)
} }
l.Log(ctx, lvl, "shutting down http/3 server", "addr", srv.Addr, slogutil.KeyError, err)
} }
// PasswordMinRunes is the minimum length of user's password in runes. // PasswordMinRunes is the minimum length of user's password in runes.
@ -493,7 +492,7 @@ func (web *webAPI) handleInstallConfigure(w http.ResponseWriter, r *http.Request
defer slogutil.RecoverAndLog(ctx, web.logger) defer slogutil.RecoverAndLog(ctx, web.logger)
defer cancel() defer cancel()
shutdownSrv(ctx, web.httpServer) shutdownSrv(ctx, web.logger, web.httpServer)
}(shutdownTimeout) }(shutdownTimeout)
} }

View file

@ -112,8 +112,8 @@ type webAPI struct {
httpsServer httpsServer httpsServer httpsServer
} }
// newWebAPI creates a new instance of the web UI and API server. baseLogger // newWebAPI creates a new instance of the web UI and API server. conf must not
// must not be nil. // be nil and must be valid
// //
// TODO(a.garipov): Return a proper error. // TODO(a.garipov): Return a proper error.
func newWebAPI(ctx context.Context, conf *webConfig) (w *webAPI) { func newWebAPI(ctx context.Context, conf *webConfig) (w *webAPI) {
@ -193,8 +193,8 @@ func (web *webAPI) tlsConfigChanged(ctx context.Context, tlsConf tlsConfigSettin
if web.httpsServer.server != nil { if web.httpsServer.server != nil {
var cancel context.CancelFunc var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, shutdownTimeout) ctx, cancel = context.WithTimeout(ctx, shutdownTimeout)
shutdownSrv(ctx, web.httpsServer.server) shutdownSrv(ctx, web.logger, web.httpsServer.server)
shutdownSrv3(web.httpsServer.server3) shutdownSrv3(ctx, web.logger, web.httpsServer.server3)
cancel() cancel()
} }
@ -263,9 +263,9 @@ func (web *webAPI) close(ctx context.Context) {
ctx, cancel = context.WithTimeout(ctx, shutdownTimeout) ctx, cancel = context.WithTimeout(ctx, shutdownTimeout)
defer cancel() defer cancel()
shutdownSrv(ctx, web.httpsServer.server) shutdownSrv(ctx, web.logger, web.httpsServer.server)
shutdownSrv3(web.httpsServer.server3) shutdownSrv3(ctx, web.logger, web.httpsServer.server3)
shutdownSrv(ctx, web.httpServer) shutdownSrv(ctx, web.logger, web.httpServer)
web.logger.InfoContext(ctx, "stopped http server") web.logger.InfoContext(ctx, "stopped http server")
} }