-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
142 lines (120 loc) · 3.22 KB
/
handlers.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
package main
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
// handler for viewing content pages (not the index page)
func pageHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
filename := vars["pageReq"]
filename += ".md"
page, err := pullFromCache(filename)
if err != nil {
log.Printf("%v\n", err)
}
pingCache(page)
if page.Body == nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
etag := fmt.Sprintf("%x", sha256.Sum256([]byte(page.Modtime.String())))
w.Header().Set("ETag", "\""+etag+"\"")
w.Header().Set("Content-Type", htmlutf8)
w.Header().Set("Link", "</>; rel=\"contents\", </css>; rel=\"stylesheet\"")
_, err = w.Write(page.Body)
if err != nil {
log500(w, r, err)
return
}
log200(r)
}
// Handler for viewing the index page.
func indexHandler(w http.ResponseWriter, r *http.Request) {
pingCache(indexCache)
etag := fmt.Sprintf("%x", sha256.Sum256([]byte(indexCache.page.Modtime.String())))
w.Header().Set("ETag", "\""+etag+"\"")
w.Header().Set("Content-Type", htmlutf8)
w.Header().Set("Link", "</>; rel=\"contents\", </css>; rel=\"stylesheet\"")
_, err := w.Write(indexCache.page.Body)
if err != nil {
log500(w, r, err)
return
}
log200(r)
}
// Serves the favicon as a URL.
// This is due to the default behavior of
// not serving naked paths but virtual ones.
func iconHandler(w http.ResponseWriter, r *http.Request) {
confVars.mu.RLock()
assetsDir := confVars.assetsDir
iconPath := confVars.iconPath
confVars.mu.RUnlock()
longname := assetsDir + "/" + iconPath
icon, err := ioutil.ReadFile(longname)
if err != nil {
if os.IsNotExist(err) {
log.Printf("Favicon file specified in config does not exist: /icon request 404\n")
error404(w, r)
return
}
log500(w, r, err)
return
}
stat, err := os.Stat(longname)
if err != nil {
log.Printf("Couldn't stat icon to send ETag header: %v\n", err.Error())
}
etag := fmt.Sprintf("%x", sha256.Sum256([]byte(stat.ModTime().String())))
w.Header().Set("ETag", "\""+etag+"\"")
w.Header().Set("Content-Type", http.DetectContentType(icon))
_, err = w.Write(icon)
if err != nil {
log500(w, r, err)
return
}
log200(r)
}
// Serves the local css file as a url.
// This is due to the default behavior of
// not serving naked paths but virtual ones.
func cssHandler(w http.ResponseWriter, r *http.Request) {
confVars.mu.RLock()
cssPath := confVars.cssPath
confVars.mu.RUnlock()
// check if using local or remote CSS.
// if remote, don't bother doing anything
// and redirect requests to /
if !cssLocal([]byte(cssPath)) {
http.Redirect(w, r, "/", http.StatusFound)
return
}
css, err := ioutil.ReadFile(cssPath)
if err != nil {
if os.IsNotExist(err) {
log.Printf("CSS file specified in config does not exist: /css request 404\n")
error404(w, r)
return
}
log500(w, r, err)
return
}
stat, err := os.Stat(cssPath)
if err != nil {
log.Printf("Couldn't stat CSS file to send ETag header: %v\n", err.Error())
}
etag := fmt.Sprintf("%x", sha256.Sum256([]byte(stat.ModTime().String())))
w.Header().Set("ETag", "\""+etag+"\"")
w.Header().Set("Content-Type", cssutf8)
_, err = w.Write(css)
if err != nil {
log500(w, r, err)
return
}
log200(r)
}