godns/settings.go

106 lines
1.9 KiB
Go
Raw Normal View History

2013-07-23 11:10:38 +00:00
package main
import (
"flag"
"fmt"
"os"
2013-07-26 10:54:19 +00:00
"strconv"
"github.com/BurntSushi/toml"
2013-07-23 11:10:38 +00:00
)
var (
settings Settings
)
var LogLevelMap = map[string]int{
"DEBUG": LevelDebug,
"INFO": LevelInfo,
"NOTICE": LevelNotice,
"WARN": LevelWarn,
"ERROR": LevelError,
}
2013-07-23 11:10:38 +00:00
type Settings struct {
Version string
Debug bool
Server DNSServerSettings `toml:"server"`
ResolvConfig ResolvSettings `toml:"resolv"`
Redis RedisSettings `toml:"redis"`
2016-02-12 13:29:25 +00:00
Memcache MemcacheSettings `toml:"memcache"`
2013-07-23 11:10:38 +00:00
Log LogSettings `toml:"log"`
Cache CacheSettings `toml:"cache"`
2013-07-26 04:06:16 +00:00
Hosts HostsSettings `toml:"hosts"`
2013-07-23 11:10:38 +00:00
}
type ResolvSettings struct {
ResolvFile string `toml:"resolv-file"`
Timeout int
Interval int
SetEDNS0 bool
2013-07-23 11:10:38 +00:00
}
type DNSServerSettings struct {
Host string
Port int
}
type RedisSettings struct {
Host string
Port int
DB int
Password string
}
2016-02-12 13:29:25 +00:00
type MemcacheSettings struct {
Servers []string
}
2013-07-26 10:54:19 +00:00
func (s RedisSettings) Addr() string {
return s.Host + ":" + strconv.Itoa(s.Port)
}
2013-07-23 11:10:38 +00:00
type LogSettings struct {
Stdout bool
File string
Level string
}
func (ls LogSettings) LogLevel() int {
l, ok := LogLevelMap[ls.Level]
if !ok {
panic("Config error: invalid log level: " + ls.Level)
}
return l
2013-07-23 11:10:38 +00:00
}
type CacheSettings struct {
Backend string
2013-07-24 16:09:07 +00:00
Expire int
2013-07-24 10:29:38 +00:00
Maxcount int
2013-07-23 11:10:38 +00:00
}
2013-07-26 04:06:16 +00:00
type HostsSettings struct {
Enable bool
HostsFile string `toml:"host-file"`
RedisEnable bool `toml:"redis-enable"`
RedisKey string `toml:"redis-key"`
TTL uint32 `toml:"ttl"`
RefreshInterval uint32 `toml:"refresh-interval"`
2013-07-26 04:06:16 +00:00
}
2013-07-23 11:10:38 +00:00
func init() {
var configFile string
flag.StringVar(&configFile, "c", "godns.conf", "Look for godns toml-formatting config file in this directory")
flag.Parse()
if _, err := toml.DecodeFile(configFile, &settings); err != nil {
fmt.Printf("%s is not a valid toml config file\n", configFile)
fmt.Println(err)
os.Exit(1)
}
}