This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.go
96 lines (81 loc) · 2.21 KB
/
module.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
package hikaru
import (
"net/http"
"path"
"github.com/julienschmidt/httprouter"
)
type HandlerFunc func(*Context)
type Middleware func(HandlerFunc) HandlerFunc
type Module struct {
parent *Module
prefix string
router *httprouter.Router
middleware Middleware
}
func NewModule(prefix string) *Module {
return &Module{
parent: nil,
prefix: prefix,
router: httprouter.New(),
middleware: nil,
}
}
func (m *Module) ServeHTTP(w http.ResponseWriter, req *http.Request) {
m.router.ServeHTTP(w, req)
}
func (m *Module) Module(component string, middleware Middleware) *Module {
prefix := path.Join(m.prefix, component)
return &Module{
parent: m,
prefix: prefix,
router: m.router,
middleware: middleware,
}
}
func (m *Module) Handle(method, p string, handler HandlerFunc) {
if m.middleware != nil {
handler = m.middleware(handler)
}
h := m.wrapHandlerFunc(handler)
m.router.Handle(method, path.Join(m.prefix, p), h)
}
func (m *Module) wrapHandlerFunc(handler HandlerFunc) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, hp httprouter.Params) {
c := getContext(w, r, hp)
defer releaseContext(c)
c.tracef("hikaru: Request is %v", c.Request)
handler(c)
}
}
// POST is a shortcut for Module.Handle("POST", p, handle)
func (m *Module) POST(p string, handler HandlerFunc) {
m.Handle("POST", p, handler)
}
// GET is a shortcut for Module.Handle("GET", p, handle)
func (m *Module) GET(p string, handler HandlerFunc) {
m.Handle("GET", p, handler)
}
// OPTIONS is a shortcut for Module.Handle("OPTIONS", p, handle)
func (m *Module) OPTIONS(p string, handler HandlerFunc) {
m.Handle("OPTIONS", p, handler)
}
// HEAD is a shortcut for Module.Handle("HEAD", p, handle)
func (m *Module) HEAD(p string, handler HandlerFunc) {
m.Handle("HEAD", p, handler)
}
func (m *Module) Static(p, root string) {
p = path.Join(p, "*filepath")
fileServer := http.FileServer(http.Dir(root))
m.GET(p, func(c *Context) {
fp, err := c.TryString("filepath")
if err != nil {
c.Errorf(err.Error())
c.Fail()
} else {
original := c.Request.URL.Path
c.Request.URL.Path = fp
fileServer.ServeHTTP(c.ResponseWriter, c.Request)
c.Request.URL.Path = original
}
})
}