-
Notifications
You must be signed in to change notification settings - Fork 4
/
builder_http.go
269 lines (220 loc) · 6.59 KB
/
builder_http.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cadre
import (
"context"
"fmt"
"log"
"github.com/gin-gonic/gin"
"github.com/moderntv/cadre/http"
"github.com/moderntv/cadre/http/middleware"
"github.com/moderntv/cadre/metrics"
"github.com/rs/zerolog"
)
// HTTP Options.
type httpOptions struct {
serverName string
services []string
listeningAddress string
enableLoggingMiddleware bool
enableMetricsMiddleware bool
globalMiddleware []gin.HandlerFunc
metricsAggregation bool
routingGroups map[string]http.RoutingGroup
}
func (h *httpOptions) ensure() (err error) {
if h.listeningAddress == "" {
return fmt.Errorf("no listening address for http server `%s`", h.serverName)
}
return
}
func (h *httpOptions) merge(other *httpOptions) (hh *httpOptions, err error) {
log.Printf("merging %s into %s", other.serverName, h.serverName)
hh = &httpOptions{
serverName: h.serverName,
services: append(h.services, other.services...),
listeningAddress: h.listeningAddress,
enableLoggingMiddleware: h.enableLoggingMiddleware,
enableMetricsMiddleware: h.enableMetricsMiddleware,
globalMiddleware: append(h.globalMiddleware, other.globalMiddleware...),
metricsAggregation: h.metricsAggregation,
routingGroups: h.routingGroups,
}
for _, othersRoutingGroup := range other.routingGroups {
err = WithRoutingGroup(othersRoutingGroup)(hh)
if err != nil {
return
}
}
return
}
func (h *httpOptions) build(cadreContext context.Context, logger zerolog.Logger, metricsRegistry *metrics.Registry) (httpServer *http.HttpServer, err error) {
serverMiddlewares := []gin.HandlerFunc{}
{
if h.enableMetricsMiddleware {
var metricsMiddleware gin.HandlerFunc
metricsMiddleware, err = middleware.NewMetrics(metricsRegistry, h.serverName, h.metricsAggregation)
if err != nil {
return
}
serverMiddlewares = append(serverMiddlewares, metricsMiddleware)
}
if h.enableLoggingMiddleware {
serverMiddlewares = append(serverMiddlewares, middleware.NewLogger(logger))
}
serverMiddlewares = append(serverMiddlewares, gin.Recovery())
serverMiddlewares = append(serverMiddlewares, h.globalMiddleware...)
}
httpServer, err = http.NewHttpServer(cadreContext, h.serverName, h.listeningAddress, logger, serverMiddlewares...)
if err != nil {
return
}
for _, group := range h.routingGroups {
err = httpServer.RegisterRouteGroup(group)
if err != nil {
return
}
}
return
}
func defaultHTTPOptions() *httpOptions {
return &httpOptions{
services: []string{},
enableLoggingMiddleware: true,
enableMetricsMiddleware: true,
globalMiddleware: []gin.HandlerFunc{},
routingGroups: map[string]http.RoutingGroup{},
}
}
type HTTPOption func(*httpOptions) error
// WithHTTP enables HTTP server.
func WithHTTP(serverName string, myHTTPOptions ...HTTPOption) Option {
return func(options *Builder) error {
if options.httpOptions == nil {
options.httpOptions = []*httpOptions{}
}
thisHTTPServerOptions := defaultHTTPOptions()
thisHTTPServerOptions.serverName = serverName
thisHTTPServerOptions.services = append(thisHTTPServerOptions.services, serverName)
for _, option := range myHTTPOptions {
err := option(thisHTTPServerOptions)
if err != nil {
return err
}
}
options.httpOptions = append(options.httpOptions, thisHTTPServerOptions)
return nil
}
}
// WithHTTPListeningAddress configures the HTTP server's listening address.
func WithHTTPListeningAddress(addr string) HTTPOption {
return func(h *httpOptions) error {
h.listeningAddress = addr
return nil
}
}
// WithMetricsAggregation enables path aggregation of endpoint.
// For example when using asterisk (*) in path and endpoint unpacks all possible values
// it will aggregate it back to asterisk (*).
func WithMetricsAggregation() HTTPOption {
return func(h *httpOptions) error {
h.metricsAggregation = true
return nil
}
}
// WithGlobalMiddleware adds new global middleware to the HTTP server
// default - metrics, logging and recovery (in this order).
func WithGlobalMiddleware(middleware ...gin.HandlerFunc) HTTPOption {
return func(h *httpOptions) error {
h.globalMiddleware = append(h.globalMiddleware, middleware...)
return nil
}
}
// WithRoute adds new route to the HTTP server
// returns an error if the path-method combo is already registered.
func WithRoute(method, path string, handlers ...gin.HandlerFunc) HTTPOption {
return WithRoutingGroup(http.RoutingGroup{
Base: "",
Routes: map[string]map[string][]gin.HandlerFunc{
path: {
method: handlers,
},
},
})
}
// WithRoutingGroup adds a new routing group to the HTTP server
// may cause gin configuration eror at runtime. Use with care.
func WithRoutingGroup(group http.RoutingGroup) HTTPOption {
return func(h *httpOptions) (err error) {
g, ok := h.routingGroups[group.Base]
if !ok {
automaticMethods(group)
h.routingGroups[group.Base] = group
return nil
}
group, err = mergeRoutingGroups(g, group)
if err != nil {
return err
}
automaticMethods(group)
h.routingGroups[group.Base] = group
return nil
}
}
func WithoutLoggingMiddleware() HTTPOption {
return func(h *httpOptions) error {
h.enableLoggingMiddleware = false
return nil
}
}
func WithoutMetricsMiddleware() HTTPOption {
return func(h *httpOptions) error {
h.enableMetricsMiddleware = false
return nil
}
}
func automaticMethods(group http.RoutingGroup) {
for path, methodHandlers := range group.Routes {
getHandlers, ok := group.Routes[path]["GET"]
if ok {
_, ok = methodHandlers["HEAD"]
if !ok {
methodHandlers["HEAD"] = getHandlers
}
}
}
}
func mergeRoutingGroups(old, _new http.RoutingGroup) (merged http.RoutingGroup, err error) {
var ok bool
// TODO: deduplicate middleware
old.Middleware = append(old.Middleware, _new.Middleware...)
old.Static = append(old.Static, _new.Static...)
for path, methodHandlers := range _new.Routes {
_, ok = old.Routes[path]
if !ok {
old.Routes[path] = map[string][]gin.HandlerFunc{}
}
for method, handlers := range methodHandlers {
_, ok = old.Routes[path][method]
if ok {
err = fmt.Errorf("conflicting path already registered: path = `%s`; method = `%s`", path, method)
return
}
old.Routes[path][method] = handlers
}
}
outer_loop:
for _, newSubGroup := range _new.Groups {
for i, oldSubGroup := range old.Groups {
if oldSubGroup.Base != newSubGroup.Base {
continue // not it => try next
}
old.Groups[i], err = mergeRoutingGroups(oldSubGroup, newSubGroup)
if err != nil {
return
}
continue outer_loop // merged => continue with new newSubGroup
}
old.Groups = append(old.Groups, newSubGroup)
}
merged = old
return
}