forked from cloud66-oss/coredns_mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
152 lines (135 loc) · 2.94 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package coredns_mysql
import (
"database/sql"
"os"
"strconv"
"time"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
)
const (
defaultTtl = 360
defaultMaxLifeTime = 1 * time.Minute
defaultMaxOpenConnections = 10
defaultMaxIdleConnections = 10
defaultZoneUpdateTime = 10 * time.Minute
)
func init() {
caddy.RegisterPlugin("mysql", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
r, err := mysqlParse(c)
if err != nil {
return plugin.Error("mysql", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
r.Next = next
return r
})
return nil
}
func mysqlParse(c *caddy.Controller) (*CoreDNSMySql, error) {
mysql := CoreDNSMySql{
TableName: "records",
Ttl: 300,
}
var err error
c.Next()
if c.NextBlock() {
for {
switch c.Val() {
case "dsn":
if !c.NextArg() {
return &CoreDNSMySql{}, c.ArgErr()
}
mysql.Dsn = c.Val()
case "table":
if !c.NextArg() {
return &CoreDNSMySql{}, c.ArgErr()
}
mysql.TableName = c.Val()
case "max_lifetime":
if !c.NextArg() {
return &CoreDNSMySql{}, c.ArgErr()
}
var val time.Duration
val, err = time.ParseDuration(c.Val())
if err != nil {
val = defaultMaxLifeTime
}
mysql.MaxLifetime = val
case "max_open_connections":
if !c.NextArg() {
return &CoreDNSMySql{}, c.ArgErr()
}
var val int
val, err = strconv.Atoi(c.Val())
if err != nil {
val = defaultMaxOpenConnections
}
mysql.MaxOpenConnections = val
case "max_idle_connections":
if !c.NextArg() {
return &CoreDNSMySql{}, c.ArgErr()
}
var val int
val, err = strconv.Atoi(c.Val())
if err != nil {
val = defaultMaxIdleConnections
}
mysql.MaxIdleConnections = val
case "zone_update_interval":
if !c.NextArg() {
return &CoreDNSMySql{}, c.ArgErr()
}
var val time.Duration
val, err = time.ParseDuration(c.Val())
if err != nil {
val = defaultZoneUpdateTime
}
mysql.zoneUpdateTime = val
case "ttl":
if !c.NextArg() {
return &CoreDNSMySql{}, c.ArgErr()
}
var val int
val, err = strconv.Atoi(c.Val())
if err != nil {
val = defaultTtl
}
mysql.Ttl = uint32(val)
default:
if c.Val() != "}" {
return &CoreDNSMySql{}, c.Errf("unknown property '%s'", c.Val())
}
}
if !c.Next() {
break
}
}
}
db, err := mysql.db()
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
defer db.Close()
return &mysql, nil
}
func (handler *CoreDNSMySql) db() (*sql.DB, error) {
db, err := sql.Open("mysql", os.ExpandEnv(handler.Dsn))
if err != nil {
return nil, err
}
db.SetConnMaxLifetime(handler.MaxLifetime)
db.SetMaxOpenConns(handler.MaxOpenConnections)
db.SetMaxIdleConns(handler.MaxIdleConnections)
return db, nil
}