-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
62 lines (52 loc) · 1.67 KB
/
server.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
package main
import (
"html/template"
"net/http"
"strconv"
)
func serve(chapters [10][]string) {
http.HandleFunc("/app.css", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "public/app.css")
})
http.HandleFunc("/manifest.json", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "public/manifest.json")
})
http.HandleFunc("/icon-512.png", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "public/icon-512.png")
})
http.HandleFunc("/icon-192.png", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "public/icon-192.png")
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dayParam := r.FormValue("day")
dateParam := r.FormValue("started")
advancedParam := r.FormValue("advanced")
skippedParam := r.FormValue("skipped")
var dayNr, daysAdvanced, daysSkipped int64
if dayParam != "" {
dayNr, err = strconv.ParseInt(dayParam, 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// TODO: Handle these ParseInt errors
daysAdvanced, _ = strconv.ParseInt(advancedParam, 10, 64)
daysSkipped, _ = strconv.ParseInt(skippedParam, 10, 64)
day, err := decidePrintDay(int(dayNr),
dateParam, int(daysAdvanced), int(daysSkipped))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl := template.Must(template.ParseFiles("tmpl/web.html"))
tmplData := prepareTmplData(day, chapters)
tmpl.Execute(w, tmplData)
})
http.ListenAndServe(":8080", nil)
}