Redo env parsing
This commit is contained in:
parent
0013c1e655
commit
b5e5fede9a
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
|
|
||||||
Title = "GODNS"
|
Title = "GODNS"
|
||||||
Version = "0.1.2"
|
Version = "0.1.3"
|
||||||
Author = "kenshin"
|
Author = "kenshin"
|
||||||
|
|
||||||
Debug = false
|
Debug = false
|
||||||
|
|
|
@ -48,9 +48,7 @@ func NewResolver(c ResolvSettings) *Resolver {
|
||||||
config: &c,
|
config: &c,
|
||||||
}
|
}
|
||||||
|
|
||||||
if serverList := os.Getenv("SERVER_LIST_FILE"); serverList != "" {
|
if len(c.ServerListFile) > 0 {
|
||||||
r.ReadServerListFile(serverList)
|
|
||||||
} else if len(c.ServerListFile) > 0 {
|
|
||||||
r.ReadServerListFile(c.ServerListFile)
|
r.ReadServerListFile(c.ServerListFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
"os"
|
)
|
||||||
)
|
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
host string
|
host string
|
||||||
|
@ -17,10 +16,6 @@ type Server struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Addr() string {
|
func (s *Server) Addr() string {
|
||||||
if addr := os.Getenv("SERVER_BIND"); addr != "" {
|
|
||||||
return addr
|
|
||||||
}
|
|
||||||
|
|
||||||
return net.JoinHostPort(s.host, strconv.Itoa(s.port))
|
return net.JoinHostPort(s.host, strconv.Itoa(s.port))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
60
settings.go
60
settings.go
|
@ -7,7 +7,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"strings"
|
"reflect"
|
||||||
|
"github.com/caarlos0/env"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -38,32 +39,28 @@ type ResolvSettings struct {
|
||||||
Timeout int
|
Timeout int
|
||||||
Interval int
|
Interval int
|
||||||
SetEDNS0 bool
|
SetEDNS0 bool
|
||||||
ServerListFile string `toml:"server-list-file"`
|
ServerListFile string `toml:"server-list-file" env:"SERVER_LIST_FILE"`
|
||||||
ResolvFile string `toml:"resolv-file"`
|
ResolvFile string `toml:"resolv-file" env:"RESOLV_FILE"`
|
||||||
DOHServer string `toml:"dns-over-https"`
|
DOHServer string `toml:"dns-over-https" env:"DNS_HTTPS_SERVER"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DNSServerSettings struct {
|
type DNSServerSettings struct {
|
||||||
Host string
|
Host string `toml:"host" env:"SERVER_BIND"`
|
||||||
Port int
|
Port int `toml:"port" env:"SERVER_PORT"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RedisSettings struct {
|
type RedisSettings struct {
|
||||||
Host string
|
Host string `toml:"host" env:"REDIS_HOST"`
|
||||||
Port int
|
Port int `toml:"port" env:"REDIS_PORT"`
|
||||||
DB int
|
DB int `toml:"db" env:"REDIS_DB"`
|
||||||
Password string
|
Password string `toml:"password" env:"REDIS_PASSWORD"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MemcacheSettings struct {
|
type MemcacheSettings struct {
|
||||||
Servers []string
|
Servers []string `toml:"servers" env:"MEMCACHE_SERVERS"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s RedisSettings) Addr() string {
|
func (s RedisSettings) Addr() string {
|
||||||
if addr := os.Getenv("REDIS_ADDR"); addr != "" {
|
|
||||||
return addr
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.Host + ":" + strconv.Itoa(s.Port)
|
return s.Host + ":" + strconv.Itoa(s.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,18 +79,18 @@ func (ls LogSettings) LogLevel() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
type CacheSettings struct {
|
type CacheSettings struct {
|
||||||
Backend string
|
Backend string `toml:"backend" env:"CACHE_BACKEND"`
|
||||||
Expire int
|
Expire int `toml:"expire" env:"CACHE_EXPIRE"`
|
||||||
Maxcount int
|
Maxcount int `toml:"maxcount" env:"CACHE_MAX_COUNT"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type HostsSettings struct {
|
type HostsSettings struct {
|
||||||
Enable bool
|
Enable bool `toml:"enable" env:"HOSTS_ENABLE"`
|
||||||
HostsFile string `toml:"host-file"`
|
HostsFile string `toml:"host-file" env:"HOSTS_FILE"`
|
||||||
RedisEnable bool `toml:"redis-enable"`
|
RedisEnable bool `toml:"redis-enable" env:"REDIS_HOSTS_ENABLE"`
|
||||||
RedisKey string `toml:"redis-key"`
|
RedisKey string `toml:"redis-key" env:"REDIS_HOSTS_KEY"`
|
||||||
TTL uint32 `toml:"ttl"`
|
TTL uint32 `toml:"ttl" env:"HOSTS_TTL"`
|
||||||
RefreshInterval uint32 `toml:"refresh-interval"`
|
RefreshInterval uint32 `toml:"refresh-interval" env:"HOSTS_REFRESH_INTERVAL"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -108,12 +105,17 @@ func init() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Env overrides
|
val := reflect.ValueOf(settings)
|
||||||
if cacheBackend := os.Getenv("CACHE_BACKEND"); cacheBackend != "" {
|
|
||||||
settings.Cache.Backend = cacheBackend
|
for i := 0; i < val.NumField(); i++ {
|
||||||
|
f := val.Field(i)
|
||||||
|
|
||||||
|
if f.Kind() != reflect.Struct {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if memcacheServers := os.Getenv("MEMCACHE_SERVERS"); memcacheServers != "" {
|
v := f.Interface()
|
||||||
settings.Memcache.Servers = strings.Split(memcacheServers, ",")
|
|
||||||
|
env.Parse(&v)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue