-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrouter.go
168 lines (147 loc) · 4.23 KB
/
grouter.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package grouter
import (
"context"
"fmt"
"log"
"net/http"
"regexp"
"github.com/dghubble/trie"
"go.opentelemetry.io/otel"
)
type ProxyTarget struct {
Host string `json:"url"`
Port string `json:"port"`
}
type ProxyConfig struct {
Path string `json:"path"`
Method string `json:"method"`
Proxy *ProxyTarget `json:"proxy"`
Params map[string]string
}
type HTTPMethod string
const (
GET HTTPMethod = "GET"
POST HTTPMethod = "POST"
PUT HTTPMethod = "PUT"
DELETE HTTPMethod = "DELETE"
PATCH HTTPMethod = "PATCH"
OPTIONS HTTPMethod = "OPTIONS"
HEAD HTTPMethod = "HEAD"
)
type RequestHandler func(context.Context, *ResponseWriter, *http.Request, func()) error
type Route map[HTTPMethod][]RequestHandler
type GlobalRouteOptions struct {
afterAll bool
ignoredPathRegexes []string
}
type concreteGlobalRouteOptions struct {
afterAll bool
ignoredPathRegexes []regexp.Regexp
}
type GlobalHandler struct {
options *concreteGlobalRouteOptions
handler RequestHandler
}
type internalGlobalHandlers struct {
beforeAll []GlobalHandler
afterAll []GlobalHandler
}
type Router struct {
trie *trie.PathTrie
paths map[string]struct{}
globalHandlers internalGlobalHandlers
context context.Context
}
func NewRouter(context context.Context) *Router {
return &Router{
trie: trie.NewPathTrie(),
paths: make(map[string]struct{}),
globalHandlers: internalGlobalHandlers{
beforeAll: []GlobalHandler{},
afterAll: []GlobalHandler{},
},
context: context,
}
}
func (instance *Router) UseGlobal(handler RequestHandler, options *GlobalRouteOptions) {
// Tracing
var spanName string
if options != nil && options.afterAll {
spanName = "UseGlobal:AfterAll"
} else {
spanName = "UseGlobal:BeforeAll"
}
_, span := otel.Tracer(traceProviderName).Start(instance.context, spanName)
defer span.End()
// End tracing
concreteOptions := convertToConcreteGlobalRouteOptions(options)
if concreteOptions != nil && concreteOptions.afterAll {
instance.globalHandlers.afterAll = append(instance.globalHandlers.afterAll, GlobalHandler{
options: concreteOptions,
handler: handler,
})
} else {
instance.globalHandlers.beforeAll = append(instance.globalHandlers.beforeAll, GlobalHandler{
options: concreteOptions,
handler: handler,
})
}
}
func (instance *Router) Use(path string, method HTTPMethod, handler RequestHandler) {
// Tracing
_, span := otel.Tracer(traceProviderName).Start(instance.context, fmt.Sprintf("Use %s %s", method, path))
defer span.End()
// End tracing
instance.paths[path] = struct{}{}
value := instance.trie.Get(path)
if value == nil {
value = make(Route)
}
if value.(Route)[method] == nil || len(value.(Route)[method]) == 0 {
value.(Route)[method] = []RequestHandler{}
}
value.(Route)[method] = append(value.(Route)[method], handler)
instance.trie.Put(path, value)
}
// Convenience methods for each HTTP method
func (instance *Router) Get(path string, handler RequestHandler) {
instance.Use(path, GET, handler)
}
func (instance *Router) Post(path string, handler RequestHandler) {
instance.Use(path, POST, handler)
}
func (instance *Router) Put(path string, handler RequestHandler) {
instance.Use(path, PUT, handler)
}
func (instance *Router) Del(path string, handler RequestHandler) {
instance.Use(path, DELETE, handler)
}
func (instance *Router) Patch(path string, handler RequestHandler) {
instance.Use(path, PATCH, handler)
}
func (instance *Router) Options(path string, handler RequestHandler) {
instance.Use(path, OPTIONS, handler)
}
func (instance *Router) Head(path string, handler RequestHandler) {
instance.Use(path, HEAD, handler)
}
func convertToConcreteGlobalRouteOptions(options *GlobalRouteOptions) *concreteGlobalRouteOptions {
concrete := concreteGlobalRouteOptions{
afterAll: false,
ignoredPathRegexes: []regexp.Regexp{},
}
if options == nil {
return &concrete
}
if options.ignoredPathRegexes != nil && len(options.ignoredPathRegexes) > 0 {
for _, regex := range options.ignoredPathRegexes {
compiled, err := regexp.Compile(regex)
if err != nil {
log.Fatal(err)
}
concrete.ignoredPathRegexes = append(concrete.ignoredPathRegexes, *compiled)
}
}
concrete.afterAll = options.afterAll
return &concrete
}