2022-04-26 20:50:09 +03:00
|
|
|
// Package cmd is the AdGuard Home entry point. It contains the on-disk
|
|
|
|
// configuration file utilities, signal processing logic, and so on.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Move to the upper-level internal/.
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/fs"
|
|
|
|
"math/rand"
|
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"
|
2022-04-26 20:50:09 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Main is the entry point of application.
|
|
|
|
func Main(clientBuildFS fs.FS) {
|
2022-10-04 16:02:55 +03:00
|
|
|
// Initial Configuration
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2022-05-12 17:41:39 +03:00
|
|
|
start := time.Now()
|
|
|
|
rand.Seed(start.UnixNano())
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
// TODO(a.garipov): Set up logging.
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
log.Info("starting adguard home, version %s, pid %d", version.Version(), os.Getpid())
|
|
|
|
|
2022-10-04 16:02:55 +03:00
|
|
|
// Web Service
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
// TODO(a.garipov): Use in the Web service.
|
|
|
|
_ = clientBuildFS
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// TODO(a.garipov): Set up configuration file name.
|
|
|
|
const confFile = "AdGuardHome.1.yaml"
|
|
|
|
|
|
|
|
confMgr, err := configmgr.New(confFile, start)
|
|
|
|
fatalOnError(err)
|
|
|
|
|
|
|
|
web := confMgr.Web()
|
|
|
|
err = web.Start()
|
|
|
|
fatalOnError(err)
|
|
|
|
|
|
|
|
dns := confMgr.DNS()
|
|
|
|
err = dns.Start()
|
2022-04-26 20:50:09 +03:00
|
|
|
fatalOnError(err)
|
|
|
|
|
|
|
|
sigHdlr := newSignalHandler(
|
2022-10-10 14:05:24 +03:00
|
|
|
confFile,
|
|
|
|
start,
|
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
|
|
|
)
|
|
|
|
|
|
|
|
go sigHdlr.handle()
|
|
|
|
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultTimeout is the timeout used for some operations where another timeout
|
|
|
|
// hasn't been defined yet.
|
|
|
|
const defaultTimeout = 15 * time.Second
|
|
|
|
|
|
|
|
// ctxWithDefaultTimeout is a helper function that returns a context with
|
|
|
|
// timeout set to defaultTimeout.
|
|
|
|
func ctxWithDefaultTimeout() (ctx context.Context, cancel context.CancelFunc) {
|
|
|
|
return context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fatalOnError is a helper that exits the program with an error code if err is
|
|
|
|
// not nil. It must only be used within Main.
|
|
|
|
func fatalOnError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|