This repository was archived by the owner on Mar 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
72 lines (59 loc) · 1.8 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
63
64
65
66
67
68
69
70
71
72
package nametagprinter
import (
"fmt"
"log"
"net/http"
"regexp"
)
type route struct {
re *regexp.Regexp
handler func(http.ResponseWriter, *http.Request, []string)
}
type staticRoute struct {
re *regexp.Regexp
}
type RegexpHandler struct {
routes []*route
staticRoutes []*staticRoute
}
func (h *RegexpHandler) AddRoute(re string, handler func(http.ResponseWriter, *http.Request, []string)) {
r := &route{regexp.MustCompile(re), handler}
h.routes = append(h.routes, r)
}
func (h *RegexpHandler) ServeStaticFiles(re string) {
r := &staticRoute{regexp.MustCompile(re)}
h.staticRoutes = append(h.staticRoutes, r)
}
func (h *RegexpHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
for _, route := range h.routes {
matches := route.re.FindStringSubmatch(r.URL.Path)
if matches != nil {
route.handler(rw, r, matches)
return
}
}
for _, staticRoute := range h.staticRoutes {
matches := staticRoute.re.FindStringSubmatch(r.URL.Path)
if matches != nil {
http.ServeFile(rw, r, "."+matches[0])
return
}
}
// No match
rw.WriteHeader(http.StatusNotFound)
log.Println("No handler found for " + r.URL.Path)
}
func Serve(c *Config) (err error) {
log.Println(fmt.Sprintf("Starting server %s on at %s:%d ...", VERSION, c.Server.Address, c.Server.Port))
pageCntrl := NewPageController()
tagCntrl := NewTagController()
printCntrl := NewPrintController(c.Tag.Template)
reHandler := new(RegexpHandler)
reHandler.ServeStaticFiles("^/static/(.+)")
reHandler.AddRoute("^/api/print$", printCntrl.PrintTagHandler)
reHandler.AddRoute("^/new$", tagCntrl.NewTagHandler)
reHandler.AddRoute("^/([^/\\.]+)$", pageCntrl.PageHandler)
reHandler.AddRoute("^/$", pageCntrl.IndexHandler)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", c.Server.Address, c.Server.Port), reHandler))
return
}