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-08-10 13:39:28 +03:00
|
|
|
"net/netip"
|
2022-04-26 20:50:09 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/v1/websvc"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Main is the entry point of application.
|
|
|
|
func Main(clientBuildFS fs.FS) {
|
|
|
|
// # Initial Configuration
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
// # Web Service
|
|
|
|
|
|
|
|
// TODO(a.garipov): Use in the Web service.
|
|
|
|
_ = clientBuildFS
|
|
|
|
|
|
|
|
// TODO(a.garipov): Make configurable.
|
|
|
|
web := websvc.New(&websvc.Config{
|
2022-08-10 13:39:28 +03:00
|
|
|
Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:3001")},
|
|
|
|
Start: start,
|
|
|
|
Timeout: 60 * time.Second,
|
2022-04-26 20:50:09 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
err := web.Start()
|
|
|
|
fatalOnError(err)
|
|
|
|
|
|
|
|
sigHdlr := newSignalHandler(
|
|
|
|
web,
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|