-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
79 lines (62 loc) · 1.53 KB
/
router.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
package main
import (
"io/ioutil"
"path/filepath"
"regexp"
)
func router(req Request) func(Request) string {
// static files
staticRxp, _ := regexp.Compile("/static/(.*)")
if len(staticRxp.FindStringSubmatch(req["path"])) > 0 {
return func(req Request) string {
return getStatic(staticRxp.FindStringSubmatch(req["path"])[1], req)
}
}
// index route
if req["path"] == "/" {
return getIndex
}
// posts route
if req["path"] == "/post" {
return getPost
}
if req["path"] == "/favicon.ico" {
return func(req Request) string {
return getStatic("gopher3.png", req)
}
}
// 404 if nothing else matched
return notFound
}
func getIndex(req Request) string {
body := `<html><body>
<h1>home page</h1>
<p><a href="/post">post</a></p>
<p><img src="/static/golang_128x128.png"/></p>
</body></html>`
headers := DefaultHtmlHeaders()
return response(200, headers, body)
}
func getPost(req Request) string {
body := `<html><body><h1>A Post</h1>
<p>Lorem Ipsom</p>
<p><a href="/">index</a></p></body></html>`
headers := DefaultHtmlHeaders()
return response(200, headers, body)
}
func getStatic(path string, req Request) string {
imagePath, _ := filepath.Abs("static/" + path)
file, err := ioutil.ReadFile(imagePath)
if err != nil {
return notFound(req)
}
body := string(file[:])
headers := PngHeaders()
return response(200, headers, body)
}
func notFound(req Request) string {
body := `<html><body><h1>404</h1>
<p>Page Not Found</p></body></html>`
headers := DefaultHtmlHeaders()
return response(200, headers, body)
}