-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype.go
69 lines (55 loc) · 1.7 KB
/
type.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
package doob
import (
"log"
"regexp"
"github.com/fudali113/doob/utils"
"github.com/fudali113/doob/config"
"github.com/fudali113/doob/http/router"
. "github.com/fudali113/doob/http/const"
)
// 封装node,对外提供简单方法
type Router struct {
node *router.Node
}
func (r Router) AddHandlerFunc(allUrl string, handler interface{}, methods ...HttpMethod) {
urls := utils.Split(allUrl, config.UrlSplitSymbol)
for _, url := range urls {
for _, method := range methods {
methodStr := string(method)
if checkMethodStr(methodStr) {
r.node.InsertChild(url, router.GetSimpleRestHandler(methodStr, handler))
if config.AutoAddHead && methodStr == string(GET) {
r.node.InsertChild(url, router.GetSimpleRestHandler(string(HEAD), handler))
}
} else {
log.Printf("%s method is unsupport", methodStr)
}
}
}
}
func (r Router) Get(allUrl string, handler interface{}) {
r.AddHandlerFunc(allUrl, handler, GET)
}
func (r Router) Post(allUrl string, handler interface{}) {
r.AddHandlerFunc(allUrl, handler, POST)
}
func (r Router) Put(allUrl string, handler interface{}) {
r.AddHandlerFunc(allUrl, handler, PUT)
}
func (r Router) Delete(allUrl string, handler interface{}) {
r.AddHandlerFunc(allUrl, handler, DELETE)
}
func (r Router) Options(allUrl string, handler interface{}) {
r.AddHandlerFunc(allUrl, handler, OPTIONS)
}
func (r Router) Head(allUrl string, handler interface{}) {
r.AddHandlerFunc(allUrl, handler, HEAD)
}
type HttpMethod string
func checkHttpMethod(httpMethod HttpMethod) bool {
return checkMethodStr(string(httpMethod))
}
func checkMethodStr(httpMethod string) bool {
match, _ := regexp.MatchString("get|post|put|delete|options|head", httpMethod)
return match
}