| 1 |
package dbhost |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"path/filepath" |
| 6 |
"strconv" |
| 7 |
|
| 8 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnsserver" |
| 9 |
) |
| 10 |
|
| 11 |
func (d *Dbhost) Setup(co *dnsserver.Controller) error { |
| 12 |
d.Path, d.ttl = "/etc/hosts", 3600 |
| 13 |
d.ctx, d.cancel = context.WithCancel(context.Background()) |
| 14 |
for co.Next() { |
| 15 |
paths := co.RemainingPaths() |
| 16 |
if len(paths) > 1 { |
| 17 |
return co.ArgErr() |
| 18 |
} |
| 19 |
if len(paths) == 1 { |
| 20 |
d.Path = paths[0] |
| 21 |
} |
| 22 |
|
| 23 |
if co.NextBlock(0) { |
| 24 |
switch co.Val() { |
| 25 |
case "ttl": |
| 26 |
args := co.RemainingArgs() |
| 27 |
if len(args) == 0 { |
| 28 |
return co.PropEmptyErr("ttl") |
| 29 |
} |
| 30 |
ttl, err := strconv.ParseUint(args[0], 10, 32) |
| 31 |
if err != nil { |
| 32 |
return co.PropErr(err) |
| 33 |
} |
| 34 |
d.ttl = uint32(ttl) |
| 35 |
|
| 36 |
default: |
| 37 |
return co.PropErr() |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
co.OnStartup(func() error { |
| 43 |
log().Info("Startup", "reload", filepath.Base(d.Path)) |
| 44 |
if err := d.Load(); err != nil { |
| 45 |
return d.Err(err) |
| 46 |
} |
| 47 |
return d.Reload() |
| 48 |
}) |
| 49 |
|
| 50 |
co.OnShutdown(func() error { |
| 51 |
log().Info("Shutdown", "reload", filepath.Base(d.Path)) |
| 52 |
d.cancel() |
| 53 |
return nil |
| 54 |
}) |
| 55 |
|
| 56 |
return nil |
| 57 |
} |