summaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'config.go')
-rw-r--r--config.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..78bdc31
--- /dev/null
+++ b/config.go
@@ -0,0 +1,90 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "github.com/BurntSushi/toml"
+ "github.com/imdario/mergo"
+ "os"
+ "path/filepath"
+ "sync"
+)
+
+type config struct {
+ Address *string
+ Port *int
+ Debug *bool
+ Quiet *bool
+ CfgFile *string
+ Servers *[]string
+ NoTcp *bool `toml:"no-tcp"`
+ NoHttp *bool `toml:"no-http"`
+ HttpAddress *string `toml:"http-address"`
+ HttpPort *int `toml:"http-port"`
+ UdpTimeout *int `toml:"udp-timeout"`
+ TcpTimeout *int `toml:"tcp-timeout"`
+ sync.RWMutex
+}
+
+// Global
+var cfg config
+
+// Defaults, overwritten by config and then flags
+// TODO pick better nameservers
+var defaultCfg = config{
+ Address: ptrStr("127.0.0.1"),
+ Port: ptrInt(6881),
+ Debug: ptrBool(false),
+ CfgFile: ptrStr("/etc/nomoreads/config"),
+ Servers: &[]string{"8.8.8.8", "8.8.4.4"},
+ Quiet: ptrBool(false),
+ NoTcp: ptrBool(false),
+ NoHttp: ptrBool(false),
+ HttpAddress: ptrStr("localhost"),
+ HttpPort: ptrInt(8080),
+ UdpTimeout: ptrInt(10),
+ TcpTimeout: ptrInt(10),
+}
+
+func loadConfigFlags() config {
+ newCfg := config{}
+ flag.StringVar(&newCfg.Address, "address", defaultCfg.Address, "listen address")
+ flag.IntVar(&newCfg.Port, "port", defaultCfg.BasePort, "listen port")
+ flag.BoolVar(&newCfg.Debug, "debug", defaultCfg.Debug, "provide debug output")
+ flag.BoolVar(&newCfg.Quiet, "quiet", defaultCfg.Quiet, "log only errors")
+ flag.BoolVar(&newCfg.NoTcp, "no-tcp", defaultCfg.NoTcp, "no TCP listener")
+ flag.BoolVar(&newCfg.NoHttp, "no-http", defaultCfg.NoHttp, "no HTTP service")
+ flag.StringVar(&newCfg.HttpAddress, "http-address", defaultCfg.HttpAddress, "HTTP listen address")
+ flag.IntVar(&newCfg.HttpPort, "http-port", defaultCfg.HttpPort, "HTTP listen port")
+ flag.IntVar(&newCfg.UdpTimeout, "udp-timeout", defaultCfg.UdpTimeout, "UDP timeout in seconds")
+ flag.IntVar(&newCfg.TcpTimeout, "tcp-timeout", defaultCfg.TcpTimeout, "TCP timeout in seconds")
+
+ flag.Parse()
+
+ if cfg.Debug {
+ fmt.Printf("Configuration flags read: %v\n", newCfg)
+ }
+ mergo.MergeWitOverwrite(&cfg, newCfg)
+}
+
+func loadConfigFile() config {
+ newCfg := config{}
+ // Read current value of configuration file
+ cfgPath, _ := filepath.Abs(cfg.CfgFile)
+ if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
+ // fmt.Printf("Using configuration from %s\n", cfgPath)
+ md, err := toml.DecodeFile(cfgPath, &newCfg)
+ if err != nil {
+ fmt.Printf("Failed to read configuration: %q\n", err)
+ os.Exit(1)
+ }
+ if len(md.Undecoded()) > 0 {
+ fmt.Printf("Extraneous configuration keys: %q\n", md.Undecoded())
+ }
+ }
+
+ if cfg.Debug {
+ fmt.Printf("Configuration file read: %v\n", newCfg)
+ }
+ mergo.MergeWitOverwrite(&cfg, newCfg)
+}