home: imp code

This commit is contained in:
Stanislav Chzhen 2024-12-03 19:01:55 +03:00
parent 5368d8de50
commit 035477513f
3 changed files with 37 additions and 28 deletions

View File

@ -243,27 +243,18 @@ func checkDNSStubListener(ctx context.Context, l *slog.Logger) (ok bool) {
return false return false
} }
var cmd *exec.Cmd cmd := exec.Command("systemctl", "is-enabled", "systemd-resolved")
var err error l.DebugContext(ctx, "executing", "cmd", cmd.Path, "args", cmd.Args)
_, err := cmd.Output()
defer func() { if err != nil || cmd.ProcessState.ExitCode() != 0 {
if ok { l.InfoContext(
return
}
l.ErrorContext(
ctx, ctx,
"execution failed", "execution failed",
"cmd", cmd.Path, "cmd", cmd.Path,
"code", cmd.ProcessState.ExitCode(), "code", cmd.ProcessState.ExitCode(),
slogutil.KeyError, err, slogutil.KeyError, err,
) )
}()
cmd = exec.Command("systemctl", "is-enabled", "systemd-resolved")
l.DebugContext(ctx, "executing", "cmd", cmd.Path, "args", cmd.Args)
_, err = cmd.Output()
if err != nil || cmd.ProcessState.ExitCode() != 0 {
return false return false
} }
@ -271,6 +262,14 @@ func checkDNSStubListener(ctx context.Context, l *slog.Logger) (ok bool) {
l.DebugContext(ctx, "executing", "cmd", cmd.Path, "args", cmd.Args) l.DebugContext(ctx, "executing", "cmd", cmd.Path, "args", cmd.Args)
_, err = cmd.Output() _, err = cmd.Output()
if err != nil || cmd.ProcessState.ExitCode() != 0 { if err != nil || cmd.ProcessState.ExitCode() != 0 {
l.InfoContext(
ctx,
"execution failed",
"cmd", cmd.Path,
"code", cmd.ProcessState.ExitCode(),
slogutil.KeyError, err,
)
return false return false
} }

View File

@ -531,9 +531,11 @@ func initWeb(
baseLogger *slog.Logger, baseLogger *slog.Logger,
customURL bool, customURL bool,
) (web *webAPI, err error) { ) (web *webAPI, err error) {
logger := baseLogger.With(slogutil.KeyPrefix, "webapi")
var clientFS fs.FS var clientFS fs.FS
if opts.localFrontend { if opts.localFrontend {
log.Info("warning: using local frontend files") logger.WarnContext(ctx, "using local frontend files")
clientFS = os.DirFS("build/static") clientFS = os.DirFS("build/static")
} else { } else {
@ -547,6 +549,8 @@ func initWeb(
webConf := &webConfig{ webConf := &webConfig{
updater: upd, updater: upd,
logger: logger,
baseLogger: baseLogger,
clientFS: clientFS, clientFS: clientFS,
@ -562,7 +566,7 @@ func initWeb(
serveHTTP3: config.DNS.ServeHTTP3, serveHTTP3: config.DNS.ServeHTTP3,
} }
web = newWebAPI(ctx, webConf, baseLogger) web = newWebAPI(ctx, webConf)
if web == nil { if web == nil {
return nil, errors.Error("can not initialize web") return nil, errors.Error("can not initialize web")
} }

View File

@ -41,6 +41,13 @@ const (
type webConfig struct { type webConfig struct {
updater *updater.Updater updater *updater.Updater
// logger is a slog logger used in webAPI. It must not be nil.
logger *slog.Logger
// baseLogger is used to create loggers for other entities. It must not be
// nil.
baseLogger *slog.Logger
clientFS fs.FS clientFS fs.FS
// BindAddr is the binding address with port for plain HTTP web interface. // BindAddr is the binding address with port for plain HTTP web interface.
@ -109,14 +116,13 @@ type webAPI struct {
// must not be nil. // must not be nil.
// //
// TODO(a.garipov): Return a proper error. // TODO(a.garipov): Return a proper error.
func newWebAPI(ctx context.Context, conf *webConfig, baseLogger *slog.Logger) (w *webAPI) { func newWebAPI(ctx context.Context, conf *webConfig) (w *webAPI) {
logger := baseLogger.With(slogutil.KeyPrefix, "webapi") conf.logger.InfoContext(ctx, "initializing")
logger.InfoContext(ctx, "initializing")
w = &webAPI{ w = &webAPI{
conf: conf, conf: conf,
logger: logger, logger: conf.logger,
baseLogger: baseLogger, baseLogger: conf.baseLogger,
} }
clientFS := http.FileServer(http.FS(conf.clientFS)) clientFS := http.FileServer(http.FS(conf.clientFS))
@ -126,7 +132,7 @@ func newWebAPI(ctx context.Context, conf *webConfig, baseLogger *slog.Logger) (w
// add handlers for /install paths, we only need them when we're not configured yet // add handlers for /install paths, we only need them when we're not configured yet
if conf.firstRun { if conf.firstRun {
logger.InfoContext( conf.logger.InfoContext(
ctx, ctx,
"This is the first launch of AdGuard Home, redirecting everything to /install.html", "This is the first launch of AdGuard Home, redirecting everything to /install.html",
) )
@ -204,10 +210,10 @@ const loggerKeyServer = "server"
// start - start serving HTTP requests // start - start serving HTTP requests
func (web *webAPI) start(ctx context.Context) { func (web *webAPI) start(ctx context.Context) {
web.logger.InfoContext(ctx, "AdGuard Home is available at the following addresses:")
defer slogutil.RecoverAndLog(ctx, web.logger) defer slogutil.RecoverAndLog(ctx, web.logger)
web.logger.InfoContext(ctx, "AdGuard Home is available at the following addresses:")
// for https, we have a separate goroutine loop // for https, we have a separate goroutine loop
go web.tlsServerLoop(ctx) go web.tlsServerLoop(ctx)
@ -358,10 +364,10 @@ func startPprof(baseLogger *slog.Logger, port uint16) {
mux := http.NewServeMux() mux := http.NewServeMux()
httputil.RoutePprof(mux) httputil.RoutePprof(mux)
go func() {
ctx := context.Background() ctx := context.Background()
logger := baseLogger.With(slogutil.KeyPrefix, "pprof") logger := baseLogger.With(slogutil.KeyPrefix, "pprof")
go func() {
defer slogutil.RecoverAndLog(ctx, logger) defer slogutil.RecoverAndLog(ctx, logger)
logger.InfoContext(ctx, "listening", "addr", addr) logger.InfoContext(ctx, "listening", "addr", addr)