-
Notifications
You must be signed in to change notification settings - Fork 2
/
headers.go
64 lines (54 loc) · 1.68 KB
/
headers.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
package core
import (
"io/ioutil"
"fmt"
"net/http"
"regexp"
)
var (
contentTypes = map[string] string {
"css": "text/css",
"csv": "text/csv",
"gif": "image/gif",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "application/javascript",
"json": "application/json",
"png": "image/png",
"svg": "image/svg+xml",
"txt": "text/plain",
"xml": "text/xml" }
defaultHeaders = map[string]string {
"Content-Type": "text/html",
"Accept-Charset": "utf-8" }
)
func Redirect (url string) {
http.Redirect(Context().ResponseWriter, Context().Request, url, http.StatusFound)
}
func NotFound (w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
template404, err := ioutil.ReadFile("public/404.html")
if err != nil {
template404 = []byte("404 Not Found")
}
w.WriteHeader(404)
fmt.Fprintln(w, string(template404))
}
func setHeaders (w http.ResponseWriter, req *http.Request) {
//FIXME: copying map on each request is bad
var requestHeaders = make(map[string]string)
for k,v := range defaultHeaders {
requestHeaders[k] = v
}
// getting request format
pattern, _ := regexp.Compile("\\.(css|csv|gif|jpeg|jpg|js|json|png|svg|txt|xml)$")
if pattern.MatchString(req.URL.String()) {
requestType := pattern.FindAllStringSubmatch(req.URL.String(), 1)[0][1]
requestHeaders["Content-Type"] = contentTypes[requestType]
}
// setting up custom headers
for key, value := range requestHeaders {
w.Header().Set(key, value)
}
}
// vim: noai:ts=4:sw=4