-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
39 lines (34 loc) · 987 Bytes
/
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
package gopeach
import "strings"
// Route stores path, param indexes and handler.
type Route struct {
Path string
ParamIndexes map[string]uint // map[string]uint{"id": 0, "name": 1, ...}
Handler func(ctx *RequestCtx)
}
// NewRoute takes a path string and an handler function and returns new Route.
func NewRoute(path string, h func(ctx *RequestCtx)) Route {
r := convertPath(path)
r.Handler = h
return r
}
// convertPath converts path to regexp and returns Route struct.
//
// e.g. "/users/:id" -> "/users/[a-zA-Z0-9-@:%._\\+~#?&=]{1,256}"
func convertPath(s string) Route {
pi := make(map[string]uint)
ps := strings.Split(s, "/")
for i, p := range ps {
if strings.HasPrefix(p, ":") {
pi[p[1:]] = uint(i)
ps[i] = `[a-zA-Z0-9-@:%._\\+~#?&=]{1,256}`
}
}
ps[0] = "^" + ps[0]
ps[len(ps)-1] = ps[len(ps)-1] + "$"
return Route{
Path: strings.Join(ps, "/"),
ParamIndexes: pi,
Handler: nil,
}
}