-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.go
37 lines (33 loc) · 948 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
package chakra
import (
"net/http"
"strings"
"github.com/pressly/chi"
"golang.org/x/net/context"
)
// Route builds a resource identifier from the middleware chain. This resource
// identifier along with the operation (HTTP verb) can be used for determining
// access to a resource
func Route(part string) func(chi.Handler) chi.Handler {
return func(next chi.Handler) chi.Handler {
hn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
part = extractACLRoute(part)
if part != "" {
route, ok := ctx.Value("acl.route").([]string)
if ok {
route = append(route, part)
} else {
route = []string{part}
}
ctx = context.WithValue(ctx, "acl.route", route)
}
next.ServeHTTPC(ctx, w, r)
}
return chi.HandlerFunc(hn)
}
}
// extractACLRoute strips leading and trailing slashes as well as ":" url param
// prefix
func extractACLRoute(s string) string {
return strings.Trim(s, "/:")
}