summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-13 17:20:40 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-13 17:22:28 -0400
commitd6fbc25111a9a76b40f19b2481428a9170c706b0 (patch)
treed071366939de7a6994c56153f81b16e836883043
parent9d31e04f8ec8c53c6f6e2323d7422b86b7a4ad90 (diff)
downloadgetwtxt-d6fbc25111a9a76b40f19b2481428a9170c706b0.tar.gz
serving css virtually instead of directly
-rw-r--r--assets/style.css1
-rw-r--r--handlers.go49
-rw-r--r--handlers_test.go26
-rw-r--r--main.go3
-rw-r--r--types.go5
5 files changed, 75 insertions, 9 deletions
diff --git a/assets/style.css b/assets/style.css
new file mode 100644
index 0000000..e148bda
--- /dev/null
+++ b/assets/style.css
@@ -0,0 +1 @@
+@import url("https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css");
diff --git a/handlers.go b/handlers.go
index 01049b0..3b23761 100644
--- a/handlers.go
+++ b/handlers.go
@@ -3,8 +3,10 @@ package main
 import (
 	"crypto/sha256"
 	"fmt"
+	"io/ioutil"
 	"log"
 	"net/http"
+	"os"
 	"time"
 
 	"github.com/gorilla/mux"
@@ -15,7 +17,7 @@ func indexHandler(w http.ResponseWriter, _ *http.Request) {
 	w.Header().Set("Content-Type", htmlutf8)
 	n, err := w.Write([]byte("getwtxt v" + getwtxt))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 
 }
@@ -33,7 +35,7 @@ func apiBaseHandler(w http.ResponseWriter, r *http.Request) {
 	timerfc3339 = append(timerfc3339, pathdata...)
 	n, err := w.Write(timerfc3339)
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 }
 
@@ -46,7 +48,7 @@ func apiFormatHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", txtutf8)
 	n, err := w.Write([]byte(format + "\n"))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 }
 
@@ -59,7 +61,7 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", htmlutf8)
 	n, err := w.Write([]byte(format + "/" + endpoint))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 
 }
@@ -73,7 +75,7 @@ func apiEndpointPOSTHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", htmlutf8)
 	n, err := w.Write([]byte(format + "/" + endpoint))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 
 }
@@ -86,7 +88,7 @@ func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", htmlutf8)
 	n, err := w.Write([]byte("api/" + format + "/tags"))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 
 }
@@ -100,7 +102,40 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", htmlutf8)
 	n, err := w.Write([]byte("api/" + format + "/tags/" + tags))
 	if err != nil || n == 0 {
-		log.Printf("Error writing to HTTP stream: %v\n", err)
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
 	}
 
 }
+
+// Serving the stylesheet virtually because
+// files aren't served directly.
+func cssHandler(w http.ResponseWriter, _ *http.Request) {
+	// read the raw bytes of the stylesheet
+	css, err := ioutil.ReadFile("assets/style.css")
+	if err != nil {
+		if os.IsNotExist(err) {
+			log.Printf("CSS file does not exist: /css request 404\n")
+			http.Error(w, err.Error(), http.StatusNotFound)
+			return
+		}
+		log.Printf("%v\n", err)
+		http.Error(w, err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	// Get the mod time for the etag header
+	stat, err := os.Stat("assets/style.css")
+	if err != nil {
+		log.Printf("Couldn't stat CSS file to send ETag header: %v\n", err)
+	}
+
+	// Sending the sha256 sum of the modtime in hexadecimal for the ETag header
+	etag := fmt.Sprintf("%x", sha256.Sum256([]byte(stat.ModTime().String())))
+
+	w.Header().Set("ETag", "\""+etag+"\"")
+	w.Header().Set("Content-Type", cssutf8)
+	n, err := w.Write(css)
+	if err != nil || n == 0 {
+		log.Printf("Error writing to HTTP stream: %v bytes,  %v\n", n, err)
+	}
+}
diff --git a/handlers_test.go b/handlers_test.go
index 66c1566..4ae7ae6 100644
--- a/handlers_test.go
+++ b/handlers_test.go
@@ -1,7 +1,9 @@
 package main
 
 import (
+	"bytes"
 	"fmt"
+	"io/ioutil"
 	"log"
 	"net/http"
 	"net/http/httptest"
@@ -98,3 +100,27 @@ func Test_apiTagsHandler(t *testing.T) {
 		}
 	})
 }
