summary refs log tree commit diff stats
path: root/init.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-20 23:01:13 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-21 03:42:18 -0400
commitbd23ef0959496aba4c6fc8ca2b3969bbf17aa9d5 (patch)
tree7b388de8cdf89b6c1db180122678f5559aacdf9a /init.go
parentdf1d1efa19aed5bc6553c7c0a0b4b7dfe20e3bd0 (diff)
downloadgetwtxt-bd23ef0959496aba4c6fc8ca2b3969bbf17aa9d5.tar.gz
added cache update / db push intervals to conf;
made confObj concurrency-safe via sync.RWMutex;
new config values related to db/cache
Diffstat (limited to 'init.go')
-rw-r--r--init.go95
1 files changed, 86 insertions, 9 deletions
diff --git a/init.go b/init.go
index 08ac5b6..47304c5 100644
--- a/init.go
+++ b/init.go
@@ -12,8 +12,11 @@ import (
 	"github.com/getwtxt/registry"
 	"github.com/spf13/pflag"
 	"github.com/spf13/viper"
+	"github.com/syndtr/goleveldb/leveldb"
 )
 
+const getwtxt = "0.1"
+
 // command line flags
 var (
 	flagVersion *bool = pflag.BoolP("version", "v", false, "Display version information, then exit")
@@ -26,6 +29,10 @@ var confObj = &configuration{}
 // signals to close the log file
 var closelog = make(chan bool, 1)
 
+// used to transmit database pointer after
+// initialization
+var dbChan = make(chan *leveldb.DB, 1)
+
 // templates
 var tmpls *template.Template
 
@@ -51,8 +58,8 @@ func checkFlags() {
 		os.Exit(0)
 	}
 	if *flagHelp {
-		fmt.Printf("\nplaceholder\n")
-		fmt.Printf("will add info later\n")
+		titleScreen()
+		helpScreen()
 		os.Exit(0)
 	}
 }
@@ -80,29 +87,54 @@ func initConfig() {
 	})
 
 	viper.SetDefault("port", 9001)
-	viper.SetDefault("logfile", "getwtxt.log")
+	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")
+	}
 
+	confObj.mu.Lock()
 	confObj.port = viper.GetInt("port")
-	confObj.logfile = viper.GetString("logfile")
+	confObj.logFile = viper.GetString("logFile")
+	confObj.dbPath = viper.GetString("databasePath")
 	confObj.stdoutLogging = viper.GetBool("stdoutLogging")
+	confObj.cacheInterval = dur
+	confObj.dbInterval = dbDur
+	confObj.lastCache = time.Now()
 	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()
 }
 
 func initLogging() {
 
 	// only open a log file if it's necessary
+	confObj.mu.RLock()
+
 	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)
 		}
@@ -112,8 +144,10 @@ func initLogging() {
 		// to prevent race conditions when the config is
 		// reloaded.
 		go func(logfile *os.File) {
+
 			<-closelog
 			log.Printf("Closing log file ...\n")
+
 			err = logfile.Close()
 			if err != nil {
 				log.Printf("Couldn't close log file: %v\n", err)
@@ -122,24 +156,48 @@ func initLogging() {
 
 		log.SetOutput(logfile)
 	}
+	confObj.mu.RUnlock()
 }
 
 func rebindConfig() {
 
 	// signal to close the log file then wait
+	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")
+	}
 
 	// reassign values to the config object
+	confObj.mu.Lock()
 	confObj.port = viper.GetInt("port")
-	confObj.logfile = viper.GetString("logfile")
+	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()
 
 	// reinitialize logging
 	initLogging()
@@ -150,6 +208,16 @@ func initTemplates() *template.Template {
 	return template.Must(template.ParseFiles("assets/tmpl/index.html"))
 }
 
+// Pull DB data into cache, if available
+func initDatabase() {
+	db, err := leveldb.OpenFile(confObj.dbPath, nil)
+	if err != nil {
+		log.Fatalf("%v\n", err)
+	}
+
+	dbChan <- db
+}
+
 // Watch for SIGINT aka ^C
 // Close the log file then exit
 func watchForInterrupt() {
@@ -160,10 +228,12 @@ func watchForInterrupt() {
 		for sigint := range c {
 			log.Printf("\n\nCaught %v. Cleaning up ...\n", sigint)
 
+			confObj.mu.RLock()
 			if !confObj.stdoutLogging {
 				// signal to close the log file
 				closelog <- true
 			}
+			confObj.mu.RUnlock()
 
 			close(closelog)
 
@@ -184,8 +254,15 @@ func titleScreen() {
  \__, |\___|\__| \_/\_/  \__/_/\_\\__|
  |___/
              version ` + getwtxt + `
-      github.com/gbmor/getwtxt
-                GPL v3	
-			 
+      github.com/getwtxt/getwtxt
+               GPL  v3	
+`)
+}
+
+func helpScreen() {
+	fmt.Printf(`
+              Help File
+
+  Sections:
 `)
 }