Pull request: websvc: add system info

Merge in DNS/adguard-home from websvc-system-info to master

Squashed commit of the following:

commit 333aaa0602da254e25e0262a10080bf44a3718a7
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu May 12 16:32:32 2022 +0300

    websvc: fmt

commit d8a35bf71dcc59fdd595494e5b220e3d24516728
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu May 12 16:10:11 2022 +0300

    websvc: refactor, imp tests

commit dfeb24f3f35513bf51323d3ab6f717f582a1defc
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 11 20:52:02 2022 +0300

    websvc: add system info
This commit is contained in:
Ainar Garipov
2022-05-12 17:41:39 +03:00
parent 58515fce43
commit f289f4b1b6
11 changed files with 260 additions and 17 deletions

View File

@@ -17,6 +17,7 @@ import (
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
httptreemux "github.com/dimfeld/httptreemux/v5"
)
// Config is the AdGuard Home web service configuration structure.
@@ -32,6 +33,9 @@ type Config struct {
// SecureAddresses is not empty, TLS must not be nil.
SecureAddresses []*netutil.IPPort
// Start is the time of start of AdGuard Home.
Start time.Time
// Timeout is the timeout for all server operations.
Timeout time.Duration
}
@@ -41,6 +45,7 @@ type Config struct {
type Service struct {
tls *tls.Config
servers []*http.Server
start time.Time
timeout time.Duration
}
@@ -53,11 +58,11 @@ func New(c *Config) (svc *Service) {
svc = &Service{
tls: c.TLS,
start: c.Start,
timeout: c.Timeout,
}
mux := http.NewServeMux()
mux.HandleFunc("/health-check", svc.handleGetHealthCheck)
mux := newMux(svc)
for _, a := range c.Addresses {
addr := a.String()
@@ -91,6 +96,43 @@ func New(c *Config) (svc *Service) {
return svc
}
// newMux returns a new HTTP request multiplexor for the AdGuard Home web
// service.
func newMux(svc *Service) (mux *httptreemux.ContextMux) {
mux = httptreemux.NewContextMux()
routes := []struct {
handler http.HandlerFunc
method string
path string
isJSON bool
}{{
handler: svc.handleGetHealthCheck,
method: http.MethodGet,
path: PathHealthCheck,
isJSON: false,
}, {
handler: svc.handleGetV1SystemInfo,
method: http.MethodGet,
path: PathV1SystemInfo,
isJSON: true,
}}
for _, r := range routes {
var h http.HandlerFunc
if r.isJSON {
// TODO(a.garipov): Consider using httptreemux's MiddlewareFunc.
h = jsonMw(r.handler)
} else {
h = r.handler
}
mux.Handle(r.method, r.path, h)
}
return mux
}
// Addrs returns all addresses on which this server serves the HTTP API. Addrs
// must not be called until Start returns.
func (svc *Service) Addrs() (addrs []string) {