cli flags for db path/type, assets dir. systemd unit file. makefile.

This commit is contained in:
Ben Morrison 2019-06-04 22:56:28 -04:00
parent db1dc5d8c2
commit 69217dd271
Signed by untrusted user: gbmor
GPG Key ID: 8F192E4720BB0DAC
8 changed files with 89 additions and 10 deletions

38
Makefile Normal file
View File

@ -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

View File

@ -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())
}

14
etc/getwtxt.service Normal file
View File

@ -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

View File

@ -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.

33
init.go
View File

@ -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.

View File

@ -11,7 +11,6 @@ import (
)
func main() {
initGetwtxt()
// StrictSlash(true) allows /api and /api/
// to serve the same content without duplicating

View File

@ -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"`