Skip to content

Commit 681c604

Browse files
committed
Pull request 1870: nextapi-frontend-fs
Merge in DNS/adguard-home from nextapi-frontend-fs to master Squashed commit of the following: commit 3ed959f Merge: e60bbdd 9fda7bf Author: Ainar Garipov <[email protected]> Date: Tue Jun 13 13:37:00 2023 +0300 Merge branch 'master' into nextapi-frontend-fs commit e60bbdd Author: Ainar Garipov <[email protected]> Date: Fri Jun 9 14:53:09 2023 +0300 next: support frontend fs
1 parent 9fda7bf commit 681c604

14 files changed

+169
-108
lines changed

internal/aghnet/interfaces_windows.go

-17
This file was deleted.

internal/dhcpd/os_windows.go

-15
This file was deleted.

internal/next/cmd/cmd.go

+14-21
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
// Package cmd is the AdGuard Home entry point. It contains the on-disk
2-
// configuration file utilities, signal processing logic, and so on.
1+
// Package cmd is the AdGuard Home entry point. It assembles the configuration
2+
// file manager, sets up signal processing logic, and so on.
33
//
44
// TODO(a.garipov): Move to the upper-level internal/.
55
package cmd
66

77
import (
88
"context"
99
"io/fs"
10-
"math/rand"
1110
"os"
1211
"time"
1312

@@ -16,62 +15,56 @@ import (
1615
"github.com/AdguardTeam/golibs/log"
1716
)
1817

19-
// Main is the entry point of application.
20-
func Main(clientBuildFS fs.FS) {
18+
// Main is the entry point of AdGuard Home.
19+
func Main(frontend fs.FS) {
2120
// Initial Configuration
2221

2322
start := time.Now()
24-
rand.Seed(start.UnixNano())
2523

2624
// TODO(a.garipov): Set up logging.
2725

2826
log.Info("starting adguard home, version %s, pid %d", version.Version(), os.Getpid())
2927

3028
// Web Service
3129

32-
// TODO(a.garipov): Use in the Web service.
33-
_ = clientBuildFS
34-
3530
// TODO(a.garipov): Set up configuration file name.
3631
const confFile = "AdGuardHome.1.yaml"
3732

38-
confMgr, err := configmgr.New(confFile, start)
39-
fatalOnError(err)
33+
confMgr, err := configmgr.New(confFile, frontend, start)
34+
check(err)
4035

4136
web := confMgr.Web()
4237
err = web.Start()
43-
fatalOnError(err)
38+
check(err)
4439

4540
dns := confMgr.DNS()
4641
err = dns.Start()
47-
fatalOnError(err)
42+
check(err)
4843

4944
sigHdlr := newSignalHandler(
5045
confFile,
46+
frontend,
5147
start,
5248
web,
5349
dns,
5450
)
5551

56-
go sigHdlr.handle()
57-
58-
select {}
52+
sigHdlr.handle()
5953
}
6054

6155
// defaultTimeout is the timeout used for some operations where another timeout
6256
// hasn't been defined yet.
63-
const defaultTimeout = 15 * time.Second
57+
const defaultTimeout = 5 * time.Second
6458

6559
// ctxWithDefaultTimeout is a helper function that returns a context with
6660
// timeout set to defaultTimeout.
6761
func ctxWithDefaultTimeout() (ctx context.Context, cancel context.CancelFunc) {
6862
return context.WithTimeout(context.Background(), defaultTimeout)
6963
}
7064

71-
// fatalOnError is a helper that exits the program with an error code if err is
72-
// not nil. It must only be used within Main.
73-
func fatalOnError(err error) {
65+
// check is a simple error-checking helper. It must only be used within Main.
66+
func check(err error) {
7467
if err != nil {
75-
log.Fatal(err)
68+
panic(err)
7669
}
7770
}

internal/next/cmd/signal.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"io/fs"
45
"os"
56
"time"
67

@@ -18,6 +19,10 @@ type signalHandler struct {
1819
// confFile is the path to the configuration file.
1920
confFile string
2021

22+
// frontend is the filesystem with the frontend and other statically
23+
// compiled files.
24+
frontend fs.FS
25+
2126
// start is the time at which AdGuard Home has been started.
2227
start time.Time
2328

@@ -58,16 +63,16 @@ func (h *signalHandler) reconfigure() {
5863
// reconfigured without the full shutdown, and the error handling is
5964
// currently not the best.
6065

61-
confMgr, err := configmgr.New(h.confFile, h.start)
62-
fatalOnError(err)
66+
confMgr, err := configmgr.New(h.confFile, h.frontend, h.start)
67+
check(err)
6368

6469
web := confMgr.Web()
6570
err = web.Start()
66-
fatalOnError(err)
71+
check(err)
6772

6873
dns := confMgr.DNS()
6974
err = dns.Start()
70-
fatalOnError(err)
75+
check(err)
7176

7277
h.services = []agh.Service{
7378
dns,
@@ -103,10 +108,16 @@ func (h *signalHandler) shutdown() (status int) {
103108
}
104109

105110
// newSignalHandler returns a new signalHandler that shuts down svcs.
106-
func newSignalHandler(confFile string, start time.Time, svcs ...agh.Service) (h *signalHandler) {
111+
func newSignalHandler(
112+
confFile string,
113+
frontend fs.FS,
114+
start time.Time,
115+
svcs ...agh.Service,
116+
) (h *signalHandler) {
107117
h = &signalHandler{
108118
signal: make(chan os.Signal, 1),
109119
confFile: confFile,
120+
frontend: frontend,
110121
start: start,
111122
services: svcs,
112123
}

internal/next/configmgr/configmgr.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package configmgr
55
import (
66
"context"
77
"fmt"
8+
"io/fs"
89
"os"
910
"sync"
1011
"time"
@@ -42,7 +43,11 @@ type Manager struct {
4243
// New creates a new *Manager that persists changes to the file pointed to by
4344
// fileName. It reads the configuration file and populates the service fields.
4445
// start is the startup time of AdGuard Home.
45-
func New(fileName string, start time.Time) (m *Manager, err error) {
46+
func New(
47+
fileName string,
48+
frontend fs.FS,
49+
start time.Time,
50+
) (m *Manager, err error) {
4651
defer func() { err = errors.Annotate(err, "reading config") }()
4752

4853
conf := &config{}
@@ -79,7 +84,7 @@ func New(fileName string, start time.Time) (m *Manager, err error) {
7984
ctx, cancel := context.WithTimeout(context.Background(), assemblyTimeout)
8085
defer cancel()
8186

82-
err = m.assemble(ctx, conf, start)
87+
err = m.assemble(ctx, conf, frontend, start)
8388
if err != nil {
8489
// Don't wrap the error, because it's informative enough as is.
8590
return nil, err
@@ -90,7 +95,12 @@ func New(fileName string, start time.Time) (m *Manager, err error) {
9095

9196
// assemble creates all services and puts them into the corresponding fields.
9297
// The fields of conf must not be modified after calling assemble.
93-
func (m *Manager) assemble(ctx context.Context, conf *config, start time.Time) (err error) {
98+
func (m *Manager) assemble(
99+
ctx context.Context,
100+
conf *config,
101+
frontend fs.FS,
102+
start time.Time,
103+
) (err error) {
94104
dnsConf := &dnssvc.Config{
95105
Addresses: conf.DNS.Addresses,
96106
BootstrapServers: conf.DNS.BootstrapDNS,
@@ -104,6 +114,7 @@ func (m *Manager) assemble(ctx context.Context, conf *config, start time.Time) (
104114

105115
webSvcConf := &websvc.Config{
106116
ConfigManager: m,
117+
Frontend: frontend,
107118
// TODO(a.garipov): Fill from config file.
108119
TLS: nil,
109120
Start: start,
@@ -199,7 +210,10 @@ func (m *Manager) updateWeb(ctx context.Context, c *websvc.Config) (err error) {
199210
}
200211
}
201212

202-
m.web = websvc.New(c)
213+
m.web, err = websvc.New(c)
214+
if err != nil {
215+
return fmt.Errorf("creating web svc: %w", err)
216+
}
203217

204218
return nil
205219
}

internal/next/websvc/http.go

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Reque
5353

5454
newConf := &Config{
5555
ConfigManager: svc.confMgr,
56+
Frontend: svc.frontend,
5657
TLS: svc.tls,
5758
Addresses: req.Addresses,
5859
SecureAddresses: req.SecureAddresses,

internal/next/websvc/http_test.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ func TestService_HandlePatchSettingsHTTP(t *testing.T) {
2424
ForceHTTPS: false,
2525
}
2626

27+
svc, err := websvc.New(&websvc.Config{
28+
TLS: &tls.Config{
29+
Certificates: []tls.Certificate{{}},
30+
},
31+
Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:80")},
32+
SecureAddresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:443")},
33+
Timeout: 5 * time.Second,
34+
ForceHTTPS: true,
35+
})
36+
require.NoError(t, err)
37+
2738
confMgr := newConfigManager()
28-
confMgr.onWeb = func() (s agh.ServiceWithConfig[*websvc.Config]) {
29-
return websvc.New(&websvc.Config{
30-
TLS: &tls.Config{
31-
Certificates: []tls.Certificate{{}},
32-
},
33-
Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:80")},
34-
SecureAddresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:443")},
35-
Timeout: 5 * time.Second,
36-
ForceHTTPS: true,
37-
})
38-
}
39-
confMgr.onUpdateWeb = func(ctx context.Context, c *websvc.Config) (err error) {
40-
return nil
41-
}
39+
confMgr.onWeb = func() (s agh.ServiceWithConfig[*websvc.Config]) { return svc }
40+
confMgr.onUpdateWeb = func(ctx context.Context, c *websvc.Config) (err error) { return nil }
4241

4342
_, addr := newTestServer(t, confMgr)
4443
u := &url.URL{
@@ -56,7 +55,7 @@ func TestService_HandlePatchSettingsHTTP(t *testing.T) {
5655

5756
respBody := httpPatch(t, u, req, http.StatusOK)
5857
resp := &websvc.HTTPAPIHTTPSettings{}
59-
err := json.Unmarshal(respBody, resp)
58+
err = json.Unmarshal(respBody, resp)
6059
require.NoError(t, err)
6160

6261
assert.Equal(t, wantWeb, resp)

internal/next/websvc/middleware.go

+17
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package websvc
22

33
import (
44
"net/http"
5+
"time"
56

67
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
78
"github.com/AdguardTeam/golibs/httphdr"
9+
"github.com/AdguardTeam/golibs/log"
810
)
911

1012
// Middlewares
@@ -19,3 +21,18 @@ func jsonMw(h http.Handler) (wrapped http.HandlerFunc) {
1921

2022
return http.HandlerFunc(f)
2123
}
24+
25+
// logMw logs the queries with level debug.
26+
func logMw(h http.Handler) (wrapped http.HandlerFunc) {
27+
f := func(w http.ResponseWriter, r *http.Request) {
28+
start := time.Now()
29+
m, u := r.Method, r.RequestURI
30+
31+
log.Debug("websvc: %s %s started", m, u)
32+
defer func() { log.Debug("websvc: %s %s finished in %s", m, u, time.Since(start)) }()
33+
34+
h.ServeHTTP(w, r)
35+
}
36+
37+
return http.HandlerFunc(f)
38+
}

internal/next/websvc/path.go

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package websvc
22

33
// Path constants
44
const (
5+
PathRoot = "/"
6+
PathFrontend = "/*filepath"
7+
58
PathHealthCheck = "/health-check"
69

710
PathV1SettingsAll = "/api/v1/settings/all"

internal/next/websvc/settings_test.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,19 @@ func TestService_HandleGetSettingsAll(t *testing.T) {
4646
return c
4747
}
4848

49+
svc, err := websvc.New(&websvc.Config{
50+
TLS: &tls.Config{
51+
Certificates: []tls.Certificate{{}},
52+
},
53+
Addresses: wantWeb.Addresses,
54+
SecureAddresses: wantWeb.SecureAddresses,
55+
Timeout: time.Duration(wantWeb.Timeout),
56+
ForceHTTPS: true,
57+
})
58+
require.NoError(t, err)
59+
4960
confMgr.onWeb = func() (s agh.ServiceWithConfig[*websvc.Config]) {
50-
return websvc.New(&websvc.Config{
51-
TLS: &tls.Config{
52-
Certificates: []tls.Certificate{{}},
53-
},
54-
Addresses: wantWeb.Addresses,
55-
SecureAddresses: wantWeb.SecureAddresses,
56-
Timeout: time.Duration(wantWeb.Timeout),
57-
ForceHTTPS: true,
58-
})
61+
return svc
5962
}
6063

6164
_, addr := newTestServer(t, confMgr)
@@ -67,7 +70,7 @@ func TestService_HandleGetSettingsAll(t *testing.T) {
6770

6871
body := httpGet(t, u, http.StatusOK)
6972
resp := &websvc.RespGetV1SettingsAll{}
70-
err := json.Unmarshal(body, resp)
73+
err = json.Unmarshal(body, resp)
7174
require.NoError(t, err)
7275

7376
assert.Equal(t, wantDNS, resp.DNS)

0 commit comments

Comments
 (0)