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 {
ik/mu/commit/403unicode.mu?h=hlt&id=951c3f4c92358d3962154a4ef24a19366ea8a619'>951c3f4c ^
307a7553 ^
cd94852d ^






951c3f4c ^

cd94852d ^



951c3f4c ^

cd94852d ^





















71e4f381 ^

cd94852d ^

65dcc746 ^
cd94852d ^












951c3f4c ^
cd94852d ^

cd94852d ^
951c3f4c ^
cd94852d ^
951c3f4c ^
cd94852d ^








951c3f4c ^
cd94852d ^

8e4b4f20 ^



















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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193