+func Test_cssHandler(t *testing.T) {
+	initTestConf()
+
+	name := "CSS Handler Test"
+	css, err := ioutil.ReadFile("assets/style.css")
+	if err != nil {
+		t.Errorf("Couldn't read assets/style.css: %v\n", err)
+	}
+
+	w := httptest.NewRecorder()
+	req := httptest.NewRequest("GET", "localhost"+testport+"/css", nil)
+
+	t.Run(name, func(t *testing.T) {
+		cssHandler(w, req)
+		resp := w.Result()
+		body, _ := ioutil.ReadAll(resp.Body)
+		if resp.StatusCode != 200 {
+			t.Errorf("cssHandler(): %v\n", resp.StatusCode)
+		}
+		if !bytes.Equal(body, css) {
+			t.Errorf("cssHandler(): Byte mismatch\n")
+		}
+	})
+}
diff --git a/main.go b/main.go
index bca7847..8e3a2b2 100644
--- a/main.go
+++ b/main.go
@@ -24,6 +24,9 @@ func main() {
 	index.Path("/").
 		Methods("GET").
 		HandlerFunc(indexHandler)
+	index.Path("/css").
+		Methods("GET").
+		HandlerFunc(cssHandler)
 	index.Path("/api").
 		Methods("GET").
 		HandlerFunc(apiBaseHandler)
diff --git a/types.go b/types.go
index 35f70b3..c7d1c50 100644
--- a/types.go
+++ b/types.go
@@ -1,8 +1,9 @@
 package main
 
 // content-type consts
-const txtutf8 = "text/plain; charset=utf8"
-const htmlutf8 = "text/html; charset=utf8"
+const txtutf8 = "text/plain; charset=utf-8"
+const htmlutf8 = "text/html; charset=utf-8"
+const cssutf8 = "text/css; charset=utf-8"
 
 // config object definition
 type configuration struct {
mp;id=080e9cb73fa55cdc862f1dd7593df56e0a6302b8'>^
672e3e50 ^
4690ce81 ^

a654e4ec ^
65361948 ^
65361948 ^

672e3e50 ^



4690ce81 ^
9570363a ^

4690ce81 ^
672e3e50 ^
672e3e50 ^

90560d71 ^
4690ce81 ^

65361948 ^



4690ce81 ^

65361948 ^


90560d71 ^
4690ce81 ^

65361948 ^


672e3e50 ^


76755b28 ^

4690ce81 ^
a654e4ec ^
4690ce81 ^
2f02189d ^
76755b28 ^


4690ce81 ^
c842d90b ^
4690ce81 ^


2f02189d ^
4690ce81 ^

76755b28 ^

672e3e50 ^
4690ce81 ^

a654e4ec ^
65361948 ^
65361948 ^

672e3e50 ^



4690ce81 ^
9570363a ^

4690ce81 ^
672e3e50 ^
672e3e50 ^

90560d71 ^
4690ce81 ^

65361948 ^



4690ce81 ^

65361948 ^


90560d71 ^
4690ce81 ^

65361948 ^


672e3e50 ^


76755b28 ^

4690ce81 ^
0cac7022 ^
c842d90b ^
76755b28 ^

a654e4ec ^
4690ce81 ^
2f02189d ^
4690ce81 ^


a654e4ec ^
4690ce81 ^

2f02189d ^
76755b28 ^




672e3e50 ^
4690ce81 ^
9542bb11 ^
a654e4ec ^
65361948 ^

672e3e50 ^



4690ce81 ^
9570363a ^
4690ce81 ^
672e3e50 ^
672e3e50 ^
65361948 ^

4690ce81 ^

65361948 ^



672e3e50 ^


a654e4ec ^
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
194
195
196
197
198
199
200
201
202
203
204