2023-06-13 13:41:13 +03:00
|
|
|
// Package cmd is the AdGuard Home entry point. It assembles the configuration
|
|
|
|
// file manager, sets up signal processing logic, and so on.
|
2022-04-26 20:50:09 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Move to the upper-level internal/.
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/fs"
|
2022-10-10 14:05:24 +03:00
|
|
|
"os"
|
2022-04-26 20:50:09 +03:00
|
|
|
"time"
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/version"
|
2024-11-13 15:44:21 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
|
|
|
"github.com/AdguardTeam/golibs/service"
|
2022-04-26 20:50:09 +03:00
|
|
|
)
|
|
|
|
|
2023-06-13 13:41:13 +03:00
|
|
|
// Main is the entry point of AdGuard Home.
|
2023-06-29 19:10:39 +03:00
|
|
|
func Main(embeddedFrontend fs.FS) {
|
2024-11-13 15:44:21 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-06-20 14:11:34 +03:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
cmdName := os.Args[0]
|
|
|
|
opts, err := parseOptions(cmdName, os.Args[1:])
|
|
|
|
exitCode, needExit := processOptions(opts, cmdName, err)
|
|
|
|
if needExit {
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
baseLogger := newBaseLogger(opts)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
baseLogger.InfoContext(
|
|
|
|
ctx,
|
|
|
|
"starting adguard home",
|
|
|
|
"version", version.Version(),
|
|
|
|
"pid", os.Getpid(),
|
|
|
|
)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
2023-06-20 14:11:34 +03:00
|
|
|
if opts.workDir != "" {
|
2024-11-13 15:44:21 +03:00
|
|
|
baseLogger.InfoContext(ctx, "changing working directory", "dir", opts.workDir)
|
|
|
|
|
2023-06-20 14:11:34 +03:00
|
|
|
err = os.Chdir(opts.workDir)
|
2024-11-13 15:44:21 +03:00
|
|
|
errors.Check(err)
|
2023-06-20 14:11:34 +03:00
|
|
|
}
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
frontend, err := frontendFromOpts(ctx, baseLogger, opts, embeddedFrontend)
|
|
|
|
errors.Check(err)
|
|
|
|
|
|
|
|
startCtx, startCancel := context.WithTimeout(ctx, defaultTimeoutStart)
|
|
|
|
defer startCancel()
|
2023-06-29 19:10:39 +03:00
|
|
|
|
|
|
|
confMgrConf := &configmgr.Config{
|
2024-11-13 15:44:21 +03:00
|
|
|
BaseLogger: baseLogger,
|
|
|
|
Logger: baseLogger.With(slogutil.KeyPrefix, "configmgr"),
|
|
|
|
Frontend: frontend,
|
|
|
|
WebAddr: opts.webAddr,
|
|
|
|
Start: start,
|
|
|
|
FileName: opts.confFile,
|
2023-06-29 19:10:39 +03:00
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
confMgr, err := configmgr.New(startCtx, confMgrConf)
|
|
|
|
errors.Check(err)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
web := confMgr.Web()
|
2024-11-13 15:44:21 +03:00
|
|
|
err = web.Start(startCtx)
|
|
|
|
errors.Check(err)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
dns := confMgr.DNS()
|
2024-11-13 15:44:21 +03:00
|
|
|
err = dns.Start(startCtx)
|
|
|
|
errors.Check(err)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
sigHdlr := newSignalHandler(
|
2024-11-13 15:44:21 +03:00
|
|
|
baseLogger.With(slogutil.KeyPrefix, service.SignalHandlerPrefix),
|
2023-06-29 19:10:39 +03:00
|
|
|
confMgrConf,
|
|
|
|
opts.pidFile,
|
2022-04-26 20:50:09 +03:00
|
|
|
web,
|
2022-10-10 14:05:24 +03:00
|
|
|
dns,
|
2022-04-26 20:50:09 +03:00
|
|
|
)
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
os.Exit(sigHdlr.handle(ctx))
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
2024-11-13 15:44:21 +03:00
|
|
|
// Default timeouts.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Make configurable.
|
|
|
|
const (
|
|
|
|
defaultTimeoutStart = 1 * time.Minute
|
|
|
|
defaultTimeoutShutdown = 5 * time.Second
|
|
|
|
)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2023-06-29 14:34:06 +03:00
|
|
|
// newConfigMgr returns a new configuration manager using defaultTimeout as the
|
|
|
|
// context timeout.
|
2024-11-13 15:44:21 +03:00
|
|
|
func newConfigMgr(ctx context.Context, c *configmgr.Config) (m *configmgr.Manager, err error) {
|
2023-06-29 19:10:39 +03:00
|
|
|
return configmgr.New(ctx, c)
|
2023-06-29 14:34:06 +03:00
|
|
|
}
|