-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathrouter.go
70 lines (64 loc) · 1.85 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
package baa
const (
GET int = iota
POST
PUT
DELETE
PATCH
OPTIONS
HEAD
// RouteLength route table length
RouteLength
)
// RouterMethods declare method key in route table
var RouterMethods = map[string]int{
"GET": GET,
"POST": POST,
"PUT": PUT,
"DELETE": DELETE,
"PATCH": PATCH,
"OPTIONS": OPTIONS,
"HEAD": HEAD,
}
// RouterMethodName declare method name with route table key
var RouterMethodName = map[int]string{
GET: "GET",
POST: "POST",
PUT: "PUT",
DELETE: "DELETE",
PATCH: "PATCH",
OPTIONS: "OPTIONS",
HEAD: "HEAD",
}
// Router is an router interface for baa
type Router interface {
// SetAutoHead sets the value who determines whether add HEAD method automatically
// when GET method is added. Combo router will not be affected by this value.
SetAutoHead(v bool)
// SetAutoTrailingSlash optional trailing slash.
SetAutoTrailingSlash(v bool)
// Match find matched route then returns handlers and name
Match(method, uri string, c *Context) ([]HandlerFunc, string)
// URLFor use named route return format url
URLFor(name string, args ...interface{}) string
// Add registers a new handle with the given method, pattern and handlers.
Add(method, pattern string, handlers []HandlerFunc) RouteNode
// GroupAdd registers a list of same prefix route
GroupAdd(pattern string, f func(), handlers []HandlerFunc)
// Routes returns registered route uri in a string slice
Routes() map[string][]string
// NamedRoutes returns named route uri in a string slice
NamedRoutes() map[string]string
}
// RouteNode is an router node
type RouteNode interface {
Name(name string)
}
// IsParamChar check the char can used for route params
// a-z->65:90, A-Z->97:122, 0-9->48->57, _->95
func IsParamChar(c byte) bool {
if (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c >= 48 && c <= 57) || c == 95 {
return true
}
return false
}