Debian Codegraph @

cmd/atomdns/internal/dnsserver/controller.go

raw
1 package dnsserver
2
3 import (
4 "path/filepath"
5
6 "codeberg.org/miekg/dns/cmd/atomdns/handlers/global"
7 "codeberg.org/miekg/dns/cmd/atomdns/internal/conffile"
8 )
9
10 //go:generate go run string_generate.go
11
12 // Controller is used by handlers to parse their config.
13 type Controller struct {
14 conffile.Dispenser
15 Global *global.Global
16 }
17
18 // OnStartup and OnShutdown can be used by handlers to register startup and shutdown functions. Each function
19 // is execute once during starting and shutting of the server. OnReset is called after OnShutdown to clear
20 // anything out that can prevent a clean reload, think [sync.Once] mutexes.
21 func (c *Controller) OnStartup(fn func() error) { c.Global.OnStartup(fn) }
22 func (c *Controller) OnShutdown(fn func() error) { c.Global.OnShutdown(fn) }
23 func (c *Controller) OnReset(fn func()) { c.Global.OnReset(fn) }
24
25 // NewTestController create a controller useful for tests.
26 func NewTestController(input string) *Controller {
27 d := conffile.NewTestDispenser(input)
28 return &Controller{Dispenser: d, Global: &global.Global{}}
29 }
30
31 func (c *Controller) Path() string {
32 p := c.Val()
33 p = conffile.Tilde(p)
34 if filepath.IsAbs(p) {
35 return p
36 }
37 return filepath.Join(c.Global.Root, p)
38 }
39
40 func (c *Controller) RemainingPaths() []string {
41 args := c.RemainingArgs()
42 if len(args) == 0 {
43 return nil
44 }
45
46 paths := make([]string, len(args))
47 for i, arg := range args {
48 paths[i] = conffile.Tilde(arg)
49
50 if filepath.IsAbs(paths[i]) {
51 continue
52 }
53 paths[i] = filepath.Join(c.Global.Root, paths[i])
54 }
55 return paths
56 }