From f3af1bf3dd476733d0286eeea9f6dd5ebbdb4950 Mon Sep 17 00:00:00 2001 From: Stanislav Chzhen Date: Wed, 4 Dec 2024 18:33:35 +0300 Subject: [PATCH] home: imp code --- internal/home/controlinstall.go | 35 ++++++++++++++++----------------- internal/home/web.go | 14 ++++++------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/internal/home/controlinstall.go b/internal/home/controlinstall.go index 085003dc..9d7a96a0 100644 --- a/internal/home/controlinstall.go +++ b/internal/home/controlinstall.go @@ -20,7 +20,6 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/aghnet" "github.com/AdguardTeam/AdGuardHome/internal/version" "github.com/AdguardTeam/golibs/errors" - "github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/logutil/slogutil" "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. const shutdownTimeout = 5 * time.Second -// shutdownSrv shuts srv down and prints error messages to the log. -func shutdownSrv(ctx context.Context, srv *http.Server) { - defer log.OnPanic("") +// shutdownSrv shuts down srv and logs the error, if any. l must not be nil. +func shutdownSrv(ctx context.Context, l *slog.Logger, srv *http.Server) { + defer slogutil.RecoverAndLog(ctx, l) if srv == nil { return @@ -357,19 +356,19 @@ func shutdownSrv(ctx context.Context, srv *http.Server) { return } - const msgFmt = "shutting down http server %q: %s" - if errors.Is(err, context.Canceled) { - log.Debug(msgFmt, srv.Addr, err) - } else { - log.Error(msgFmt, srv.Addr, err) + lvl := slog.LevelDebug + if !errors.Is(err, context.Canceled) { + lvl = slog.LevelError } + + 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]. -func shutdownSrv3(srv *http3.Server) { - defer log.OnPanic("") +func shutdownSrv3(ctx context.Context, l *slog.Logger, srv *http3.Server) { + defer slogutil.RecoverAndLog(ctx, l) if srv == nil { return @@ -380,12 +379,12 @@ func shutdownSrv3(srv *http3.Server) { return } - const msgFmt = "shutting down http/3 server %q: %s" - if errors.Is(err, context.Canceled) { - log.Debug(msgFmt, srv.Addr, err) - } else { - log.Error(msgFmt, srv.Addr, err) + lvl := slog.LevelDebug + if !errors.Is(err, context.Canceled) { + lvl = slog.LevelError } + + 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. @@ -493,7 +492,7 @@ func (web *webAPI) handleInstallConfigure(w http.ResponseWriter, r *http.Request defer slogutil.RecoverAndLog(ctx, web.logger) defer cancel() - shutdownSrv(ctx, web.httpServer) + shutdownSrv(ctx, web.logger, web.httpServer) }(shutdownTimeout) } diff --git a/internal/home/web.go b/internal/home/web.go index 56032ed7..672b1672 100644 --- a/internal/home/web.go +++ b/internal/home/web.go @@ -112,8 +112,8 @@ type webAPI struct { httpsServer httpsServer } -// newWebAPI creates a new instance of the web UI and API server. baseLogger -// must not be nil. +// newWebAPI creates a new instance of the web UI and API server. conf must not +// be nil and must be valid // // TODO(a.garipov): Return a proper error. 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 { var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, shutdownTimeout) - shutdownSrv(ctx, web.httpsServer.server) - shutdownSrv3(web.httpsServer.server3) + shutdownSrv(ctx, web.logger, web.httpsServer.server) + shutdownSrv3(ctx, web.logger, web.httpsServer.server3) cancel() } @@ -263,9 +263,9 @@ func (web *webAPI) close(ctx context.Context) { ctx, cancel = context.WithTimeout(ctx, shutdownTimeout) defer cancel() - shutdownSrv(ctx, web.httpsServer.server) - shutdownSrv3(web.httpsServer.server3) - shutdownSrv(ctx, web.httpServer) + shutdownSrv(ctx, web.logger, web.httpsServer.server) + shutdownSrv3(ctx, web.logger, web.httpsServer.server3) + shutdownSrv(ctx, web.logger, web.httpServer) web.logger.InfoContext(ctx, "stopped http server") }