-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
76 lines (71 loc) · 1.3 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
package gorouter
import (
"net/http"
)
type Router struct {
c *Controller
http.HandlerFunc
}
func NewRouter(c *Controller) *Router {
return &Router{c: c}
}
func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
method := r.Method
switch method {
case http.MethodGet:
h, ok := rr.c.get[path]
if !ok {
var v Vars
if h, v, ok = findPath(r, rr.c.get); ok {
h.hFunc(toCtx(w, r, v))
return
}
w.WriteHeader(http.StatusNotFound)
return
}
h.hFunc(toCtx(w, r))
break
case http.MethodDelete:
h, ok := rr.c.delete[path]
if !ok {
var v Vars
if h, v, ok = findPath(r, rr.c.delete); ok {
h.hFunc(toCtx(w, r, v))
return
}
w.WriteHeader(http.StatusNotFound)
return
}
h.hFunc(toCtx(w, r))
break
case http.MethodPut:
h, ok := rr.c.put[path]
if !ok {
var v Vars
if h, v, ok = findPath(r, rr.c.put); ok {
h.hFunc(toCtx(w, r, v))
return
}
w.WriteHeader(http.StatusNotFound)
return
}
h.hFunc(toCtx(w, r))
break
case http.MethodPost:
h, ok := rr.c.post[path]
if !ok {
var v Vars
if h, v, ok = findPath(r, rr.c.post); ok {
h.hFunc(toCtx(w, r, v))
return
}
w.WriteHeader(http.StatusNotFound)
return
}
h.hFunc(toCtx(w, r))
break
default:
w.WriteHeader(http.StatusNotFound)
}
}