forked from dougbw/coredns_omada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
64 lines (51 loc) · 1.24 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package coredns_omada
import (
"context"
"fmt"
"time"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
var log = clog.NewWithPlugin("omada")
func init() { plugin.Register("omada", setup) }
func setup(c *caddy.Controller) error {
config, err := parse(c)
if err != nil {
return plugin.Error("omada", err)
}
ctx, cancel := context.WithCancel(context.Background())
url := config.Controller_url
u := config.Username
p := config.Password
o, err := NewOmada(ctx, url, u, p)
if err != nil {
cancel()
return plugin.Error("omada", err)
}
o.config = config
// initial login
if err := o.login(ctx); err != nil {
cancel()
return plugin.Error("omada", err)
}
// initial zone update
if err := o.updateZones(ctx); err != nil {
cancel()
return plugin.Error("omada", err)
}
delay := 5 * time.Minute
fmt.Printf("delay: %d\n", delay)
// start update loop
if err := o.updateLoop(ctx); err != nil {
cancel()
return plugin.Error("omada", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
o.Next = next
return o
})
c.OnShutdown(func() error { cancel(); return nil })
return nil
}