Redo settings a bit, move a lot of init logic to main.go
This commit is contained in:
		@ -1,18 +1,8 @@
 | 
			
		||||
package settings
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"flag"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"meow.tf/joker/godns/log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strconv"
 | 
			
		||||
 | 
			
		||||
	"github.com/BurntSushi/toml"
 | 
			
		||||
	"github.com/caarlos0/env"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	settings Settings
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var LogLevelMap = map[string]int{
 | 
			
		||||
@ -24,7 +14,6 @@ var LogLevelMap = map[string]int{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HostsSettings struct {
 | 
			
		||||
	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"`
 | 
			
		||||
@ -48,9 +37,8 @@ type ResolvSettings struct {
 | 
			
		||||
	Timeout        int `toml:"timeout" env:"RESOLV_TIMEOUT"`
 | 
			
		||||
	Interval       int `toml:"interval" env:"RESOLV_INTERVAL"`
 | 
			
		||||
	SetEDNS0       bool `toml:"setedns0" env:"RESOLV_EDNS0"`
 | 
			
		||||
	ServerListFile string `toml:"server-list-file" env:"SERVER_LIST_FILE"`
 | 
			
		||||
	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 {
 | 
			
		||||
@ -79,10 +67,10 @@ type LogSettings struct {
 | 
			
		||||
	Level  string `toml:"level" env:"LOG_LEVEL"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (ls LogSettings) LogLevel() int {
 | 
			
		||||
	l, ok := LogLevelMap[ls.Level]
 | 
			
		||||
func LogLevelFor(level string) int {
 | 
			
		||||
	l, ok := LogLevelMap[level]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		panic("Config error: invalid log level: " + ls.Level)
 | 
			
		||||
		panic("Config error: invalid log level: " + level)
 | 
			
		||||
	}
 | 
			
		||||
	return l
 | 
			
		||||
}
 | 
			
		||||
@ -91,56 +79,4 @@ type CacheSettings struct {
 | 
			
		||||
	Backend  string `toml:"backend" env:"CACHE_BACKEND"`
 | 
			
		||||
	Expire   int `toml:"expire" env:"CACHE_EXPIRE"`
 | 
			
		||||
	Maxcount int `toml:"maxcount" env:"CACHE_MAX_COUNT"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	var configFile string
 | 
			
		||||
 | 
			
		||||
	flag.StringVar(&configFile, "c", "/etc/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)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	env.Parse(&settings.ResolvConfig)
 | 
			
		||||
	env.Parse(&settings.Redis)
 | 
			
		||||
	env.Parse(&settings.Memcache)
 | 
			
		||||
	env.Parse(&settings.Log)
 | 
			
		||||
	env.Parse(&settings.Cache)
 | 
			
		||||
	env.Parse(&settings.Hosts)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Resolver() ResolvSettings {
 | 
			
		||||
	return settings.ResolvConfig
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Cache() CacheSettings {
 | 
			
		||||
	return settings.Cache
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Server() DNSServerSettings {
 | 
			
		||||
	return settings.Server
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Hosts() HostsSettings {
 | 
			
		||||
	return settings.Hosts
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Redis() RedisSettings {
 | 
			
		||||
	return settings.Redis
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Memcache() MemcacheSettings {
 | 
			
		||||
	return settings.Memcache
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Debug() bool {
 | 
			
		||||
	return settings.Debug
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Log() LogSettings {
 | 
			
		||||
	return settings.Log
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user