aghhttp: registrar

This commit is contained in:
Stanislav Chzhen 2025-10-08 15:38:01 +03:00
parent a356473855
commit 9155edef67
22 changed files with 176 additions and 158 deletions

View File

@ -0,0 +1,80 @@
package aghhttp
import (
"net/http"
"sync"
)
// Registrar registers an HTTP handler for a method and path.
type Registrar interface {
Register(method, path string, h http.HandlerFunc)
}
// DeferredRegistrar is an implementation of [Registrar] that queues handler
// registrations until Bind is called.
type DeferredRegistrar struct {
mu *sync.Mutex
registerFn RegisterFunc
queue []item
}
// item is an entry in the [DeferredRegistrar] queue.
type item struct {
handlerFn http.HandlerFunc
method string
path string
}
// NewDeferredRegistrar returns a new properly initialized *DeferredRegistrar.
func NewDeferredRegistrar() (r *DeferredRegistrar) {
return &DeferredRegistrar{
mu: &sync.Mutex{},
}
}
// type check
var _ Registrar = (*DeferredRegistrar)(nil)
// Register implements the [Registrar] interface.
func (r *DeferredRegistrar) Register(method, path string, h http.HandlerFunc) {
var fn RegisterFunc
defer func() {
if fn != nil {
fn(method, path, h)
}
}()
r.mu.Lock()
defer r.mu.Unlock()
if r.registerFn == nil {
r.queue = append(r.queue, item{
handlerFn: h,
method: method,
path: path,
})
return
}
fn = r.registerFn
}
// Bind registers queued HTTP handlers with fn and uses fn for future
// registrations.
func (r *DeferredRegistrar) Bind(fn RegisterFunc) {
var q []item
func() {
r.mu.Lock()
defer r.mu.Unlock()
q = r.queue
r.queue = nil
r.registerFn = fn
}()
for _, it := range q {
fn(it.method, it.path, it.handlerFn)
}
}

View File

@ -31,7 +31,7 @@ type ServerConfig struct {
ConfModifier agh.ConfigModifier `yaml:"-"`
// Register an HTTP handler
HTTPRegister aghhttp.RegisterFunc `yaml:"-"`
HTTPReg aghhttp.Registrar `yaml:"-"`
Enabled bool `yaml:"enabled"`
InterfaceName string `yaml:"interface_name"`

View File

@ -112,7 +112,7 @@ func Create(ctx context.Context, conf *ServerConfig) (s *server, err error) {
CommandConstructor: conf.CommandConstructor,
ConfModifier: conf.ConfModifier,
HTTPRegister: conf.HTTPRegister,
HTTPReg: conf.HTTPReg,
Enabled: conf.Enabled,
InterfaceName: conf.InterfaceName,

View File

@ -741,7 +741,7 @@ func (s *server) handleReset(w http.ResponseWriter, r *http.Request) {
s.conf = &ServerConfig{
ConfModifier: s.conf.ConfModifier,
HTTPRegister: s.conf.HTTPRegister,
HTTPReg: s.conf.HTTPReg,
LocalDomainName: s.conf.LocalDomainName,
@ -778,17 +778,17 @@ func (s *server) handleResetLeases(w http.ResponseWriter, r *http.Request) {
}
func (s *server) registerHandlers() {
if s.conf.HTTPRegister == nil {
if s.conf.HTTPReg == nil {
return
}
s.conf.HTTPRegister(http.MethodGet, "/control/dhcp/status", s.handleDHCPStatus)
s.conf.HTTPRegister(http.MethodGet, "/control/dhcp/interfaces", s.handleDHCPInterfaces)
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/set_config", s.handleDHCPSetConfig)
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/find_active_dhcp", s.handleDHCPFindActiveServer)
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/add_static_lease", s.handleDHCPAddStaticLease)
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/remove_static_lease", s.handleDHCPRemoveStaticLease)
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/update_static_lease", s.handleDHCPUpdateStaticLease)
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/reset", s.handleReset)
s.conf.HTTPRegister(http.MethodPost, "/control/dhcp/reset_leases", s.handleResetLeases)
s.conf.HTTPReg.Register(http.MethodGet, "/control/dhcp/status", s.handleDHCPStatus)
s.conf.HTTPReg.Register(http.MethodGet, "/control/dhcp/interfaces", s.handleDHCPInterfaces)
s.conf.HTTPReg.Register(http.MethodPost, "/control/dhcp/set_config", s.handleDHCPSetConfig)
s.conf.HTTPReg.Register(http.MethodPost, "/control/dhcp/find_active_dhcp", s.handleDHCPFindActiveServer)
s.conf.HTTPReg.Register(http.MethodPost, "/control/dhcp/add_static_lease", s.handleDHCPAddStaticLease)
s.conf.HTTPReg.Register(http.MethodPost, "/control/dhcp/remove_static_lease", s.handleDHCPRemoveStaticLease)
s.conf.HTTPReg.Register(http.MethodPost, "/control/dhcp/update_static_lease", s.handleDHCPUpdateStaticLease)
s.conf.HTTPReg.Register(http.MethodPost, "/control/dhcp/reset", s.handleReset)
s.conf.HTTPReg.Register(http.MethodPost, "/control/dhcp/reset_leases", s.handleResetLeases)
}

View File

@ -270,7 +270,7 @@ type ServerConfig struct {
ConfModifier agh.ConfigModifier
// Register an HTTP handler
HTTPRegister aghhttp.RegisterFunc
HTTPReg aghhttp.Registrar
// LocalPTRResolvers is a slice of addresses to be used as upstreams for
// resolving PTR queries for local addresses.

View File

@ -837,19 +837,19 @@ func (s *Server) handleDoH(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) registerHandlers() {
if webRegistered || s.conf.HTTPRegister == nil {
if webRegistered || s.conf.HTTPReg == nil {
return
}
s.conf.HTTPRegister(http.MethodGet, "/control/dns_info", s.handleGetConfig)
s.conf.HTTPRegister(http.MethodPost, "/control/dns_config", s.handleSetConfig)
s.conf.HTTPRegister(http.MethodPost, "/control/test_upstream_dns", s.handleTestUpstreamDNS)
s.conf.HTTPRegister(http.MethodPost, "/control/protection", s.handleSetProtection)
s.conf.HTTPReg.Register(http.MethodGet, "/control/dns_info", s.handleGetConfig)
s.conf.HTTPReg.Register(http.MethodPost, "/control/dns_config", s.handleSetConfig)
s.conf.HTTPReg.Register(http.MethodPost, "/control/test_upstream_dns", s.handleTestUpstreamDNS)
s.conf.HTTPReg.Register(http.MethodPost, "/control/protection", s.handleSetProtection)
s.conf.HTTPRegister(http.MethodGet, "/control/access/list", s.handleAccessList)
s.conf.HTTPRegister(http.MethodPost, "/control/access/set", s.handleAccessSet)
s.conf.HTTPReg.Register(http.MethodGet, "/control/access/list", s.handleAccessList)
s.conf.HTTPReg.Register(http.MethodPost, "/control/access/set", s.handleAccessSet)
s.conf.HTTPRegister(http.MethodPost, "/control/cache_clear", s.handleCacheClear)
s.conf.HTTPReg.Register(http.MethodPost, "/control/cache_clear", s.handleCacheClear)
// Register both versions, with and without the trailing slash, to
// prevent a 301 Moved Permanently redirect when clients request the
@ -858,8 +858,8 @@ func (s *Server) registerHandlers() {
// See go doc net/http.ServeMux.
//
// See also https://github.com/AdguardTeam/AdGuardHome/issues/2628.
s.conf.HTTPRegister("", "/dns-query", s.handleDoH)
s.conf.HTTPRegister("", "/dns-query/", s.handleDoH)
s.conf.HTTPReg.Register("", "/dns-query", s.handleDoH)
s.conf.HTTPReg.Register("", "/dns-query/", s.handleDoH)
webRegistered = true
}

View File

@ -110,7 +110,7 @@ type Config struct {
ConfModifier agh.ConfigModifier `yaml:"-"`
// Register an HTTP handler
HTTPRegister aghhttp.RegisterFunc `yaml:"-"`
HTTPReg aghhttp.Registrar `yaml:"-"`
// HTTPClient is the client to use for updating the remote filters.
HTTPClient *http.Client `yaml:"-"`

View File

@ -711,7 +711,7 @@ func (d *DNSFilter) handleParentalStatus(w http.ResponseWriter, r *http.Request)
// RegisterFilteringHandlers - register handlers
func (d *DNSFilter) RegisterFilteringHandlers() {
registerHTTP := d.conf.HTTPRegister
registerHTTP := d.conf.HTTPReg.Register
if registerHTTP == nil {
return
}

View File

@ -270,7 +270,7 @@ func (web *webAPI) registerAuthHandlers() {
"/control/login",
web.postInstallHandler(ensure(http.MethodPost, web.handleLogin)),
)
web.httpRegister(http.MethodGet, "/control/logout", web.handleLogout)
web.httpReg.Register(http.MethodGet, "/control/logout", web.handleLogout)
}
// isPublicResource returns true if p is a path to a public resource.

View File

@ -45,7 +45,7 @@ type clientsContainer struct {
// nil.
confModifier agh.ConfigModifier
httpRegister aghhttp.RegisterFunc
httpReg aghhttp.Registrar
// lock protects all fields.
//
@ -82,7 +82,7 @@ func (clients *clientsContainer) Init(
filteringConf *filtering.Config,
sigHdlr *signalHandler,
confModifier agh.ConfigModifier,
httpRegister aghhttp.RegisterFunc,
httpReg aghhttp.Registrar,
) (err error) {
// TODO(s.chzhen): Refactor it.
if clients.storage != nil {
@ -94,7 +94,7 @@ func (clients *clientsContainer) Init(
clients.safeSearchCacheSize = filteringConf.SafeSearchCacheSize
clients.safeSearchCacheTTL = time.Minute * time.Duration(filteringConf.CacheTime)
clients.confModifier = confModifier
clients.httpRegister = httpRegister
clients.httpReg = httpReg
confClients := make([]*client.Persistent, 0, len(objects))
for i, o := range objects {

View File

@ -615,12 +615,12 @@ func (clients *clientsContainer) findRuntime(
// registerWebHandlers registers HTTP handlers.
func (clients *clientsContainer) registerWebHandlers() {
clients.httpRegister(http.MethodGet, "/control/clients", clients.handleGetClients)
clients.httpRegister(http.MethodPost, "/control/clients/add", clients.handleAddClient)
clients.httpRegister(http.MethodPost, "/control/clients/delete", clients.handleDelClient)
clients.httpRegister(http.MethodPost, "/control/clients/update", clients.handleUpdateClient)
clients.httpRegister(http.MethodPost, "/control/clients/search", clients.handleSearchClient)
clients.httpReg.Register(http.MethodGet, "/control/clients", clients.handleGetClients)
clients.httpReg.Register(http.MethodPost, "/control/clients/add", clients.handleAddClient)
clients.httpReg.Register(http.MethodPost, "/control/clients/delete", clients.handleDelClient)
clients.httpReg.Register(http.MethodPost, "/control/clients/update", clients.handleUpdateClient)
clients.httpReg.Register(http.MethodPost, "/control/clients/search", clients.handleSearchClient)
// Deprecated handler.
clients.httpRegister(http.MethodGet, "/control/clients/find", clients.handleFindClient)
clients.httpReg.Register(http.MethodGet, "/control/clients/find", clients.handleFindClient)
}

View File

@ -7,7 +7,6 @@ import (
"net/url"
"runtime"
"strings"
"sync"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
@ -179,13 +178,13 @@ func (web *webAPI) registerControlHandlers() {
"/control/version.json",
web.postInstallHandler(http.HandlerFunc(web.handleVersionJSON)),
)
web.httpRegister(http.MethodPost, "/control/update", web.handleUpdate)
web.httpReg.Register(http.MethodPost, "/control/update", web.handleUpdate)
web.httpRegister(http.MethodGet, "/control/status", web.handleStatus)
web.httpRegister(http.MethodPost, "/control/i18n/change_language", web.handleI18nChangeLanguage)
web.httpRegister(http.MethodGet, "/control/i18n/current_language", handleI18nCurrentLanguage)
web.httpRegister(http.MethodGet, "/control/profile", web.handleGetProfile)
web.httpRegister(http.MethodPut, "/control/profile/update", web.handlePutProfile)
web.httpReg.Register(http.MethodGet, "/control/status", web.handleStatus)
web.httpReg.Register(http.MethodPost, "/control/i18n/change_language", web.handleI18nChangeLanguage)
web.httpReg.Register(http.MethodGet, "/control/i18n/current_language", handleI18nCurrentLanguage)
web.httpReg.Register(http.MethodGet, "/control/profile", web.handleGetProfile)
web.httpReg.Register(http.MethodPut, "/control/profile/update", web.handlePutProfile)
// No authentication is required for DoH/DoT configuration endpoints.
mux.Handle(
@ -215,66 +214,6 @@ func (web *webAPI) register(method, path string, handler http.HandlerFunc) {
)
}
// TODO(s.chzhen): !! Consider alternative approaches.
type httpRegistrar struct {
mu *sync.Mutex
registerFn aghhttp.RegisterFunc
queue []item
}
type item struct {
handlerFn http.HandlerFunc
method string
path string
}
func newHTTPRegistrar() (r *httpRegistrar) {
return &httpRegistrar{
mu: &sync.Mutex{},
}
}
func (r *httpRegistrar) register(method, path string, h http.HandlerFunc) {
var fn aghhttp.RegisterFunc
defer func() {
if fn != nil {
fn(method, path, h)
}
}()
r.mu.Lock()
defer r.mu.Unlock()
if r.registerFn == nil {
r.queue = append(r.queue, item{
handlerFn: h,
method: method,
path: path,
})
return
}
fn = r.registerFn
}
func (r *httpRegistrar) bind(fn aghhttp.RegisterFunc) {
var q []item
func() {
r.mu.Lock()
defer r.mu.Unlock()
q = r.queue
r.queue = nil
r.registerFn = fn
}()
for _, it := range q {
fn(it.method, it.path, it.handlerFn)
}
}
// ensure returns a wrapped handler that makes sure that the request has the
// correct method as well as additional method and header checks.
func ensure(

View File

@ -510,7 +510,7 @@ func (web *webAPI) finalizeInstall(
// moment we'll allow setting up TLS in the initial configuration or the
// configuration itself will use HTTPS protocol, because the underlying
// functions potentially restart the HTTPS server.
err = startMods(ctx, web.baseLogger, web.tlsManager, web.confModifier, web.httpRegister)
err = startMods(ctx, web.baseLogger, web.tlsManager, web.confModifier, web.httpReg)
if err != nil {
aghhttp.ErrorAndLog(ctx, web.logger, r, w, http.StatusInternalServerError, "%s", err)
@ -599,14 +599,14 @@ func startMods(
baseLogger *slog.Logger,
tlsMgr *tlsManager,
confModifier agh.ConfigModifier,
httpRegister aghhttp.RegisterFunc,
httpReg aghhttp.Registrar,
) (err error) {
statsDir, querylogDir, err := checkStatsAndQuerylogDirs(&globalContext, config)
if err != nil {
return err
}
err = initDNS(ctx, baseLogger, tlsMgr, confModifier, httpRegister, statsDir, querylogDir)
err = initDNS(ctx, baseLogger, tlsMgr, confModifier, httpReg, statsDir, querylogDir)
if err != nil {
return err
}

View File

@ -48,7 +48,7 @@ func initDNS(
baseLogger *slog.Logger,
tlsMgr *tlsManager,
confModifier agh.ConfigModifier,
httpRegister aghhttp.RegisterFunc,
httpReg aghhttp.Registrar,
statsDir string,
querylogDir string,
) (err error) {
@ -59,7 +59,7 @@ func initDNS(
Filename: filepath.Join(statsDir, "stats.db"),
Limit: time.Duration(config.Stats.Interval),
ConfigModifier: confModifier,
HTTPRegister: httpRegister,
HTTPReg: httpReg,
Enabled: config.Stats.Enabled,
ShouldCountClient: globalContext.clients.shouldCountClient,
}
@ -79,7 +79,7 @@ func initDNS(
Logger: baseLogger.With(slogutil.KeyPrefix, "querylog"),
Anonymizer: anonymizer,
ConfigModifier: confModifier,
HTTPRegister: httpRegister,
HTTPReg: httpReg,
FindClient: globalContext.clients.findMultiple,
BaseDir: querylogDir,
AnonymizeClientIP: config.DNS.AnonymizeClientIP,
@ -113,7 +113,7 @@ func initDNS(
globalContext.queryLog,
globalContext.dhcpServer,
anonymizer,
httpRegister,
httpReg,
tlsMgr,
baseLogger,
confModifier,
@ -133,7 +133,7 @@ func initDNSServer(
qlog querylog.QueryLog,
dhcpSrv dnsforward.DHCP,
anonymizer *aghnet.IPMut,
httpRegister aghhttp.RegisterFunc,
httpReg aghhttp.Registrar,
tlsMgr *tlsManager,
l *slog.Logger,
confModifier agh.ConfigModifier,
@ -165,7 +165,7 @@ func initDNSServer(
config.Clients.Sources,
tlsMgr.config(),
tlsMgr,
httpRegister,
httpReg,
globalContext.clients.storage,
confModifier,
)
@ -242,7 +242,7 @@ func newServerConfig(
clientSrcConf *clientSourcesConfig,
tlsConf *tlsConfigSettings,
tlsMgr *tlsManager,
httpRegister aghhttp.RegisterFunc,
httpReg aghhttp.Registrar,
clientsContainer dnsforward.ClientsContainer,
confModifier agh.ConfigModifier,
) (newConf *dnsforward.ServerConfig, err error) {
@ -265,7 +265,7 @@ func newServerConfig(
UpstreamTimeout: time.Duration(dnsConf.UpstreamTimeout),
TLSv12Roots: tlsMgr.rootCerts,
ConfModifier: confModifier,
HTTPRegister: httpRegister,
HTTPReg: httpReg,
LocalPTRResolvers: dnsConf.PrivateRDNSResolvers,
UseDNS64: dnsConf.UseDNS64,
DNS64Prefixes: dnsConf.DNS64Prefixes,

View File

@ -303,12 +303,12 @@ func initContextClients(
logger *slog.Logger,
sigHdlr *signalHandler,
confModifier agh.ConfigModifier,
httpRegister aghhttp.RegisterFunc,
httpReg aghhttp.Registrar,
) (err error) {
//lint:ignore SA1019 Migration is not over.
config.DHCP.WorkDir = globalContext.workDir
config.DHCP.DataDir = globalContext.getDataDir()
config.DHCP.HTTPRegister = httpRegister
config.DHCP.HTTPReg = httpReg
config.DHCP.CommandConstructor = executil.SystemCommandConstructor{}
config.DHCP.Logger = logger.With(slogutil.KeyPrefix, "dhcpd")
config.DHCP.ConfModifier = confModifier
@ -337,7 +337,7 @@ func initContextClients(
config.Filtering,
sigHdlr,
confModifier,
httpRegister,
httpReg,
)
}
@ -389,7 +389,7 @@ func setupDNSFilteringConf(
conf *filtering.Config,
tlsMgr *tlsManager,
confModifier agh.ConfigModifier,
httpRegister aghhttp.RegisterFunc,
httpReg aghhttp.Registrar,
) (err error) {
const (
dnsTimeout = 3 * time.Second
@ -412,7 +412,7 @@ func setupDNSFilteringConf(
}
conf.ConfModifier = confModifier
conf.HTTPRegister = httpRegister
conf.HTTPReg = httpReg
conf.DataDir = globalContext.getDataDir()
conf.Filters = slices.Clone(config.Filters)
conf.WhitelistFilters = slices.Clone(config.WhitelistFilters)
@ -579,7 +579,7 @@ func initWeb(
auth *auth,
mux *http.ServeMux,
confModifier agh.ConfigModifier,
httpRegister aghhttp.RegisterFunc,
httpReg aghhttp.Registrar,
isCustomUpdURL bool,
isFirstRun bool,
) (web *webAPI, err error) {
@ -607,7 +607,7 @@ func initWeb(
logger: logger,
baseLogger: baseLogger,
confModifier: confModifier,
httpRegister: httpRegister,
httpReg: httpReg,
tlsManager: tlsMgr,
auth: auth,
mux: mux,
@ -724,10 +724,9 @@ func run(
slogLogger.With(slogutil.KeyPrefix, "config_modifier"),
)
reg := newHTTPRegistrar()
httpRegister := reg.register
httpReg := aghhttp.NewDeferredRegistrar()
err = initContextClients(ctx, slogLogger, sigHdlr, confModifier, httpRegister)
err = initContextClients(ctx, slogLogger, sigHdlr, confModifier, httpReg)
fatalOnError(err)
tlsMgrLogger := slogLogger.With(slogutil.KeyPrefix, "tls_manager")
@ -735,7 +734,7 @@ func run(
tlsMgr, err := newTLSManager(ctx, &tlsManagerConfig{
logger: tlsMgrLogger,
confModifier: confModifier,
httpRegister: httpRegister,
httpReg: httpReg,
tlsSettings: config.TLS,
servePlainDNS: config.DNS.ServePlainDNS,
})
@ -752,7 +751,7 @@ func run(
config.Filtering,
tlsMgr,
confModifier,
httpRegister,
httpReg,
)
fatalOnError(err)
@ -800,13 +799,13 @@ func run(
auth,
globalContext.mux,
confModifier,
httpRegister,
httpReg,
isCustomURL,
isFirstRun,
)
fatalOnError(err)
reg.bind(web.register)
httpReg.Bind(web.register)
globalContext.web = web
@ -817,7 +816,7 @@ func run(
fatalOnError(err)
if !isFirstRun {
err = initDNS(ctx, slogLogger, tlsMgr, confModifier, httpRegister, statsDir, querylogDir)
err = initDNS(ctx, slogLogger, tlsMgr, confModifier, httpReg, statsDir, querylogDir)
fatalOnError(err)
tlsMgr.start(ctx)

View File

@ -60,7 +60,7 @@ type tlsManager struct {
// confModifier is used to update the global configuration.
confModifier agh.ConfigModifier
httpRegister aghhttp.RegisterFunc
httpReg aghhttp.Registrar
// customCipherIDs are the IDs of the cipher suites that AdGuard Home must
// use.
@ -80,7 +80,7 @@ type tlsManagerConfig struct {
// nil.
confModifier agh.ConfigModifier
httpRegister aghhttp.RegisterFunc
httpReg aghhttp.Registrar
// tlsSettings contains the TLS configuration settings.
tlsSettings tlsConfigSettings
@ -99,7 +99,7 @@ func newTLSManager(ctx context.Context, conf *tlsManagerConfig) (m *tlsManager,
logger: conf.logger,
mu: &sync.Mutex{},
confModifier: conf.confModifier,
httpRegister: conf.httpRegister,
httpReg: conf.httpReg,
status: &tlsConfigStatus{},
conf: &conf.tlsSettings,
servePlainDNS: conf.servePlainDNS,
@ -257,7 +257,7 @@ func (m *tlsManager) reconfigureDNSServer(ctx context.Context) (err error) {
config.Clients.Sources,
m.conf,
m,
m.httpRegister,
m.httpReg,
globalContext.clients.storage,
m.confModifier,
)
@ -1049,7 +1049,7 @@ func marshalTLS(w http.ResponseWriter, r *http.Request, data tlsConfig) {
// registerWebHandlers registers HTTP handlers for TLS configuration.
func (m *tlsManager) registerWebHandlers() {
m.httpRegister(http.MethodGet, "/control/tls/status", m.handleTLSStatus)
m.httpRegister(http.MethodPost, "/control/tls/configure", m.handleTLSConfigure)
m.httpRegister(http.MethodPost, "/control/tls/validate", m.handleTLSValidate)
m.httpReg.Register(http.MethodGet, "/control/tls/status", m.handleTLSStatus)
m.httpReg.Register(http.MethodPost, "/control/tls/configure", m.handleTLSConfigure)
m.httpReg.Register(http.MethodPost, "/control/tls/validate", m.handleTLSValidate)
}

View File

@ -56,7 +56,7 @@ type webConfig struct {
// confModifier is used to update the global configuration.
confModifier agh.ConfigModifier
httpRegister aghhttp.RegisterFunc
httpReg aghhttp.Registrar
// tlsManager contains the current configuration and state of TLS
// encryption. It must not be nil.
@ -128,7 +128,7 @@ type webAPI struct {
// cmdCons is used to run external commands.
cmdCons executil.CommandConstructor
httpRegister aghhttp.RegisterFunc
httpReg aghhttp.Registrar
// TODO(a.garipov): Refactor all these servers.
httpServer *http.Server
@ -162,7 +162,7 @@ func newWebAPI(ctx context.Context, conf *webConfig) (w *webAPI) {
w = &webAPI{
conf: conf,
confModifier: conf.confModifier,
httpRegister: conf.httpRegister,
httpReg: conf.httpReg,
cmdCons: conf.CommandConstructor,
logger: conf.logger,
baseLogger: conf.baseLogger,

View File

@ -59,18 +59,18 @@ type getConfigResp struct {
// Register web handlers
func (l *queryLog) initWeb() {
l.conf.HTTPRegister(http.MethodGet, "/control/querylog", l.handleQueryLog)
l.conf.HTTPRegister(http.MethodPost, "/control/querylog_clear", l.handleQueryLogClear)
l.conf.HTTPRegister(http.MethodGet, "/control/querylog/config", l.handleGetQueryLogConfig)
l.conf.HTTPRegister(
l.conf.HTTPReg.Register(http.MethodGet, "/control/querylog", l.handleQueryLog)
l.conf.HTTPReg.Register(http.MethodPost, "/control/querylog_clear", l.handleQueryLogClear)
l.conf.HTTPReg.Register(http.MethodGet, "/control/querylog/config", l.handleGetQueryLogConfig)
l.conf.HTTPReg.Register(
http.MethodPut,
"/control/querylog/config/update",
l.handlePutQueryLogConfig,
)
// Deprecated handlers.
l.conf.HTTPRegister(http.MethodGet, "/control/querylog_info", l.handleQueryLogInfo)
l.conf.HTTPRegister(http.MethodPost, "/control/querylog_config", l.handleQueryLogConfig)
l.conf.HTTPReg.Register(http.MethodGet, "/control/querylog_info", l.handleQueryLogInfo)
l.conf.HTTPReg.Register(http.MethodPost, "/control/querylog_config", l.handleQueryLogConfig)
}
// handleQueryLog is the handler for the GET /control/querylog HTTP API.

View File

@ -87,7 +87,7 @@ var _ QueryLog = (*queryLog)(nil)
// Start implements the [QueryLog] interface for *queryLog.
func (l *queryLog) Start(ctx context.Context) (err error) {
if l.conf.HTTPRegister != nil {
if l.conf.HTTPReg != nil {
l.initWeb()
}

View File

@ -53,7 +53,7 @@ type Config struct {
ConfigModifier agh.ConfigModifier
// HTTPRegister registers an HTTP handler.
HTTPRegister aghhttp.RegisterFunc
HTTPReg aghhttp.Registrar
// FindClient returns client information by their IDs.
FindClient func(ids []string) (c *Client, err error)

View File

@ -244,16 +244,16 @@ func (s *StatsCtx) handleStatsReset(w http.ResponseWriter, r *http.Request) {
// initWeb registers the handlers for web endpoints of statistics module.
func (s *StatsCtx) initWeb() {
if s.httpRegister == nil {
if s.httpReg == nil {
return
}
s.httpRegister(http.MethodGet, "/control/stats", s.handleStats)
s.httpRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset)
s.httpRegister(http.MethodGet, "/control/stats/config", s.handleGetStatsConfig)
s.httpRegister(http.MethodPut, "/control/stats/config/update", s.handlePutStatsConfig)
s.httpReg.Register(http.MethodGet, "/control/stats", s.handleStats)
s.httpReg.Register(http.MethodPost, "/control/stats_reset", s.handleStatsReset)
s.httpReg.Register(http.MethodGet, "/control/stats/config", s.handleGetStatsConfig)
s.httpReg.Register(http.MethodPut, "/control/stats/config/update", s.handlePutStatsConfig)
// Deprecated handlers.
s.httpRegister(http.MethodGet, "/control/stats_info", s.handleStatsInfo)
s.httpRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig)
s.httpReg.Register(http.MethodGet, "/control/stats_info", s.handleStatsInfo)
s.httpReg.Register(http.MethodPost, "/control/stats_config", s.handleStatsConfig)
}

View File

@ -65,7 +65,7 @@ type Config struct {
// HTTPRegister is the function that registers handlers for the stats
// endpoints.
HTTPRegister aghhttp.RegisterFunc
HTTPReg aghhttp.Registrar
// Ignored contains the list of host names, which should not be counted,
// and matches them.
@ -121,8 +121,8 @@ type StatsCtx struct {
// unit. It's here for only testing purposes.
unitIDGen UnitIDGenFunc
// httpRegister is used to set HTTP handlers.
httpRegister aghhttp.RegisterFunc
// httpReg is used to set HTTP handlers.
httpReg aghhttp.Registrar
// configModifier is used to update the global configuration.
configModifier agh.ConfigModifier
@ -164,7 +164,7 @@ func New(conf Config) (s *StatsCtx, err error) {
s = &StatsCtx{
logger: conf.Logger,
currMu: &sync.RWMutex{},
httpRegister: conf.HTTPRegister,
httpReg: conf.HTTPReg,
configModifier: conf.ConfigModifier,
filename: conf.Filename,