@@ -9,25 +9,35 @@ import (
9
9
)
10
10
11
11
// Version is this package's version number.
12
- const Version = "0.0.1 "
12
+ const Version = "0.0.2 "
13
13
14
- // Handler responds to an HTTP request.
14
+ // Handler responds to a HTTP request.
15
15
type Handler interface {
16
16
ServeHTTP (http.ResponseWriter , * http.Request , map [string ]string )
17
17
}
18
18
19
+ // The HandlerFunc type is an adapter to allow the use of an ordinary function
20
+ // as a Handler.
21
+ type HandlerFunc func (http.ResponseWriter , * http.Request , map [string ]string )
22
+
23
+ func (f HandlerFunc ) ServeHTTP (res http.ResponseWriter , req * http.Request , params map [string ]string ) {
24
+ f (res , req , params )
25
+ }
26
+
19
27
// Mux is the HTTP request multiplexer.
20
28
type Mux struct {
21
29
root map [string ]* routing.Node
22
30
}
23
31
24
32
// New returns a new mux.
25
33
func New () * Mux {
26
- return & Mux {}
34
+ return & Mux {root : map [ string ] * routing. Node {} }
27
35
}
28
36
29
37
// Handle registers the handler for the given method and url.
30
38
func (m * Mux ) Handle (method string , url string , handler Handler ) * Mux {
39
+ method = strings .ToUpper (method )
40
+
31
41
if _ , ok := m .root [method ]; ! ok {
32
42
m .root [method ] = routing .New ()
33
43
}
@@ -37,6 +47,36 @@ func (m *Mux) Handle(method string, url string, handler Handler) *Mux {
37
47
return m
38
48
}
39
49
50
+ // Get registers a handler for the GET http method.
51
+ func (m * Mux ) Get (url string , handler Handler ) * Mux {
52
+ return m .Handle (http .MethodGet , url , handler )
53
+ }
54
+
55
+ // Post registers a handler for the POST http method.
56
+ func (m * Mux ) Post (url string , handler Handler ) * Mux {
57
+ return m .Handle (http .MethodPost , url , handler )
58
+ }
59
+
60
+ // Put registers a handler for the PUT http method.
61
+ func (m * Mux ) Put (url string , handler Handler ) * Mux {
62
+ return m .Handle (http .MethodPut , url , handler )
63
+ }
64
+
65
+ // Delete registers a handler for the DELETE http method.
66
+ func (m * Mux ) Delete (url string , handler Handler ) * Mux {
67
+ return m .Handle (http .MethodDelete , url , handler )
68
+ }
69
+
70
+ // Head registers a handler for the HEAD http method.
71
+ func (m * Mux ) Head (url string , handler Handler ) * Mux {
72
+ return m .Handle (http .MethodHead , url , handler )
73
+ }
74
+
75
+ // Patch registers a handler for the PATCH http method.
76
+ func (m * Mux ) Patch (url string , handler Handler ) * Mux {
77
+ return m .Handle (http .MethodPatch , url , handler )
78
+ }
79
+
40
80
func (m Mux ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
41
81
uri := req .RequestURI
42
82
method := req .Method
@@ -56,10 +96,6 @@ func (m Mux) ServeHTTP(res http.ResponseWriter, req *http.Request) {
56
96
return
57
97
}
58
98
59
- if method == http .MethodHead {
60
- method = http .MethodGet
61
- }
62
-
63
99
node , ok := m .root [method ]
64
100
65
101
if ! ok {
0 commit comments