summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-09 04:00:21 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-09 04:00:21 -0400
commit7410b8cd74d0f702b087577b1793bcf541ac8371 (patch)
treedbc1aa568abc43fd7048d0587eecbb2a4d950cbb
parent7f76158b9565d804ada682c1a9aa83c22cb8de71 (diff)
downloadgetwtxt-7410b8cd74d0f702b087577b1793bcf541ac8371.tar.gz
init staticAssets explicitly on startup
-rw-r--r--svc/cache.go36
-rw-r--r--svc/conf.go9
-rw-r--r--svc/init.go8
-rw-r--r--svc/init_test.go1
4 files changed, 44 insertions, 10 deletions
diff --git a/svc/cache.go b/svc/cache.go
index 0963892..e2078d7 100644
--- a/svc/cache.go
+++ b/svc/cache.go
@@ -2,6 +2,7 @@ package svc // import "github.com/getwtxt/getwtxt/svc"
 
 import (
 	"bytes"
+	"html/template"
 	"io/ioutil"
 	"os"
 	"sync"
@@ -24,6 +25,41 @@ type staticAssets struct {
 	cssMod   time.Time
 }
 
+func initTemplates() *template.Template {
+	confObj.Mu.RLock()
+	assetsDir := confObj.AssetsDir
+	confObj.Mu.RUnlock()
+
+	return template.Must(template.ParseFiles(assetsDir + "/tmpl/index.html"))
+}
+
+func initAssets() *staticAssets {
+	confObj.Mu.RLock()
+	defer confObj.Mu.RUnlock()
+
+	css, err := os.Open(confObj.AssetsDir + "/style.css")
+	errLog("", err)
+	cssStat, err := css.Stat()
+	errLog("", err)
+	cssData, err := ioutil.ReadAll(css)
+	errLog("", err)
+
+	indStat, err := os.Stat(confObj.AssetsDir + "/tmpl/index.html")
+	errLog("", err)
+
+	var b []byte
+	buf := bytes.NewBuffer(b)
+	errLog("", tmpls.ExecuteTemplate(buf, "index.html", confObj.Instance))
+
+	return &staticAssets{
+		mu:       sync.RWMutex{},
+		index:    buf.Bytes(),
+		indexMod: indStat.ModTime(),
+		css:      cssData,
+		cssMod:   cssStat.ModTime(),
+	}
+}
+
 func cacheTimer() bool {
 	confObj.Mu.RLock()
 	answer := time.Since(confObj.LastCache) > confObj.CacheInterval
diff --git a/svc/conf.go b/svc/conf.go
index 9099477..5f4233b 100644
--- a/svc/conf.go
+++ b/svc/conf.go
@@ -1,7 +1,6 @@
 package svc // import "github.com/getwtxt/getwtxt/svc"
 
 import (
-	"html/template"
 	"log"
 	"os"
 	"path/filepath"
@@ -40,14 +39,6 @@ type Instance struct {
 	Desc  string `yaml:"Instance.Description"`
 }
 
-func initTemplates() *template.Template {
-	confObj.Mu.RLock()
-	assetsDir := confObj.AssetsDir
-	confObj.Mu.RUnlock()
-
-	return template.Must(template.ParseFiles(assetsDir + "/tmpl/index.html"))
-}
-
 func initLogging() {
 
 	confObj.Mu.RLock()
diff --git a/svc/init.go b/svc/init.go
index 4408ecf..d585bfa 100644
--- a/svc/init.go
+++ b/svc/init.go
@@ -5,6 +5,7 @@ import (
 	"log"
 	"os"
 	"os/signal"
+	"sync"
 	"time"
 
 	"github.com/getwtxt/registry"
@@ -36,7 +37,10 @@ var tmpls *template.Template
 
 var twtxtCache = registry.NewIndex()
 
-var remoteRegistries = &RemoteRegistries{}
+var remoteRegistries = &RemoteRegistries{
+	Mu:   sync.RWMutex{},
+	List: make([]string, 0),
+}
 
 var staticCache = &staticAssets{}
 
@@ -63,7 +67,9 @@ func initSvc() {
 	initDatabase()
 	go cacheAndPush()
 	tmpls = initTemplates()
+	staticCache = initAssets()
 	watchForInterrupt()
+	pingAssets()
 }
 
 func checkFlags() {
diff --git a/svc/init_test.go b/svc/init_test.go
index 11a1593..8fd8e95 100644
--- a/svc/init_test.go
+++ b/svc/init_test.go
@@ -23,6 +23,7 @@ func initTestConf() {
 
 		testConfig()
 		tmpls = testTemplates()
+		staticCache = initAssets()
 
 		confObj.Mu.RLock()
 		defer confObj.Mu.RUnlock()
nger/commit/test/tc_commandlist.py?h=v1.9.3&id=d8ea8d5fff1c0973a1c361672565b915d81bf774'>d8ea8d5f ^
0bc410c5 ^
698ba46a ^








ccd3f3c3 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100