summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-04 22:56:28 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-05 00:12:49 -0400
commit69217dd27196dd59c96bc405afe4bfd83ad17c00 (patch)
tree60183e14dd807bec5865610c1a58d4e432ed8221
parentdb1dc5d8c2d1557b085b63cb8c511f5a9864b38e (diff)
downloadgetwtxt-69217dd27196dd59c96bc405afe4bfd83ad17c00.tar.gz
cli flags for db path/type, assets dir. systemd unit file. makefile.
-rw-r--r--Makefile38
-rw-r--r--cache.go10
-rw-r--r--etc/getwtxt.service14
-rw-r--r--etc/revive.toml (renamed from revive.toml)0
-rw-r--r--getwtxt.yml2
-rw-r--r--init.go33
-rw-r--r--main.go1
-rw-r--r--types.go1
8 files changed, 89 insertions, 10 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..b6c522c
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,38 @@
+PREFIX?=/usr/local
+_INSTDIR=$(PREFIX)
+BINDIR?=$(_INSTDIR)/getwtxt
+GOFLAGS?=
+
+GOSRC!=find . -name '*.go'
+GOSRC+=go.mod go.sum
+
+getwtxt: $(GOSRC)
+	go build $(GOFLAGS) \
+		-o $@
+
+RM?=rm -f
+
+clean:
+	$(RM) getwtxt
+
+update:
+	git pull --rebase
+
+install:
+	adduser -home $(BINDIR) --system --group getwtxt
+	mkdir -p $(BINDIR)/assets/tmpl $(BINDIR)/docs
+	install -m755 getwtxt $(BINDIR)
+	install -m644 getwtxt.yml $(BINDIR)
+	install -m644 assets/style.css $(BINDIR)/assets
+	install -m644 assets/tmpl/index.html $(BINDIR)/assets/tmpl
+	install -m644 README.md $(BINDIR)/docs
+	install -m644 LICENSE $(BINDIR)/docs
+	install -m644 etc/getwtxt.service /etc/systemd/system
+	chown -R getwtxt:getwtxt $(BINDIR)
+
+uninstall:
+	systemctl stop getwtxt >/dev/null 2>&1
+	systemctl disable getwtxt >/dev/null 2>&1
+	rm -rf $(BINDIR)
+	rm -f /etc/systemd/system/getwtxt.service
+	userdel getwtxt
diff --git a/cache.go b/cache.go
index 2db0fbb..882c4d8 100644
--- a/cache.go
+++ b/cache.go
@@ -67,12 +67,16 @@ func refreshCache() {
 // pulled back into memory from disk.
 func pingAssets() {
 
-	cssStat, err := os.Stat("assets/style.css")
+	confObj.Mu.RLock()
+	assetsDir := confObj.AssetsDir
+	confObj.Mu.RUnlock()
+
+	cssStat, err := os.Stat(assetsDir + "/style.css")
 	if err != nil {
 		log.Printf("%v\n", err.Error())
 	}
 
-	indexStat, err := os.Stat("assets/tmpl/index.html")
+	indexStat, err := os.Stat(assetsDir + "/tmpl/index.html")
 	if err != nil {
 		log.Printf("%v\n", err.Error())
 	}
@@ -99,7 +103,7 @@ func pingAssets() {
 
 	if !cssMod.Equal(cssStat.ModTime()) {
 
-		css, err := ioutil.ReadFile("assets/style.css")
+		css, err := ioutil.ReadFile(assetsDir + "/style.css")
 		if err != nil {
 			log.Printf("%v\n", err.Error())
 		}
diff --git a/etc/getwtxt.service b/etc/getwtxt.service
new file mode 100644
index 0000000..0d541ff
--- /dev/null
+++ b/etc/getwtxt.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=getwtxt
+
+[Service]
+Type=simple
+ExecStart=/usr/local/getwtxt/getwtxt \
+          --assets /usr/local/getwtxt/assets \
+          --config /usr/local/getwtxt/getwtxt.yml \
+          --db /usr/local/getwtxt/getwtxt.db \
+          --dbtype leveldb
+Restart=always
+
+[Install]
+WantedBy=multi-user.target
diff --git a/revive.toml b/etc/revive.toml
index f9e2405..f9e2405 100644
--- a/revive.toml
+++ b/etc/revive.toml
diff --git a/getwtxt.yml b/getwtxt.yml
index 4c72454..fb05a96 100644
--- a/getwtxt.yml
+++ b/getwtxt.yml
@@ -41,7 +41,7 @@ DatabasePath: "getwtxt.db"
 # requests, to stdout. It will ignore any set log file.
 # Useful for debugging, but you probably want to keep
 # logs.
-StdoutLogging: true
+StdoutLogging: false
 
 # The file getwtxt will append log messages to. Can be a
 # relative or absolute path.
diff --git a/init.go b/init.go
index 076a7ca..18a3d96 100644
--- a/init.go
+++ b/init.go
@@ -25,6 +25,9 @@ var (
 	flagHelp     *bool   = pflag.BoolP("help", "h", false, "Display the quick-help screen.")
 	flagMan      *bool   = pflag.BoolP("manual", "m", false, "Display the configuration manual.")
 	flagConfFile *string = pflag.StringP("config", "c", "", "The name/path of the configuration file you wish to use.")
+	flagAssets   *string = pflag.StringP("assets", "a", "", "The location of the getwtxt assets directory")
+	flagDBPath   *string = pflag.StringP("db", "d", "", "Path to the getwtxt database")
+	flagDBType   *string = pflag.StringP("dbtype", "t", "", "Type of database being used")
 )
 
 var confObj = &Configuration{}
@@ -54,7 +57,7 @@ var staticCache = &struct {
 	cssMod:   time.Time{},
 }
 
-func initGetwtxt() {
+func init() {
 	checkFlags()
 	titleScreen()
 	initConfig()
@@ -122,6 +125,7 @@ func initConfig() {
 	viper.SetDefault("ListenPort", 9001)
 	viper.SetDefault("LogFile", "getwtxt.log")
 	viper.SetDefault("DatabasePath", "getwtxt.db")
+	viper.SetDefault("DatabaseType", "leveldb")
 	viper.SetDefault("StdoutLogging", false)
 	viper.SetDefault("ReCacheInterval", "1h")
 	viper.SetDefault("DatabasePushInterval", "5m")
@@ -137,9 +141,24 @@ func initConfig() {
 	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 database: %v\n", confObj.DBPath)
+	if *flagDBType == "" {
+		confObj.DBType = strings.ToLower(viper.GetString("DatabaseType"))
+	} else {
+		confObj.DBType = *flagDBType
+	}
+
+	if *flagDBPath == "" {
+		confObj.DBPath = viper.GetString("DatabasePath")
+	} else {
+		confObj.DBPath = *flagDBPath
+	}
+	log.Printf("Using %v database: %v\n", confObj.DBType, confObj.DBPath)
+
+	if *flagAssets == "" {
+		confObj.AssetsDir = "assets"
+	} else {
+		confObj.AssetsDir = *flagAssets
+	}
 
 	confObj.StdoutLogging = viper.GetBool("StdoutLogging")
 	if confObj.StdoutLogging {
@@ -233,7 +252,11 @@ func rebindConfig() {
 }
 
 func initTemplates() *template.Template {
-	return template.Must(template.ParseFiles("assets/tmpl/index.html"))
+	confObj.Mu.RLock()
+	assetsDir := confObj.AssetsDir
+	confObj.Mu.RUnlock()
+
+	return template.Must(template.ParseFiles(assetsDir + "/tmpl/index.html"))
 }
 
 // Pull DB data into cache, if available.
diff --git a/main.go b/main.go
index a3f0133..2aa8758 100644
--- a/main.go
+++ b/main.go
@@ -11,7 +11,6 @@ import (
 )
 
 func main() {
-	initGetwtxt()
 
 	// StrictSlash(true) allows /api and /api/
 	// to serve the same content without duplicating
diff --git a/types.go b/types.go
index 7e78ae0..b32edd3 100644
--- a/types.go
+++ b/types.go
@@ -20,6 +20,7 @@ type Configuration struct {
 	LogFile       string        `yaml:"LogFile"`
 	DBType        string        `yaml:"DatabaseType"`
 	DBPath        string        `yaml:"DatabasePath"`
+	AssetsDir     string        `yaml:"-"`
 	StdoutLogging bool          `yaml:"StdoutLogging"`
 	Version       string        `yaml:"-"`
 	CacheInterval time.Duration `yaml:"StatusFetchInterval"`
b42a4'>4f3d9065 ^
b17de7ba ^

4f3d9065 ^
b0bf5321 ^
aed29290 ^

b17de7ba ^
aed29290 ^
b17de7ba ^
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