Redo env parsing
This commit is contained in:
parent
0013c1e655
commit
b5e5fede9a
|
@ -2,7 +2,7 @@
|
|||
|
||||
|
||||
Title = "GODNS"
|
||||
Version = "0.1.2"
|
||||
Version = "0.1.3"
|
||||
Author = "kenshin"
|
||||
|
||||
Debug = false
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
64
settings.go
64
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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue