summary refs log tree commit diff stats
path: root/svc/conf.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-10 01:19:54 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-10 01:20:41 -0400
commit4fb7785e45528a69dffb94d4c164efeab3a645d1 (patch)
tree13bc055f504aae7e347e0d7cd8bca824f1545ad6 /svc/conf.go
parent4648146ad31398aef3f184145a0c9781d31c30cb (diff)
downloadgetwtxt-4fb7785e45528a69dffb94d4c164efeab3a645d1.tar.gz
refactored initConfig()
Diffstat (limited to 'svc/conf.go')
-rw-r--r--svc/conf.go111
1 files changed, 42 insertions, 69 deletions
diff --git a/svc/conf.go b/svc/conf.go
index f4d6518..0d9e739 100644
--- a/svc/conf.go
+++ b/svc/conf.go
@@ -21,7 +21,6 @@ type Configuration struct {
 	DBPath        string        `yaml:"DatabasePath"`
 	AssetsDir     string        `yaml:"-"`
 	StdoutLogging bool          `yaml:"StdoutLogging"`
-	Version       string        `yaml:"-"`
 	CacheInterval time.Duration `yaml:"StatusFetchInterval"`
 	DBInterval    time.Duration `yaml:"DatabasePushInterval"`
 	LastCache     time.Time     `yaml:"-"`
@@ -39,6 +38,29 @@ type Instance struct {
 	Desc  string `yaml:"Instance.Description"`
 }
 
+func initConfig() {
+
+	parseConfigFlag()
+
+	setConfigDefaults()
+
+	log.Printf("Loading configuration ...\n")
+	if err := viper.ReadInConfig(); err != nil {
+		log.Printf("%v\n", err.Error())
+		log.Printf("Using defaults ...\n")
+		bindConfig()
+		return
+	}
+
+	viper.WatchConfig()
+	viper.OnConfigChange(func(e fsnotify.Event) {
+		log.Printf("Config file change detected. Reloading...\n")
+		bindConfig()
+		initLogging()
+	})
+	bindConfig()
+}
+
 func initLogging() {
 
 	confObj.Mu.RLock()
@@ -70,8 +92,24 @@ func initLogging() {
 	confObj.Mu.RUnlock()
 }
 
-func initConfig() {
+func setConfigDefaults() {
+	viper.SetDefault("ListenPort", 9001)
+	viper.SetDefault("LogFile", "getwtxt.log")
+	viper.SetDefault("DatabasePath", "getwtxt.db")
+	viper.SetDefault("AssetsDirectory", "assets")
+	viper.SetDefault("DatabaseType", "leveldb")
+	viper.SetDefault("StdoutLogging", false)
+	viper.SetDefault("ReCacheInterval", "1h")
+	viper.SetDefault("DatabasePushInterval", "5m")
 
+	viper.SetDefault("Instance.SiteName", "getwtxt")
+	viper.SetDefault("Instance.OwnerName", "Anonymous Microblogger")
+	viper.SetDefault("Instance.Email", "nobody@knows")
+	viper.SetDefault("Instance.URL", "https://twtxt.example.com")
+	viper.SetDefault("Instance.Description", "A fast, resilient twtxt registry server written in Go!")
+}
+
+func parseConfigFlag() {
 	if *flagConfFile == "" {
 		viper.SetConfigName("getwtxt")
 		viper.SetConfigType("yml")
@@ -85,42 +123,14 @@ func initConfig() {
 		if path == "" {
 			path = "."
 		}
-		if file == "" {
-			file = *flagConfFile
-		}
 		filename := strings.Split(file, ".")
 		viper.SetConfigName(filename[0])
 		viper.SetConfigType(filename[1])
 		viper.AddConfigPath(path)
 	}
+}
 
-	log.Printf("Loading configuration ...\n")
-	if err := viper.ReadInConfig(); err != nil {
-		log.Printf("%v\n", err.Error())
-		log.Printf("Using defaults ...\n")
-	} else {
-		viper.WatchConfig()
-		viper.OnConfigChange(func(e fsnotify.Event) {
-			log.Printf("Config file change detected. Reloading...\n")
-			rebindConfig()
-		})
-	}
-
-	viper.SetDefault("ListenPort", 9001)
-	viper.SetDefault("LogFile", "getwtxt.log")
-	viper.SetDefault("DatabasePath", "getwtxt.db")
-	viper.SetDefault("AssetsDirectory", "assets")
-	viper.SetDefault("DatabaseType", "leveldb")
-	viper.SetDefault("StdoutLogging", false)
-	viper.SetDefault("ReCacheInterval", "1h")
-	viper.SetDefault("DatabasePushInterval", "5m")
-
-	viper.SetDefault("Instance.SiteName", "getwtxt")
-	viper.SetDefault("Instance.OwnerName", "Anonymous Microblogger")
-	viper.SetDefault("Instance.Email", "nobody@knows")
-	viper.SetDefault("Instance.URL", "https://twtxt.example.com")
-	viper.SetDefault("Instance.Description", "A fast, resilient twtxt registry server written in Go!")
-
+func bindConfig() {
 	confObj.Mu.Lock()
 
 	confObj.Port = viper.GetInt("ListenPort")
@@ -157,7 +167,6 @@ func initConfig() {
 
 	confObj.LastCache = time.Now()
 	confObj.LastPush = time.Now()
-	confObj.Version = Vers
 
 	confObj.Instance.Vers = Vers
 	confObj.Instance.Name = viper.GetString("Instance.SiteName")
@@ -167,40 +176,4 @@ func initConfig() {
 	confObj.Instance.Desc = viper.GetString("Instance.Description")
 
 	confObj.Mu.Unlock()
-
-}
-
-func rebindConfig() {
-
-	confObj.Mu.RLock()
-	if !confObj.StdoutLogging {
-		closeLog <- true
-	}
-	confObj.Mu.RUnlock()
-
-	confObj.Mu.Lock()
-
-	confObj.DBType = strings.ToLower(viper.GetString("DatabaseType"))
-	if *flagDBType != "" {
-		confObj.DBType = *flagDBType
-	}
-
-	confObj.LogFile = viper.GetString("LogFile")
-	confObj.DBPath = viper.GetString("DatabasePath")
-	if *flagDBPath != "" {
-		confObj.DBPath = *flagDBPath
-	}
-	confObj.StdoutLogging = viper.GetBool("StdoutLogging")
-	confObj.CacheInterval = viper.GetDuration("StatusFetchInterval")
-	confObj.DBInterval = viper.GetDuration("DatabasePushInterval")
-
-	confObj.Instance.Name = viper.GetString("Instance.SiteName")
-	confObj.Instance.URL = viper.GetString("Instance.URL")
-	confObj.Instance.Owner = viper.GetString("Instance.OwnerName")
-	confObj.Instance.Mail = viper.GetString("Instance.Email")
-	confObj.Instance.Desc = viper.GetString("Instance.Description")
-
-	confObj.Mu.Unlock()
-
-	initLogging()
 }