summary refs log tree commit diff stats
path: root/svc/init_test.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-06 00:27:08 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-06 00:27:14 -0400
commit6753171a8992699baef2c529dd904cf562e87448 (patch)
tree2c4f20eb93f84b8da1af240c5501f3c1c185b2e0 /svc/init_test.go
parent39c0e89b0aa010ae80156976fc5b0a5fe9b4b115 (diff)
downloadgetwtxt-6753171a8992699baef2c529dd904cf562e87448.tar.gz
updated tests to work with new project structure
Diffstat (limited to 'svc/init_test.go')
-rw-r--r--svc/init_test.go87
1 files changed, 85 insertions, 2 deletions
diff --git a/svc/init_test.go b/svc/init_test.go
index 43992a4..009b4e0 100644
--- a/svc/init_test.go
+++ b/svc/init_test.go
@@ -2,8 +2,14 @@ package svc // import "github.com/getwtxt/getwtxt/svc"
 
 import (
 	"fmt"
+	"html/template"
 	"log"
 	"os"
+	"strings"
+	"time"
+
+	"github.com/fsnotify/fsnotify"
+	"github.com/spf13/viper"
 )
 
 var testport = fmt.Sprintf(":%v", confObj.Port)
@@ -11,8 +17,8 @@ var hasInit = false
 
 func initTestConf() {
 	if !hasInit {
-		initConfig()
-		tmpls = initTemplates()
+		testConfig()
+		tmpls = testTemplates()
 		logToNull()
 		hasInit = true
 	}
@@ -25,3 +31,80 @@ func logToNull() {
 	}
 	log.SetOutput(hush)
 }
+
+func testTemplates() *template.Template {
+	return template.Must(template.ParseFiles("../assets/tmpl/index.html"))
+}
+
+func testConfig() {
+
+	viper.SetConfigName("getwtxt")
+	viper.SetConfigType("yml")
+	viper.AddConfigPath("..")
+
+	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!")
+
+	confObj.Mu.Lock()
+
+	confObj.Port = viper.GetInt("ListenPort")
+	confObj.LogFile = viper.GetString("LogFile")
+
+	confObj.DBType = strings.ToLower(viper.GetString("DatabaseType"))
+
+	confObj.DBPath = viper.GetString("DatabasePath")
+	log.Printf("Using %v database: %v\n", confObj.DBType, confObj.DBPath)
+
+	confObj.AssetsDir = "../" + viper.GetString("AssetsDirectory")
+
+	confObj.StdoutLogging = viper.GetBool("StdoutLogging")
+	if confObj.StdoutLogging {
+		log.Printf("Logging to stdout\n")
+	} else {
+		log.Printf("Logging to %v\n", confObj.LogFile)
+	}
+
+	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.Vers = 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()
+
+}