-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
421 lines (340 loc) · 11 KB
/
main.go
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"sync"
"text/template"
"time"
"github.com/justindfuller/justindfuller.com/aphorism"
grass "github.com/justindfuller/justindfuller.com/make"
"github.com/justindfuller/justindfuller.com/poem"
"github.com/justindfuller/justindfuller.com/review"
"github.com/justindfuller/justindfuller.com/story"
"github.com/justindfuller/justindfuller.com/word"
"github.com/justindfuller/secretmanager"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type data struct {
Title string
Meta string
Entries [][]byte
Entry []byte
}
const (
yellow = "\033[33m"
red = "\033[31m"
blue = "\033[34m"
noColor = "\033[0m"
)
func logWarning(message string, err error) {
fmt.Println("⚠️ "+yellow+message+":"+noColor, err)
}
func logError(message string, err any) {
fmt.Println("🛑 "+red+message+":"+noColor, err)
os.Exit(1)
}
func logInfo(message string, info string) {
fmt.Println(blue+"🛈 "+message+":"+noColor, info)
}
func main() {
var reminderConfig grass.ReminderConfig
if err := secretmanager.Parse(&reminderConfig); err != nil {
logWarning("Error reading secrets", err)
}
dir, err := os.ReadDir("./")
if err != nil {
logError("Error reading dir", err)
}
type File struct {
Path string
Dir bool
}
var mut sync.Mutex
var files []File //nolint:prealloc // false positive
for _, entry := range dir {
if strings.HasPrefix(entry.Name(), ".") {
continue
}
if strings.HasPrefix(entry.Name(), "node_modules") {
continue
}
mut.Lock()
files = append(files, File{
Path: "/" + entry.Name(),
Dir: entry.IsDir(),
})
mut.Unlock()
}
funcs := template.FuncMap{
"sub1": func(x int) int { return x - 1 },
}
templates := template.New("").Funcs(funcs).Option("missingkey=error")
suffixes := []string{".js", ".css", ".html", ".tmpl"}
for i := 0; i < len(files); i++ {
file := files[i]
path := file.Path
if file.Dir {
dir, err := os.ReadDir("." + path)
if err != nil {
logError("Error reading dir", path)
}
for _, entry := range dir {
files = append(files, File{
Path: path + "/" + entry.Name(),
Dir: entry.IsDir(),
})
}
}
for _, suffix := range suffixes {
if strings.HasSuffix(file.Path, suffix) {
b, err := os.ReadFile("." + file.Path)
if err != nil {
log.Fatalf("File read error=%s file=%s", err, file.Path)
}
if _, err := templates.New(file.Path).Parse(string(b)); err != nil {
log.Fatalf("Template parse error=%s path=%s", err, file.Path)
}
break
}
}
}
http.HandleFunc("/aphorism", func(w http.ResponseWriter, _ *http.Request) {
entries, err := aphorism.Entries()
if err != nil {
http.Error(w, "Error reading Aphorisms", http.StatusInternalServerError)
logWarning("Error reading Aphorisms", err)
return
}
if err := templates.ExecuteTemplate(w, "/aphorism/main.template.html", data{
Title: "Aphorism",
Entries: entries,
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/aphorism/main.template.html")
http.Error(w, "Error reading Aphorisms", http.StatusInternalServerError)
}
})
http.HandleFunc("/word", func(w http.ResponseWriter, _ *http.Request) {
entry, err := word.Entry("entries")
if err != nil {
http.Error(w, "Error reading Words", http.StatusInternalServerError)
logWarning("Error reading Words", err)
return
}
if err := templates.ExecuteTemplate(w, "/word/main.template.html", data{
Title: "Word",
Entry: entry,
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/word/main.template.html")
}
})
http.HandleFunc("/word/", func(w http.ResponseWriter, r *http.Request) {
paths := strings.Split(r.URL.Path, "/")
last := len(paths) - 1
if len(paths) == 0 {
http.Error(w, "Word not found.", http.StatusNotFound)
log.Printf("Word not found: %s", r.URL.Path)
return
}
entry, err := word.Entry(paths[last])
if err != nil {
http.Error(w, "Error reading Words", http.StatusInternalServerError)
log.Printf("Error reading Words: %s", err)
return
}
if err := templates.ExecuteTemplate(w, "/word/entry.template.html", data{
Title: Title(paths[last]),
Entry: entry,
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/word/main.template.html")
}
})
http.HandleFunc("/poem/deep-blue-subaru", func(w http.ResponseWriter, _ *http.Request) {
entries, err := poem.Entry("deep-blue-subaru")
if err != nil {
http.Error(w, "Error reading poem: deep-blue-subaru.", http.StatusInternalServerError)
log.Printf("Error reading poems: %s", err)
return
}
if err := templates.ExecuteTemplate(w, "/poem/main.template.html", data{
Title: "Deep Blue Subaru",
Entries: entries,
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/poem/main.template.html")
}
})
http.HandleFunc("/poem", func(w http.ResponseWriter, _ *http.Request) {
entries, err := poem.Entries()
if err != nil {
http.Error(w, "Error reading poems.", http.StatusInternalServerError)
log.Printf("Error reading poems: %s", err)
return
}
if err := templates.ExecuteTemplate(w, "/poem/main.template.html", data{
Title: "Poem",
Entries: entries,
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/poem/main.template.html")
}
})
http.HandleFunc("/grass.webmanifest", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./make/grass.webmanifest")
})
http.HandleFunc("/grass/worker.js", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./make/grass.worker.js")
})
http.HandleFunc("/grass", func(w http.ResponseWriter, _ *http.Request) {
if err := templates.ExecuteTemplate(w, "/make/grass.template.html", data{
Title: "Grass",
Meta: "grass",
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/make/grass.template.html")
}
})
http.HandleFunc("/kit", func(w http.ResponseWriter, _ *http.Request) {
if err := templates.ExecuteTemplate(w, "/make/kit.template.html", data{
Title: "A Game with Kit",
Meta: "kit",
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/make/kit.template.html")
}
})
http.HandleFunc("/avatar", func(w http.ResponseWriter, _ *http.Request) {
if err := templates.ExecuteTemplate(w, "/make/avatar.template.html", data{
Title: "Guild Avatars",
Meta: "Avatar",
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/make/avatar.template.html")
}
})
http.HandleFunc("/weeks-remaining", func(w http.ResponseWriter, _ *http.Request) {
if err := templates.ExecuteTemplate(w, "/make/remaining.template.html", data{
Title: "Weeks Remaining",
Meta: "Weeks Remaining",
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/make/remaining.template.html")
}
})
http.HandleFunc("/story", func(w http.ResponseWriter, _ *http.Request) {
if err := templates.ExecuteTemplate(w, "/story/main.template.html", data{
Title: "Story",
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/story/main.template.html")
}
})
http.HandleFunc("/story/", func(w http.ResponseWriter, r *http.Request) {
paths := strings.Split(r.URL.Path, "/")
last := len(paths) - 1
if len(paths) == 0 {
http.Error(w, "Story not found.", http.StatusNotFound)
log.Printf("Story not found: %s", r.URL.Path)
return
}
entry, err := story.Entry(paths[last])
if err != nil {
http.Error(w, "Error reading story.", http.StatusInternalServerError)
log.Printf("Error reading story: %s", err)
return
}
if err := templates.ExecuteTemplate(w, "/story/story.template.html", data{
Title: Title(paths[last]),
Entry: entry,
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/story/story.template.html")
}
})
http.HandleFunc("/review", func(w http.ResponseWriter, _ *http.Request) {
if err := templates.ExecuteTemplate(w, "/review/main.template.html", data{
Title: "Review",
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/review/main.template.html")
}
})
http.HandleFunc("/review/", func(w http.ResponseWriter, r *http.Request) {
paths := strings.Split(r.URL.Path, "/")
last := len(paths) - 1
if len(paths) == 0 {
http.Error(w, "Review not found.", http.StatusNotFound)
log.Printf("Review not found: %s", r.URL.Path)
return
}
entry, err := review.Entry(paths[last])
if err != nil {
http.Error(w, "Error reading review.", http.StatusInternalServerError)
log.Printf("Error reading review: %s", err)
return
}
if err := templates.ExecuteTemplate(w, "/review/review.template.html", data{
Title: Title(paths[last]),
Entry: entry,
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/word/main.template.html")
}
})
http.HandleFunc("/make", func(w http.ResponseWriter, _ *http.Request) {
if err := templates.ExecuteTemplate(w, "/make/main.template.html", data{
Title: "Make",
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/make/main.template.html")
}
})
http.HandleFunc("/nature", func(w http.ResponseWriter, _ *http.Request) {
var entries [][]byte
files, err := os.ReadDir("./image/nature")
if err != nil {
log.Printf("Error reading ./image/nature: %s", err)
http.Error(w, "Error loading page.", http.StatusInternalServerError)
return
}
for _, file := range files {
entries = append(entries, []byte(file.Name()))
}
if err := templates.ExecuteTemplate(w, "/nature/main.html.tmpl", data{
Title: "Nature",
Entries: entries,
}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/nature/main.html.tmpl")
}
})
http.HandleFunc("/image/", func(w http.ResponseWriter, r *http.Request) {
log.Print(r.URL.Path)
http.ServeFile(w, r, fmt.Sprintf(".%s", r.URL.Path))
})
http.HandleFunc("/reminder/set", grass.SetHandler)
http.HandleFunc("/reminder/send", grass.SendHandler(reminderConfig))
http.HandleFunc("/site.webmanifest", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./site.webmanifest")
})
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
if err := templates.ExecuteTemplate(w, "/main.template.html", data{}); err != nil {
log.Printf("template execution error=%s template=%s", err, "/main.template.html")
}
})
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
if !strings.HasPrefix(port, ":") {
port = ":" + port
}
logInfo("Listening on port", "http://localhost"+port)
s := http.Server{
Addr: port,
Handler: nil,
ReadTimeout: time.Second,
ReadHeaderTimeout: time.Second,
WriteTimeout: time.Second,
IdleTimeout: time.Second,
}
if err := s.ListenAndServe(); err != nil {
logError("Error listening to port", err)
}
}
func Title(s string) string {
s = strings.ReplaceAll(s, "_", " ")
s = strings.ReplaceAll(s, "-", " ")
return cases.Title(language.AmericanEnglish).String(s)
}