add multi instance support
This commit is contained in:
51
config.go
51
config.go
@@ -1,29 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/pyke369/golang-support/uconfig"
|
||||
)
|
||||
|
||||
func getPDNSInstances(config *uconfig.UConfig) ([]pdnsInstance, error) {
|
||||
var err error
|
||||
instances := []pdnsInstance{}
|
||||
hasDefaultInstance := false
|
||||
i := pdnsInstance{}
|
||||
for _, profile := range config.GetPaths("config.pdns") {
|
||||
if i.dns, err = NewClient(
|
||||
config.GetString(profile+".api-url", "http://127.0.0.1:8081/api/v1/servers/localhost"),
|
||||
config.GetString(profile+".api-key", ""),
|
||||
int(config.GetInteger(profile+".timeout", 3)),
|
||||
int(config.GetInteger(profile+".defaultTTL", 3600))); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
i.isDefault = config.GetBoolean(profile+".default", false)
|
||||
if i.isDefault && hasDefaultInstance {
|
||||
return nil, errors.New("There can be only one default instance")
|
||||
} else {
|
||||
hasDefaultInstance = true
|
||||
}
|
||||
|
||||
for _, r := range parseConfigArray(config, profile+".whenRegexp") {
|
||||
re, err := regexp.Compile(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
i.regexp = append(i.regexp, re)
|
||||
}
|
||||
instances = append(instances, i)
|
||||
}
|
||||
if len(instances) == 0 {
|
||||
return nil, errors.New("There must be at least one instance")
|
||||
}
|
||||
if len(instances) > 1 && !hasDefaultInstance {
|
||||
return nil, errors.New("There must be at least one default instance")
|
||||
}
|
||||
return instances, nil
|
||||
}
|
||||
|
||||
func loadConfig(configFile string) (*HTTPServer, error) {
|
||||
// Parse the configuration file
|
||||
config, err := uconfig.New(configFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
instances, err := getPDNSInstances(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
h := NewHTTPServer(
|
||||
config.GetString("config.http.port", "127.0.0.01:8080"),
|
||||
config.GetString("config.http.key", ""),
|
||||
config.GetString("config.http.cert", ""),
|
||||
config.GetString("config.http.crl", ""),
|
||||
config.GetString("config.http.ca", ""),
|
||||
config.GetString("config.pdns.api-url", "http://127.0.0.1:8081/api/v1/servers/localhost"),
|
||||
config.GetString("config.pdns.api-key", ""),
|
||||
int(config.GetInteger("config.pdns.timeout", 3)),
|
||||
int(config.GetInteger("config.pdns.defaultTTL", 3600)),
|
||||
instances,
|
||||
)
|
||||
populateHTTPServerZoneProfiles(config, h)
|
||||
populateHTTPServerAcls(config, h)
|
||||
|
||||
Reference in New Issue
Block a user