From b5e5fede9af24774d4ab709f6ab72d907dbd3461 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sat, 4 Aug 2018 23:53:11 -0400 Subject: [PATCH] Redo env parsing --- etc/godns.conf | 2 +- resolver.go | 4 +--- server.go | 7 +----- settings.go | 64 ++++++++++++++++++++++++++------------------------ 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/etc/godns.conf b/etc/godns.conf index e3174b0..7455b4c 100644 --- a/etc/godns.conf +++ b/etc/godns.conf @@ -2,7 +2,7 @@ Title = "GODNS" -Version = "0.1.2" +Version = "0.1.3" Author = "kenshin" Debug = false diff --git a/resolver.go b/resolver.go index 4cb41ab..1fbace8 100644 --- a/resolver.go +++ b/resolver.go @@ -48,9 +48,7 @@ func NewResolver(c ResolvSettings) *Resolver { config: &c, } - if serverList := os.Getenv("SERVER_LIST_FILE"); serverList != "" { - r.ReadServerListFile(serverList) - } else if len(c.ServerListFile) > 0 { + if len(c.ServerListFile) > 0 { r.ReadServerListFile(c.ServerListFile) } diff --git a/server.go b/server.go index 18f54a2..8602a16 100644 --- a/server.go +++ b/server.go @@ -6,8 +6,7 @@ import ( "time" "github.com/miekg/dns" - "os" -) + ) type Server struct { host string @@ -17,10 +16,6 @@ type Server struct { } func (s *Server) Addr() string { - if addr := os.Getenv("SERVER_BIND"); addr != "" { - return addr - } - return net.JoinHostPort(s.host, strconv.Itoa(s.port)) } diff --git a/settings.go b/settings.go index dc5a673..9a462e8 100644 --- a/settings.go +++ b/settings.go @@ -7,7 +7,8 @@ import ( "strconv" "github.com/BurntSushi/toml" - "strings" + "reflect" + "github.com/caarlos0/env" ) var ( @@ -38,32 +39,28 @@ type ResolvSettings struct { Timeout int Interval int SetEDNS0 bool - ServerListFile string `toml:"server-list-file"` - ResolvFile string `toml:"resolv-file"` - DOHServer string `toml:"dns-over-https"` + ServerListFile string `toml:"server-list-file" env:"SERVER_LIST_FILE"` + ResolvFile string `toml:"resolv-file" env:"RESOLV_FILE"` + DOHServer string `toml:"dns-over-https" env:"DNS_HTTPS_SERVER"` } type DNSServerSettings struct { - Host string - Port int + Host string `toml:"host" env:"SERVER_BIND"` + Port int `toml:"port" env:"SERVER_PORT"` } type RedisSettings struct { - Host string - Port int - DB int - Password string + Host string `toml:"host" env:"REDIS_HOST"` + Port int `toml:"port" env:"REDIS_PORT"` + DB int `toml:"db" env:"REDIS_DB"` + Password string `toml:"password" env:"REDIS_PASSWORD"` } type MemcacheSettings struct { - Servers []string + Servers []string `toml:"servers" env:"MEMCACHE_SERVERS"` } func (s RedisSettings) Addr() string { - if addr := os.Getenv("REDIS_ADDR"); addr != "" { - return addr - } - return s.Host + ":" + strconv.Itoa(s.Port) } @@ -82,18 +79,18 @@ func (ls LogSettings) LogLevel() int { } type CacheSettings struct { - Backend string - Expire int - Maxcount int + Backend string `toml:"backend" env:"CACHE_BACKEND"` + Expire int `toml:"expire" env:"CACHE_EXPIRE"` + Maxcount int `toml:"maxcount" env:"CACHE_MAX_COUNT"` } 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"` + Enable bool `toml:"enable" env:"HOSTS_ENABLE"` + HostsFile string `toml:"host-file" env:"HOSTS_FILE"` + RedisEnable bool `toml:"redis-enable" env:"REDIS_HOSTS_ENABLE"` + RedisKey string `toml:"redis-key" env:"REDIS_HOSTS_KEY"` + TTL uint32 `toml:"ttl" env:"HOSTS_TTL"` + RefreshInterval uint32 `toml:"refresh-interval" env:"HOSTS_REFRESH_INTERVAL"` } func init() { @@ -108,12 +105,17 @@ func init() { os.Exit(1) } - // Env overrides - if cacheBackend := os.Getenv("CACHE_BACKEND"); cacheBackend != "" { - settings.Cache.Backend = cacheBackend - } + val := reflect.ValueOf(settings) - if memcacheServers := os.Getenv("MEMCACHE_SERVERS"); memcacheServers != "" { - settings.Memcache.Servers = strings.Split(memcacheServers, ",") + for i := 0; i < val.NumField(); i++ { + f := val.Field(i) + + if f.Kind() != reflect.Struct { + continue + } + + v := f.Interface() + + env.Parse(&v) } -} +} \ No newline at end of file