summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-11 18:13:30 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-11 18:14:07 -0400
commit6dad1372a4680f2314a057b831f8cb2ef44dcf1b (patch)
treed95c10c760045c27714bc0b387f06ca8d2771eae
parentd4af885c40ba55ea0ed9adade98afe0658099c47 (diff)
downloadgetwtxt-6dad1372a4680f2314a057b831f8cb2ef44dcf1b.tar.gz
check if behind reverse proxy
-rw-r--r--Makefile12
-rw-r--r--etc/getwtxt-proxied.service15
-rw-r--r--getwtxt.yml4
-rw-r--r--svc/conf.go10
-rw-r--r--svc/handlers.go2
-rw-r--r--svc/init.go7
-rw-r--r--svc/svc.go19
7 files changed, 59 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index 2151717..eea8cdd 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,18 @@ install:
 	install -m644 assets/tmpl/index.html $(BINDIR)/assets/tmpl
 	install -m644 README.md $(BINDIR)/docs
 	install -m644 LICENSE $(BINDIR)/docs
+	install -m644 etc/getwtxt-proxied.service /etc/systemd/system
+	chown -R getwtxt:getwtxt $(BINDIR)
+
+install-unproxied:
+	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)
 
diff --git a/etc/getwtxt-proxied.service b/etc/getwtxt-proxied.service
new file mode 100644
index 0000000..07ea8cb
--- /dev/null
+++ b/etc/getwtxt-proxied.service
@@ -0,0 +1,15 @@
+[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 \
+          --proxied
+Restart=always
+
+[Install]
+WantedBy=multi-user.target
diff --git a/getwtxt.yml b/getwtxt.yml
index 6d17b95..0388651 100644
--- a/getwtxt.yml
+++ b/getwtxt.yml
@@ -19,6 +19,10 @@
 ##  Changing the following options requires a restart.     ##
 #############################################################
 
+# Set to true if getwtxt will be behind a reverse
+# proxy server, such as Caddy or nginx
+BehindProxy: true
+
 # This is the port that getwtxt will bind to.
 ListenPort: 9001
 
diff --git a/svc/conf.go b/svc/conf.go
index 90cac6a..f15bd13 100644
--- a/svc/conf.go
+++ b/svc/conf.go
@@ -15,6 +15,7 @@ import (
 // this struct.
 type Configuration struct {
 	Mu            sync.RWMutex
+	IsProxied     bool          `yaml:"BehindProxy"`
 	Port          int           `yaml:"ListenPort"`
 	LogFile       string        `yaml:"LogFile"`
 	DBType        string        `yaml:"DatabaseType"`
@@ -132,6 +133,7 @@ func parseConfigFlag() {
 func bindConfig() {
 	confObj.Mu.Lock()
 
+	confObj.IsProxied = viper.GetBool("BehindProxy")
 	confObj.Port = viper.GetInt("ListenPort")
 	confObj.LogFile = viper.GetString("LogFile")
 	confObj.DBType = strings.ToLower(viper.GetString("DatabaseType"))
@@ -157,7 +159,15 @@ func bindConfig() {
 	if *flagAssets != "" {
 		confObj.AssetsDir = *flagAssets
 	}
+	if *flagProxied {
+		confObj.IsProxied = true
+	}
 
+	if confObj.IsProxied {
+		log.Printf("Behind reverse proxy, not using host matching\n")
+	} else {
+		log.Printf("Matching host: %v\n", confObj.Instance.URL)
+	}
 	if confObj.StdoutLogging {
 		log.Printf("Logging to: stdout\n")
 	} else {
diff --git a/svc/handlers.go b/svc/handlers.go
index d3b8d8a..73ef869 100644
--- a/svc/handlers.go
+++ b/svc/handlers.go
@@ -21,7 +21,7 @@ func sendStaticEtag(w http.ResponseWriter, isCSS bool) {
 	if isCSS {
 		etag := getEtag(staticCache.cssMod)
 		w.Header().Set("ETag", "\""+etag+"\"")
-		w.Header().Set("Content-Time", txtutf8)
+		w.Header().Set("Content-Time", cssutf8)
 		return
 	}
 	etag := getEtag(staticCache.indexMod)
diff --git a/svc/init.go b/svc/init.go
index d7419de..082cef8 100644
--- a/svc/init.go
+++ b/svc/init.go
@@ -19,9 +19,10 @@ 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")
+	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.")
+	flagProxied  *bool   = pflag.BoolP("proxied", "p", false, "Use if getwtxt is behind a reverse proxy.")
 )
 
 // Holds the global configuration
diff --git a/svc/svc.go b/svc/svc.go
index 6284239..e5802d2 100644
--- a/svc/svc.go
+++ b/svc/svc.go
@@ -12,28 +12,35 @@ import (
 
 // Start is the initialization function for getwtxt
 func Start() {
+	before := time.Now()
 	initSvc()
 
 	// StrictSlash(true) allows /api and /api/
 	// to serve the same content without duplicating
 	// handlers/paths
 	index := mux.NewRouter().StrictSlash(true)
-	api := index.PathPrefix("/api").Subrouter()
-
-	setIndexRouting(index)
-	setEndpointRouting(api)
 
 	confObj.Mu.RLock()
 	portnum := fmt.Sprintf(":%v", confObj.Port)
+	if !confObj.IsProxied {
+		index.Host(confObj.Instance.URL)
+	}
 	confObj.Mu.RUnlock()
 
-	server := newServer(portnum, index)
+	setIndexRouting(index)
+	api := index.PathPrefix("/api").Subrouter()
+	setEndpointRouting(api)
 
+	server := newServer(portnum, index)
 	log.Printf("*** Listening on %v\n", portnum)
-	log.Printf("*** getwtxt %v Started :: %v ::\n\n", Vers, time.Now().Format(time.RFC3339))
+	log.Printf("*** getwtxt %v Startup finished at %v, took %v\n\n", Vers, time.Now().Format(time.RFC3339), time.Since(before))
 	errLog("", server.ListenAndServe())
 
 	closeLog <- true
+	killTickers()
+	killDB()
+	close(dbChan)
+	close(closeLog)
 }
 
 func newServer(port string, index *mux.Router) *http.Server {