about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cache.go16
-rw-r--r--getwtxt.json24
-rw-r--r--handlers.go4
-rw-r--r--init.go145
-rw-r--r--init_test.go2
-rw-r--r--main.go6
-rw-r--r--types.go36
7 files changed, 101 insertions, 132 deletions
diff --git a/cache.go b/cache.go
index fedc29d..2ed21f3 100644
--- a/cache.go
+++ b/cache.go
@@ -13,13 +13,13 @@ import (
 // Checks whether it's time to refresh
 // the cache.
 func checkCacheTime() bool {
-	return time.Since(confObj.lastCache) > confObj.cacheInterval
+	return time.Since(confObj.LastCache) > confObj.CacheInterval
 }
 
 // Checks whether it's time to push
 // the cache to the database
 func checkDBtime() bool {
-	return time.Since(confObj.lastPush) > confObj.dbInterval
+	return time.Since(confObj.LastPush) > confObj.DBInterval
 }
 
 // Launched by init as a goroutine to constantly watch
@@ -59,9 +59,9 @@ func refreshCache() {
 			log.Printf("Error while refreshing local copy of remote registry user data: %v\n", err)
 		}
 	}
-	confObj.mu.Lock()
-	confObj.lastCache = time.Now()
-	confObj.mu.Unlock()
+	confObj.Mu.Lock()
+	confObj.LastCache = time.Now()
+	confObj.Mu.Unlock()
 }
 
 // Pushes the registry's cache data to a local
@@ -104,9 +104,9 @@ func pushDatabase() error {
 	// Update the last push time for
 	// our timer/watch function to
 	// reference.
-	confObj.mu.Lock()
-	confObj.lastPush = time.Now()
-	confObj.mu.Unlock()
+	confObj.Mu.Lock()
+	confObj.LastPush = time.Now()
+	confObj.Mu.Unlock()
 
 	return nil
 }
