-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroute.go
47 lines (39 loc) · 991 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
40
41
42
43
44
45
46
47
package muxer
import (
"errors"
"net/http"
"regexp"
)
/*
Route defines a mapping between an HTTP request path and an HTTP request handler.
It contains the regular expression that matches the request path, the HTTP method,
the handler to be executed for that request, and the parameter names extracted from the path.
*/
type Route struct {
path *regexp.Regexp
method string
handler http.Handler
params []string
template string
}
func (r *Route) match(path string) map[string]string {
match := r.path.FindStringSubmatch(path)
if match == nil {
return nil
}
params := make(map[string]string)
for i, name := range r.params {
params[name] = match[i+1]
}
return params
}
// PathTemplate retrieves the path template of the current route
func (r *Route) PathTemplate() (string, error) {
if r == nil {
return "", errors.New("route is nil, no template")
}
if r.template == "" {
return r.template, errors.New("template is empty")
}
return r.template, nil
}