/
route.go
77 lines (59 loc) · 1.42 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
66
67
68
69
70
71
72
73
74
75
76
77
package courierhttp
import (
"context"
"fmt"
contextx "github.com/octohelm/x/context"
"github.com/octohelm/courier/pkg/courier"
)
type MethodDescriber interface {
Method() string
}
type PathDescriber interface {
Path() string
}
type BasePathDescriber interface {
BasePath() string
}
func GroupRouter(path string) courier.Router {
return courier.NewRouter(Group(path))
}
func BasePathRouter(basePath string) courier.Router {
return courier.NewRouter(BasePath(basePath))
}
func BasePath(basePath string) courier.Operator {
return &metaOperator{basePath: basePath}
}
func Group(path string) courier.Operator {
return &metaOperator{path: path}
}
type metaOperator struct {
path string
basePath string
courier.EmptyOperator
}
func (g *metaOperator) Path() string {
return g.path
}
func (g *metaOperator) BasePath() string {
return g.basePath
}
func (g *metaOperator) String() string {
if g.basePath != "" {
return fmt.Sprintf("basePath(%s)", g.basePath)
}
return fmt.Sprintf("group(%s)", g.path)
}
type RouteDescriber interface {
MethodDescriber
PathDescriber
}
type contextRouteDescriber struct{}
func ContextWithRouteDescriber(ctx context.Context, r RouteDescriber) context.Context {
return contextx.WithValue(ctx, contextRouteDescriber{}, r)
}
func RouteDescriberFromContext(ctx context.Context) RouteDescriber {
if v, ok := ctx.Value(contextRouteDescriber{}).(RouteDescriber); ok {
return v
}
return nil
}