-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrouter.go
120 lines (102 loc) · 2.88 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package sgo
import (
"net/http"
"strings"
)
// HTTP methods
const (
CONNECT = http.MethodConnect
DELETE = http.MethodDelete
GET = http.MethodGet
HEAD = http.MethodHead
OPTIONS = http.MethodOptions
PATCH = http.MethodPatch
POST = http.MethodPost
PUT = http.MethodPut
TRACE = http.MethodTrace
)
var methods = [...]string{
http.MethodConnect,
http.MethodDelete,
http.MethodGet,
http.MethodHead,
http.MethodOptions,
http.MethodPatch,
http.MethodPost,
http.MethodPut,
http.MethodTrace,
}
// NotFoundHandler .
func NotFoundHandler(ctx *Context) error {
http.NotFound(ctx.Resp, ctx.Req)
return nil
}
// MethodNotAllowedHandler .
func MethodNotAllowedHandler(ctx *Context) error {
http.Error(ctx.Resp, "Method Not Allowed", http.StatusMethodNotAllowed)
return nil
}
func (sg *SGo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := sg.Pool.Get().(*Context)
ctx.Init(w, r)
ctx.Req.ParseForm()
node := sg.Tree.Search(strings.Split(r.URL.Path, "/")[1:], ctx.Params())
if node != nil && node.methods[r.Method] != nil {
ctx.handlers = append(ctx.handlers, node.methods[r.Method])
} else if node != nil && node.methods[r.Method] == nil {
ctx.handlers = append(ctx.handlers, sg.MethodNotAllowedHandler)
} else {
ctx.handlers = append(ctx.handlers, sg.NotFoundHandler)
}
ctx.Next()
sg.Pool.Put(ctx)
}
// Handle register custom METHOD request HandlerFunc
func (sg *SGo) Handle(method, path string, handler HandlerFunc) {
if len(path) < 1 || path[0] != '/' {
panic("Path should be like '/s/go'")
}
sg.Tree.Insert(method, path, handler)
}
// GET register GET request handler
func (sg *SGo) GET(path string, handler HandlerFunc) {
sg.Handle(GET, path, handler)
}
// HEAD register HEAD request handler
func (sg *SGo) HEAD(path string, handler HandlerFunc) {
sg.Handle(HEAD, path, handler)
}
// OPTIONS register OPTIONS request handler
func (sg *SGo) OPTIONS(path string, handler HandlerFunc) {
sg.Handle(OPTIONS, path, handler)
}
// POST register POST request handler
func (sg *SGo) POST(path string, handler HandlerFunc) {
sg.Handle(POST, path, handler)
}
// PUT register PUT request handler
func (sg *SGo) PUT(path string, handler HandlerFunc) {
sg.Handle(PUT, path, handler)
}
// PATCH register PATCH request HandlerFunc
func (sg *SGo) PATCH(path string, handler HandlerFunc) {
sg.Handle(PATCH, path, handler)
}
// DELETE register DELETE request handler
func (sg *SGo) DELETE(path string, handler HandlerFunc) {
sg.Handle(DELETE, path, handler)
}
// CONNECT register CONNECT request handler
func (sg *SGo) CONNECT(path string, handler HandlerFunc) {
sg.Handle(CONNECT, path, handler)
}
// TRACE register TRACE request handler
func (sg *SGo) TRACE(path string, handler HandlerFunc) {
sg.Handle(TRACE, path, handler)
}
// Any register any method handler
func (sg *SGo) Any(path string, handler HandlerFunc) {
for _, m := range methods {
sg.Handle(m, path, handler)
}
}