-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
65 lines (59 loc) · 1.98 KB
/
route.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
package wstf
import (
"regexp"
)
// @see https://github.com/yuya-takeyama/regexprouter/blob/master/regexprouter.go
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
// @see README.md#Parameter-Pattern
// > The **Parameter Pattern** conforms to the form: `{parameter-name:regexp-expression}`
// This regexp is used to find parameters and their corresponding configure
var patternRegexp = regexp.MustCompile(`\{([^\}:]+)(?::([^\}]+))?\}`)
type Route struct {
// The original pattern given by initializer.
Pattern string
// The Regexp for matching path.
PathRegexp *regexp.Regexp
// Regexp for matching children.
PrefixRegexp *regexp.Regexp
// Parameter names in given pattern.
ParamNames []string
// Processors take effects only when Router is nil.
Processors map[string]Processors
// Router attached to the route.
Router *Router
}
// Create a new route with a specific path.
func NewRoute(pattern string, router *Router) *Route {
// Pre-process the pattern.
//pattern = strings.Replace(pattern, "*", `.*`, -1)
newPattern, err := generatePatternForRegexp(pattern)
// Panic for abnormal patterns.
if err != nil {
panic("pattern [" + newPattern + "] is not recognized as normal.")
}
return &Route{
pattern,
regexp.MustCompile("^" + newPattern + "$"),
regexp.MustCompile("^" + newPattern + "(.*)$"),
findParamNames(pattern),
make(map[string]Processors),
router,
}
}
// GivenPath > whether match this route.
func (m *Route) Handle(remainingPath string, req *Request, res *Response, next func()) {
if m.MatchPath(remainingPath, req, res) {
if m.Router != nil {
m.Router.Handle("", req, res, next)
} else {
HandleMethods(m.Processors[MethodAll], req, res, func() {
HandleMethods(m.Processors[req.Method], req, res, next)
})
}
} else if matched, remainingPath := m.MatchChildren(remainingPath, req, res); m.Router != nil && matched {
// Here can be optimized!
m.Router.Handle(remainingPath, req, res, next)
} else {
next()
}
}