-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (58 loc) · 1.29 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
package main
import (
"html/template"
"io"
"log"
"net/http"
"path"
"help.simplyhomework.nl/help"
)
var items []help.Item
var articleTmpl *template.Template
func setHTTPContentType(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
func getItem(path string) *help.Item {
for i := range items {
item := &items[i]
if item.Path == path {
return item
}
}
return nil
}
func mainPage(w http.ResponseWriter, r *http.Request) {
slug := r.URL.Path[1:]
if slug == "" {
setHTTPContentType(w)
// TODO: render mainpage
} else if slug[len(slug)-4:] == ".css" {
file := path.Base(slug)
name := file[:len(file)-4]
http.ServeFile(w, r, path.Join("views", name, name+".css"))
} else {
setHTTPContentType(w)
item := getItem("help/" + slug + ".md")
if item == nil {
// TODO: status code
io.WriteString(w, "404: not found")
} else {
articleTmpl.Execute(w, item)
}
}
}
func main() {
var err error
items, err = help.LoadItems("entries")
if err != nil {
log.Fatal(err)
}
log.Printf("loaded %d help items\n", len(items))
articleTmpl, err = template.ParseFiles("views/article/article.html")
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/", mainPage)
log.Println("listening on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}