diff --git a/getwtxt.json b/getwtxt.json
index 912975a..b739d8c 100644
--- a/getwtxt.json
+++ b/getwtxt.json
@@ -1,15 +1,15 @@
 {
-  "port": 9001,
-  "stdoutLogging": true,
-  "logFile": "getwtxt.log",
-  "databasePath": "getwtxt.db",
-  "databasePushInterval": "5m",
-  "reCacheInterval": "1h",
-  "instance": {
-    "name": "getwtxt",
-    "url": "https://twtxt.example.com",
-    "owner": "foo barrington",
-    "mail": "foo@barrington.ext",
-    "description": "This is the twtxt registry for the tildeverse network of public unix servers."
+  "ListenPort": 9001,
+  "StdoutLogging": true,
+  "LogFile": "getwtxt.log",
+  "DatabasePath": "getwtxt.db",
+  "DatabasePushInterval": "5m",
+  "StatusFetchInterval": "1h",
+  "Instance": {
+    "SiteName": "getwtxt",
+    "URL": "https://twtxt.example.com",
+    "OwnerName": "foo barrington",
+    "Email": "foo@barrington.ext",
+    "Description": "This is the twtxt registry for the tildeverse network of public unix servers."
   }
 }
diff --git a/handlers.go b/handlers.go
index 9dc7192..7f05f8f 100644
--- a/handlers.go
+++ b/handlers.go
@@ -31,9 +31,9 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
 
 	// Pass the confObj.Instance data to the template,
 	// then send it to the client.
-	confObj.mu.RLock()
+	confObj.Mu.RLock()
 	err := tmpls.ExecuteTemplate(w, "index.html", confObj.Instance)
-	confObj.mu.RUnlock()
+	confObj.Mu.RUnlock()
 	if err != nil {
 		log500(w, r, err)
 		return
diff --git a/init.go b/init.go
index a6b2aa1..8e7fe2f 100644
--- a/init.go
+++ b/init.go
@@ -24,7 +24,7 @@ var (
 )
 
 // config object
-var confObj = &configuration{}
+var confObj = &Configuration{}
 
 // signals to close the log file
 var closeLog = make(chan bool, 1)
@@ -68,6 +68,7 @@ func checkFlags() {
 func initConfig() {
 
 	viper.SetConfigName("getwtxt")
+	viper.SetConfigType("json")
 	viper.AddConfigPath(".")
 	viper.AddConfigPath("/usr/local/getwtxt")
 	viper.AddConfigPath("/etc")
@@ -87,66 +88,51 @@ func initConfig() {
 		rebindConfig()
 	})
 
-	viper.SetDefault("port", 9001)
-	viper.SetDefault("logFile", "getwtxt.log")
-	viper.SetDefault("databasePath", "getwtxt.db")
-	viper.SetDefault("stdoutLogging", false)
-	viper.SetDefault("reCacheInterval", "1h")
-	viper.SetDefault("databasePushInterval", "5m")
-
-	updateInterval := viper.GetString("reCacheInterval")
-	dur, err := time.ParseDuration(updateInterval)
-	if err != nil {
-		log.Printf("Unable to parse registry cache update interval. Defaulting to hourly. Msg: %v\n", err)
-		dur, _ = time.ParseDuration("1h")
-	}
-
-	dbPushInterval := viper.GetString("databasePushInterval")
-	dbDur, err := time.ParseDuration(dbPushInterval)
-	if err != nil {
-		log.Printf("Unable to parse database push interval. Defaulting to every five minutes. Msg: %v\n", err)
-		dbDur, _ = time.ParseDuration("5m")
-	}
-
-	thetime := time.Now()
-
-	confObj.mu.Lock()
-	confObj.port = viper.GetInt("port")
-	confObj.logFile = viper.GetString("logFile")
-	confObj.dbPath = viper.GetString("databasePath")
-	log.Printf("Using database: %v\n", confObj.dbPath)
-	confObj.stdoutLogging = viper.GetBool("stdoutLogging")
-	if confObj.stdoutLogging {
+	viper.SetDefault("ListenPort", 9001)
+	viper.SetDefault("LogFile", "getwtxt.log")
+	viper.SetDefault("DatabasePath", "getwtxt.db")
+	viper.SetDefault("StdoutLogging", false)
+	viper.SetDefault("ReCacheInterval", "1h")
+	viper.SetDefault("DatabasePushInterval", "5m")
+
+	confObj.Mu.Lock()
+	confObj.Port = viper.GetInt("ListenPort")
+	confObj.LogFile = viper.GetString("LogFile")
+	confObj.DBPath = viper.GetString("DatabasePath")
+	log.Printf("Using database: %v\n", confObj.DBPath)
+	confObj.StdoutLogging = viper.GetBool("StdoutLogging")
+	if confObj.StdoutLogging {
 		log.Printf("Logging to stdout\n")
 	} else {
-		log.Printf("Logging to %v\n", confObj.logFile)
+		log.Printf("Logging to %v\n", confObj.LogFile)
 	}
-	confObj.cacheInterval = dur
-	log.Printf("Cache refresh interval: %v\n", dur)
-	confObj.dbInterval = dbDur
-	log.Printf("Database push interval: %v\n", dbDur)
-	confObj.lastCache = thetime
-	confObj.lastPush = thetime
-	confObj.version = getwtxt
-	confObj.Instance.Name = viper.GetString("instance.name")
-	confObj.Instance.URL = viper.GetString("instance.url")
-	confObj.Instance.Owner = viper.GetString("instance.owner")
-	confObj.Instance.Mail = viper.GetString("instance.mail")
-	confObj.Instance.Desc = viper.GetString("instance.description")
-	confObj.mu.Unlock()
+	confObj.CacheInterval = viper.GetDuration("StatusFetchInterval")
+	log.Printf("User status fetch interval: %v\n", confObj.CacheInterval)
+	confObj.DBInterval = viper.GetDuration("DatabasePushInterval")
+	log.Printf("Database push interval: %v\n", confObj.DBInterval)
+	confObj.LastCache = time.Now()
+	confObj.LastPush = time.Now()
+	confObj.Version = getwtxt
+	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()
+
 }
 
 func initLogging() {
 
 	// only open a log file if it's necessary
-	confObj.mu.RLock()
+	confObj.Mu.RLock()
 
-	if confObj.stdoutLogging {
+	if confObj.StdoutLogging {
 		log.SetOutput(os.Stdout)
 
 	} else {
 
-		logfile, err := os.OpenFile(confObj.logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
+		logfile, err := os.OpenFile(confObj.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
 		if err != nil {
 			log.Printf("Could not open log file: %v\n", err)
 		}
@@ -168,48 +154,31 @@ func initLogging() {
 
 		log.SetOutput(logfile)
 	}
-	confObj.mu.RUnlock()
+	confObj.Mu.RUnlock()
 }
 
 func rebindConfig() {
 
 	// signal to close the log file then wait
-	confObj.mu.RLock()
-	if !confObj.stdoutLogging {
+	confObj.Mu.RLock()
+	if !confObj.StdoutLogging {
 		closeLog <- true
 	}
-	confObj.mu.RUnlock()
-
-	// re-parse update interval
-	nter := viper.GetString("reCacheInterval")
-	dur, err := time.ParseDuration(nter)
-	if err != nil {
-		log.Printf("Unable to parse update interval. Defaulting to once daily. Msg: %v\n", err)
-		dur, _ = time.ParseDuration("1h")
-	}
-
-	// re-parse database backup interval
-	dbPushInterval := viper.GetString("databasePushInterval")
-	dbDur, err := time.ParseDuration(dbPushInterval)
-	if err != nil {
-		log.Printf("Unable to parse database push interval. Defaulting to every five minutes. Msg: %v\n", err)
-		dbDur, _ = time.ParseDuration("5m")
-	}
+	confObj.Mu.RUnlock()
 
 	// reassign values to the config object
-	confObj.mu.Lock()
-	confObj.port = viper.GetInt("port")
-	confObj.logFile = viper.GetString("logFile")
-	confObj.stdoutLogging = viper.GetBool("stdoutLogging")
-	confObj.dbPath = viper.GetString("databasePath")
-	confObj.cacheInterval = dur
-	confObj.dbInterval = dbDur
-	confObj.Instance.Name = viper.GetString("instance.name")
-	confObj.Instance.URL = viper.GetString("instance.url")
-	confObj.Instance.Owner = viper.GetString("instance.owner")
-	confObj.Instance.Mail = viper.GetString("instance.mail")
-	confObj.Instance.Desc = viper.GetString("instance.description")
-	confObj.mu.Unlock()
+	confObj.Mu.Lock()
+	confObj.LogFile = viper.GetString("LogFile")
+	confObj.DBPath = viper.GetString("DatabasePath")
+	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()
 
 	// reinitialize logging
 	initLogging()
@@ -222,9 +191,9 @@ func initTemplates() *template.Template {
 
 // Pull DB data into cache, if available.
 func initDatabase() {
-	confObj.mu.RLock()
-	db, err := leveldb.OpenFile(confObj.dbPath, nil)
-	confObj.mu.RUnlock()
+	confObj.Mu.RLock()
+	db, err := leveldb.OpenFile(confObj.DBPath, nil)
+	confObj.Mu.RUnlock()
 	if err != nil {
 		log.Fatalf("%v\n", err)
 	}
@@ -247,21 +216,21 @@ func watchForInterrupt() {
 		for sigint := range c {
 
 			log.Printf("\n\nCaught %v. Cleaning up ...\n", sigint)
-			confObj.mu.RLock()
+			confObj.Mu.RLock()
 
 			// Close the database cleanly
-			log.Printf("Closing database connection to %v...\n", confObj.dbPath)
+			log.Printf("Closing database connection to %v...\n", confObj.DBPath)
 			db := <-dbChan
 			if err := db.Close(); err != nil {
 				log.Printf("%v\n", err)
 			}
 
-			if !confObj.stdoutLogging {
+			if !confObj.StdoutLogging {
 				// signal to close the log file
 				closeLog <- true
 			}
 
-			confObj.mu.RUnlock()
+			confObj.Mu.RUnlock()
 			close(dbChan)
 			close(closeLog)
 
diff --git a/init_test.go b/init_test.go
index c13ec21..0739d47 100644
--- a/init_test.go
+++ b/init_test.go
@@ -6,7 +6,7 @@ import (
 	"os"
 )
 
-var testport = fmt.Sprintf(":%v", confObj.port)
+var testport = fmt.Sprintf(":%v", confObj.Port)
 
 func initTestConf() {
 	initConfig()
diff --git a/main.go b/main.go
index 43c204b..9e76c65 100644
--- a/main.go
+++ b/main.go
@@ -83,9 +83,9 @@ func main() {
 		HandlerFunc(apiTagsHandler)
 
 	// format the port for the http.Server object
-	confObj.mu.RLock()
-	portnum := fmt.Sprintf(":%v", confObj.port)
-	confObj.mu.RUnlock()
+	confObj.Mu.RLock()
+	portnum := fmt.Sprintf(":%v", confObj.Port)
+	confObj.Mu.RUnlock()
 	// defines options for the http server.
 	// handlers.CompressHandler gzips all responses.
 	// Write/Read timeouts are self explanatory.
diff --git a/types.go b/types.go
index 1aaf7a8..5032c5a 100644
--- a/types.go
+++ b/types.go
@@ -10,28 +10,28 @@ const txtutf8 = "text/plain; charset=utf-8"
 const htmlutf8 = "text/html; charset=utf-8"
 const cssutf8 = "text/css; charset=utf-8"
 
-// config object definition
-type configuration struct {
-	mu            sync.RWMutex
-	port          int
-	logFile       string
-	dbPath        string
-	stdoutLogging bool
-	version       string
-	cacheInterval time.Duration
-	dbInterval    time.Duration
-	lastCache     time.Time
-	lastPush      time.Time
-	Instance
+// Configuration object definition
+type Configuration struct {
+	Mu            sync.RWMutex
+	Port          int           `json:"ListenPort"`
+	LogFile       string        `json:"LogFile"`
+	DBPath        string        `json:"DatabasePath"`
+	StdoutLogging bool          `json:"StdoutLogging"`
+	Version       string        `json:"-"`
+	CacheInterval time.Duration `json:"StatusFetchInterval"`
+	DBInterval    time.Duration `json:"DatabasePushInterval"`
+	LastCache     time.Time     `json:"-"`
+	LastPush      time.Time     `json:"-"`
+	Instance      `json:"Instance"`
 }
 
 // Instance refers to this specific instance of getwtxt
 type Instance struct {
-	Name  string
-	URL   string
-	Owner string
-	Mail  string
-	Desc  string
+	Name  string `json:"Instance.SiteName"`
+	URL   string `json:"Instance.URL"`
+	Owner string `json:"Instance.OwnerName"`
+	Mail  string `json:"Instance.Email"`
+	Desc  string `json:"Instance.Description"`
 }
 
 // RemoteRegistries holds a list of remote registries to