Pull request 2314: AGDNS-2374-slog-home-webapi

Squashed commit of the following:

commit 7c457d92b5
Merge: bcd3d29df 11dfc7a3e
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Dec 5 18:24:08 2024 +0300

    Merge branch 'master' into AGDNS-2374-slog-home-webapi

commit bcd3d29dfd
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Dec 5 18:24:01 2024 +0300

    all: imp code

commit f3af1bf3dd
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Dec 4 18:33:35 2024 +0300

    home: imp code

commit 035477513f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Dec 3 19:01:55 2024 +0300

    home: imp code

commit 5368d8de50
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Dec 3 17:25:37 2024 +0300

    home: imp code

commit fce1bf475f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Dec 2 18:20:31 2024 +0300

    home: slog webapi
This commit is contained in:
Stanislav Chzhen 2024-12-06 16:01:58 +03:00
parent 11dfc7a3e8
commit dab608292a
7 changed files with 175 additions and 109 deletions
internal/home

View file

@ -167,13 +167,13 @@ func setupContext(opts options) (err error) {
if err != nil {
log.Error("parsing configuration file: %s", err)
os.Exit(1)
os.Exit(osutil.ExitCodeFailure)
}
if opts.checkConfig {
log.Info("configuration file is ok")
os.Exit(0)
os.Exit(osutil.ExitCodeSuccess)
}
return nil
@ -522,18 +522,20 @@ func isUpdateEnabled(ctx context.Context, l *slog.Logger, opts *options, customU
}
}
// initWeb initializes the web module.
// initWeb initializes the web module. upd and baseLogger must not be nil.
func initWeb(
ctx context.Context,
opts options,
clientBuildFS fs.FS,
upd *updater.Updater,
l *slog.Logger,
baseLogger *slog.Logger,
customURL bool,
) (web *webAPI, err error) {
logger := baseLogger.With(slogutil.KeyPrefix, "webapi")
var clientFS fs.FS
if opts.localFrontend {
log.Info("warning: using local frontend files")
logger.WarnContext(ctx, "using local frontend files")
clientFS = os.DirFS("build/static")
} else {
@ -543,10 +545,12 @@ func initWeb(
}
}
disableUpdate := !isUpdateEnabled(ctx, l, &opts, customURL)
disableUpdate := !isUpdateEnabled(ctx, baseLogger, &opts, customURL)
webConf := &webConfig{
updater: upd,
updater: upd,
logger: logger,
baseLogger: baseLogger,
clientFS: clientFS,
@ -562,7 +566,7 @@ func initWeb(
serveHTTP3: config.DNS.ServeHTTP3,
}
web = newWebAPI(webConf, l)
web = newWebAPI(ctx, webConf)
if web == nil {
return nil, errors.Error("can not initialize web")
}
@ -640,7 +644,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
fatalOnError(err)
if config.HTTPConfig.Pprof.Enabled {
startPprof(config.HTTPConfig.Pprof.Port)
startPprof(slogLogger, config.HTTPConfig.Pprof.Port)
}
}
@ -692,7 +696,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
checkPermissions(ctx, slogLogger, Context.workDir, confPath, dataDir, statsDir, querylogDir)
}
Context.web.start()
Context.web.start(ctx)
// Wait for other goroutines to complete their job.
<-done
@ -803,15 +807,15 @@ func (c *configuration) anonymizer() (ipmut *aghnet.IPMut) {
return aghnet.NewIPMut(anonFunc)
}
// startMods initializes and starts the DNS server after installation. l must
// not be nil.
func startMods(l *slog.Logger) (err error) {
// startMods initializes and starts the DNS server after installation.
// baseLogger must not be nil.
func startMods(baseLogger *slog.Logger) (err error) {
statsDir, querylogDir, err := checkStatsAndQuerylogDirs(&Context, config)
if err != nil {
return err
}
err = initDNS(l, statsDir, querylogDir)
err = initDNS(baseLogger, statsDir, querylogDir)
if err != nil {
return err
}
@ -984,7 +988,7 @@ func loadCmdLineOpts() (opts options) {
exitWithError()
}
os.Exit(0)
os.Exit(osutil.ExitCodeSuccess)
}
return opts