-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.go
80 lines (67 loc) · 1.94 KB
/
app.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
package app
import "github.com/justinas/alice"
import "github.com/bmizerany/pat"
import "net/http"
// App builder.
type App struct {
mux *pat.PatternServeMux
chain alice.Chain
}
// New application.
func New() *App {
return &App{
mux: pat.New(),
chain: alice.New(),
}
}
// Use the given middleware.
func (a *App) Use(mw ...alice.Constructor) {
a.chain = a.chain.Append(mw...)
}
// Head will register a pattern with a handler for HEAD requests.
func (a *App) Head(path string, h interface{}) {
a.mux.Head(path, handler(h))
}
// Get will register a pattern with a handler for GET requests.
// The handler given is also registered for HEAD requests.
func (a *App) Get(path string, h interface{}) {
a.mux.Get(path, handler(h))
}
// Post will register a pattern with a handler for POST requests.
func (a *App) Post(path string, h interface{}) {
a.mux.Post(path, handler(h))
}
// Put will register a pattern with a handler for PUT requests.
func (a *App) Put(path string, h interface{}) {
a.mux.Put(path, handler(h))
}
// Del will register a pattern with a handler for DELETE requests.
func (a *App) Del(path string, h interface{}) {
a.mux.Del(path, handler(h))
}
// Options will register a pattern with a handler for OPTIONS requests.
func (a *App) Options(path string, h interface{}) {
a.mux.Options(path, handler(h))
}
// Listen on `addr`.
func (a *App) Listen(addr string) error {
handler := a.chain.Then(a.mux)
http.Handle("/", handler)
return http.ListenAndServe(addr, nil)
}
// ServeHTTP implements http.Handler
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler := a.chain.Then(a.mux)
handler.ServeHTTP(w, r)
}
// coerce handler into an http.Handler.
func handler(h interface{}) http.Handler {
switch h.(type) {
case func(w http.ResponseWriter, r *http.Request):
return http.HandlerFunc(h.(func(w http.ResponseWriter, r *http.Request)))
case http.Handler:
return h.(http.Handler)
default:
panic("invalid handler")
}
}