From 6753171a8992699baef2c529dd904cf562e87448 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Thu, 6 Jun 2019 00:27:08 -0400 Subject: updated tests to work with new project structure --- svc/handlers_test.go | 4 +-- svc/init.go | 7 +++-- svc/init_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++-- svc/svc.go | 1 + 4 files changed, 93 insertions(+), 6 deletions(-) diff --git a/svc/handlers_test.go b/svc/handlers_test.go index 501d8ad..b572a6d 100644 --- a/svc/handlers_test.go +++ b/svc/handlers_test.go @@ -94,9 +94,9 @@ func Test_cssHandler(t *testing.T) { initTestConf() name := "CSS Handler Test" - css, err := ioutil.ReadFile("assets/style.css") + css, err := ioutil.ReadFile("../assets/style.css") if err != nil { - t.Errorf("Couldn't read assets/style.css: %v\n", err) + t.Errorf("Couldn't read ../assets/style.css: %v\n", err) } w := httptest.NewRecorder() diff --git a/svc/init.go b/svc/init.go index 18fe0b6..9e4c418 100644 --- a/svc/init.go +++ b/svc/init.go @@ -18,7 +18,7 @@ import ( "github.com/syndtr/goleveldb/leveldb" ) -const getwtxt = "0.2.2" +const getwtxt = "0.3.0" var ( flagVersion *bool = pflag.BoolP("version", "v", false, "Display version information, then exit.") @@ -57,7 +57,10 @@ var staticCache = &struct { cssMod: time.Time{}, } -func init() { +// I'm not using init() because it runs +// even during testing and was causing +// problems. +func initSvc() { checkFlags() titleScreen() initConfig() 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() + +} diff --git a/svc/svc.go b/svc/svc.go index 2592122..6870350 100644 --- a/svc/svc.go +++ b/svc/svc.go @@ -12,6 +12,7 @@ import ( // Start is the initialization function for getwtxt func Start() { + initSvc() // StrictSlash(true) allows /api and /api/ // to serve the same content without duplicating -- cgit 1.4.1-2-gfad0