forked from dabdns/powerdns-remote-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerdns_remote_backend.go
74 lines (67 loc) · 1.96 KB
/
powerdns_remote_backend.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
package main
import (
"fmt"
"github.com/dabdns/powerdns-remote-backend/pkg/backend"
"github.com/dabdns/powerdns-remote-backend/pkg/config"
connectorBase "github.com/dabdns/powerdns-remote-backend/pkg/connector"
connectorHTTP "github.com/dabdns/powerdns-remote-backend/pkg/connector/http"
connectorPipe "github.com/dabdns/powerdns-remote-backend/pkg/connector/pipe"
connectorSocket "github.com/dabdns/powerdns-remote-backend/pkg/connector/socket"
"github.com/spf13/viper"
"os"
)
func loadConfig() (conf config.Config, err error) {
conf = config.GetDefaultConfig()
var vConf config.Config
v := viper.New()
// load configuration with viper
v.AddConfigPath(".")
v.AddConfigPath("/etc/dabdns")
v.SetConfigName("dabdns")
v.SetEnvPrefix("dabdns")
v.AutomaticEnv()
defaultsMap := config.GetDefaultConfigMap()
err = v.MergeConfigMap(defaultsMap)
err = v.MergeInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
panic(fmt.Errorf("error reading config file, %s", err))
}
}
err = v.Unmarshal(&vConf)
if err != nil {
panic(fmt.Errorf("unable to decode into struct, %v", err))
}
conf.Merge(&vConf)
return
}
func main() {
conf, err := loadConfig()
delegate := backend.NewDelegate()
for _, delegateConfig := range conf.Delegates {
var delegateBase *backend.DelegateBase
delegateBase, err = backend.NewDelagateBase(delegateConfig)
if err == nil {
delegate.AddDelegate(delegateBase)
} else {
panic(err)
}
}
var connector connectorBase.Connector
switch *conf.Connector.Type {
case "http":
connector = connectorHTTP.NewConnectorHTTP(delegate, *conf.Connector.Host, *conf.Connector.Port)
case "pipe":
connector = connectorPipe.NewConnectorPipe(delegate)
case "socket":
connector = connectorSocket.NewConnectorSocket(delegate, *conf.Connector.Network, *conf.Connector.Address, *conf.Connector.Timeout)
default:
}
err = connector.Config()
if err == nil {
err = connector.Start()
}
if err != nil {
os.Exit(1)
}
}