-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
595 lines (500 loc) · 14.6 KB
/
router.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package router
import (
"fmt"
"net/http"
"reflect"
"slices"
"strings"
"sync"
)
var (
DefaultRedirectTrailingSlash = false
DefaultRedirectStatusCode = http.StatusTemporaryRedirect // or http.StatusMovedPermanently
DefaultUseOpenapiDocs = false
OpenApiVersion = "3.0.1"
)
type (
Router struct {
mux *http.ServeMux
basePath string
redirectTrailingSlash bool
openapiDocs bool
middlewares []Middleware
parent *Router // Reference to the parent router
handleStatus map[int]http.HandlerFunc
patternMap map[string]string
once sync.Once
mu sync.RWMutex
openapi *OpenAPI
}
Docs struct {
Tags []string // Tags for the operation
Summary string // Short summary of the operation
Description string // Operation description
Parameters []Parameter // Parameters for the operation
RequestBody *RequestBody // Request body for the operation
Responses map[string]Response // Expected responses
Security []map[string][]string // Security requirements
In map[string]DocIn
Out map[string]DocOut
}
DocOut struct {
ApplicationType string
Description string
Object any
}
DocIn struct {
Object any
Required bool
}
Middleware func(http.Handler) http.Handler
)
func New(ht *http.ServeMux, title string, version string) *Router {
return &Router{
mux: ht,
redirectTrailingSlash: DefaultRedirectTrailingSlash,
openapiDocs: DefaultUseOpenapiDocs,
openapi: &OpenAPI{
Openapi: OpenApiVersion,
Info: Info{
Title: title,
Version: version,
},
Servers: []Server{},
Paths: make(map[string]PathItem),
Components: Components{
Schemas: make(map[string]Schema),
},
},
handleStatus: make(map[int]http.HandlerFunc),
patternMap: make(map[string]string),
}
}
func (r *Router) AddServerEndpoint(url string, description string) {
r.openapi.Servers = append(r.openapi.Servers, Server{
URL: url,
Description: description,
})
}
func (r *Router) Get(pattern string, handler http.HandlerFunc, doc ...Docs) {
r.handle(http.MethodGet, pattern, handler, doc...)
}
func (r *Router) Head(pattern string, handler http.HandlerFunc, doc ...Docs) {
r.handle(http.MethodHead, pattern, handler, doc...)
}
func (r *Router) Post(pattern string, handler http.HandlerFunc, doc ...Docs) {
r.handle(http.MethodPost, pattern, handler, doc...)
}
func (r *Router) Put(pattern string, handler http.HandlerFunc, doc ...Docs) {
r.handle(http.MethodPut, pattern, handler, doc...)
}
func (r *Router) Patch(pattern string, handler http.HandlerFunc, doc ...Docs) {
r.handle(http.MethodPatch, pattern, handler, doc...)
}
func (r *Router) Delete(pattern string, handler http.HandlerFunc, doc ...Docs) {
r.handle(http.MethodDelete, pattern, handler, doc...)
}
func (r *Router) Group(basePath string, fn func(*Router)) {
subRouter := &Router{
basePath: r.basePath + basePath,
redirectTrailingSlash: r.redirectTrailingSlash,
middlewares: append([]Middleware{}, r.middlewares...),
parent: r,
openapiDocs: r.openapiDocs,
handleStatus: r.handleStatus,
}
fn(subRouter)
}
func (r *Router) RedirectTrailingSlash(redirect bool) {
r.redirectTrailingSlash = redirect
}
func (r *Router) UseOpenapiDocs(use bool) {
r.openapiDocs = use
}
func (r *Router) HandleStatus(httpStatus int, handler http.HandlerFunc) {
r.handleStatus[httpStatus] = handler
}
func (r *Router) Use(middleware Middleware) {
r.middlewares = append(r.middlewares, middleware)
}
func (r *Router) ServeFiles(pattern string, fs http.FileSystem) {
if r.basePath != "" {
pattern = r.basePath + pattern
}
// Ensure the pattern ends with "/" for directory serving
if pattern == "" || pattern[len(pattern)-1] != '/' {
pattern += "/"
}
// Create a file server handler
fileServer := http.StripPrefix(pattern, http.FileServer(fs))
// Wrap the file server with middlewares
var finalHandler http.Handler = fileServer
for i := len(r.middlewares) - 1; i >= 0; i-- {
finalHandler = r.middlewares[i](finalHandler)
}
// Register the handler for GET method
r.mux.Handle("GET "+pattern, finalHandler)
}
func (r *Router) ServeFile(pattern string, filepath string) {
if r.basePath != "" {
pattern = r.basePath + pattern
}
// Handler to serve the file
handler := func(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, filepath)
}
// Wrap the handler with middlewares
var finalHandler http.Handler = http.HandlerFunc(handler)
for i := len(r.middlewares) - 1; i >= 0; i-- {
finalHandler = r.middlewares[i](finalHandler)
}
// Register the handler for GET method
fullPattern := "GET " + pattern
r.mux.Handle(fullPattern, finalHandler)
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// just before serving add all the option handlers based on the openapi paths
if r.openapiDocs {
r.once.Do(func() {
for p, _ := range r.openapi.Paths {
fmt.Println("Registering options handler for", p)
r.registerOptionsHandler(p)
}
})
}
if r.redirectTrailingSlash {
if req.URL.Path != "/" && req.URL.Path[len(req.URL.Path)-1] == '/' {
http.Redirect(w, req, req.URL.Path[:len(req.URL.Path)-1], DefaultRedirectStatusCode)
return
}
}
interceptor := &routingStatusInterceptWriter{
ResponseWriter: &excludeHeaderWriter{
ResponseWriter: w,
excludedHeaders: []string{HeaderFlagDoNotIntercept},
},
interceptMap: make(map[int]func() bool),
}
for k, v := range r.handleStatus {
interceptor.interceptMap[k] = func() bool {
return v != nil && w.Header().Get(HeaderFlagDoNotIntercept) == ""
}
}
r.mux.ServeHTTP(interceptor, req)
if interceptor.intercepted {
switch {
case interceptor.statusCode == http.StatusMethodNotAllowed:
// Set the Allow header
pattern := req.URL.Path
allowedMethods := r.getMethodsForPattern(pattern)
if len(allowedMethods) > 0 {
interceptor.ResponseWriter.Header().Set("Allow", strings.Join(allowedMethods, ", "))
}
r.handleStatus[http.StatusMethodNotAllowed].ServeHTTP(interceptor.ResponseWriter, req)
default:
if v, ok := r.handleStatus[interceptor.statusCode]; ok {
v.ServeHTTP(interceptor.ResponseWriter, req)
}
}
}
}
func (r *Router) handle(method, pattern string, handler http.HandlerFunc, docs ...Docs) {
if r.basePath != "" {
pattern = r.basePath + pattern
}
if pattern == "" {
pattern = "/"
} else if pattern[0] != '/' {
pattern = "/" + pattern
}
r.registerRoute(method, pattern, handler)
if r.openapiDocs {
r.registerDocs(method, pattern, docs...)
}
}
func (r *Router) registerRoute(method, pattern string, handler http.HandlerFunc) {
var (
fullPattern = method + " " + pattern
finalHandler http.Handler = handler
)
for i := len(r.middlewares) - 1; i >= 0; i-- {
finalHandler = r.middlewares[i](finalHandler)
}
rootRouter := r.rootParent()
rootRouter.mu.Lock()
defer rootRouter.mu.Unlock()
rootRouter.mux.Handle(fullPattern, finalHandler)
return
}
func (r *Router) registerDocs(method, pattern string, docs ...Docs) {
if len(docs) == 0 {
return
}
var (
stripPattern = strings.ReplaceAll(pattern, "{$}", "") //strip {$} from the pattern for the docs
doc = &docs[0]
)
rootRouter := r.rootParent()
rootRouter.mu.Lock()
defer rootRouter.mu.Unlock()
rootRouter.patternMap[stripPattern] = pattern
// Get or create RouteInfo for the pattern
pathItem, exists := rootRouter.openapi.Paths[pattern]
if !exists {
pathItem = PathItem{}
}
op := &Operation{
Tags: doc.Tags,
Summary: doc.Summary,
Description: doc.Description,
OperationID: fmt.Sprintf("%s%s", method, r.OperationID(stripPattern)),
Parameters: doc.Parameters,
RequestBody: doc.RequestBody,
Responses: doc.Responses,
Security: doc.Security,
}
// handle doc out
componentSchema, routeResponse := r.handleDocOut(doc.Out, rootRouter.openapi.Components.Schemas)
if componentSchema != nil {
for na, cs := range componentSchema {
if _, ex := rootRouter.openapi.Components.Schemas[na]; ex {
continue
}
rootRouter.openapi.Components.Schemas[na] = cs
}
}
if routeResponse != nil {
op.Responses = routeResponse
}
// handle doc in
componentSchema, requestBody := r.handleDocIn(doc.In, rootRouter.openapi.Components.Schemas)
if componentSchema != nil {
for na, cs := range componentSchema {
if _, ex := rootRouter.openapi.Components.Schemas[na]; ex {
continue
}
rootRouter.openapi.Components.Schemas[na] = cs
}
}
if requestBody != nil {
op.RequestBody = requestBody
}
rootRouter.openapi.Paths[stripPattern] = pathItem.SetMethod(method, op)
}
func (r *Router) rootParent() *Router {
if r.parent == nil {
return r
}
return r.parent.rootParent()
}
func addIfMissing[T comparable](slice []T, element T, prepend bool) []T {
if slices.Contains(slice, element) {
return slice
}
if prepend {
return append([]T{element}, slice...)
}
return append(slice, element)
}
func (r *Router) getMethodsForPattern(pattern string) []string {
rootRouter := r.rootParent()
rootRouter.mu.RLock()
defer rootRouter.mu.RUnlock()
var methods []string
if routeInfo, exists := rootRouter.openapi.Paths[pattern]; exists {
if routeInfo.Get != nil {
methods = addIfMissing(methods, http.MethodGet, false)
}
if routeInfo.Post != nil {
methods = addIfMissing(methods, http.MethodPost, false)
}
if routeInfo.Put != nil {
methods = addIfMissing(methods, http.MethodPut, false)
}
if routeInfo.Delete != nil {
methods = addIfMissing(methods, http.MethodDelete, false)
}
if routeInfo.Patch != nil {
methods = addIfMissing(methods, http.MethodPatch, false)
}
}
return methods
}
func (r *Router) registerOptionsHandler(strippedPattern string) {
rootRouter := r.rootParent()
rootRouter.mu.Lock()
defer rootRouter.mu.Unlock()
// Get the original pattern
pattern, exists := rootRouter.patternMap[strippedPattern]
if !exists {
pattern = strippedPattern // Fallback to strippedPattern if mapping is missing
}
// Get methods for the pattern
routeInfo, exists := rootRouter.openapi.Paths[pattern]
if !exists || len(routeInfo.Methods()) == 0 {
return
}
// Create the OPTIONS handler with the Allow header
methods := addIfMissing(routeInfo.Methods(), http.MethodOptions, true)
optionsHandler := func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Allow", strings.Join(methods, ", "))
w.WriteHeader(http.StatusNoContent)
}
// Register the handler
rootRouter.mux.HandleFunc("OPTIONS "+pattern, optionsHandler)
}
func (r *Router) OperationID(s string) string {
if s == "" || s == "/" {
s = "root"
}
parts := strings.Split(s, "/")
for i, part := range parts {
if part == "" {
continue
}
part = strings.TrimRight(strings.TrimLeft(part, "{"), "}")
parts[i] = strings.Title(part)
}
return strings.Join(parts, "")
}
func (r *Router) handleDocOut(do map[string]DocOut, schemas map[string]Schema) (map[string]Schema, map[string]Response) {
var (
componentSchemas map[string]Schema
routeResponse map[string]Response
)
if do == nil {
return nil, nil
}
for responseCode, docOut := range do {
var schema *Schema
if docOut.Object != nil {
obj := reflect.ValueOf(docOut.Object)
if obj.Kind() == reflect.Ptr {
obj = obj.Elem()
}
pType := "object"
name := obj.Type().Name()
schema = &Schema{
Ref: fmt.Sprintf("#/components/schemas/%s", name),
}
if _, ok := schemas[name]; !ok {
if obj.Kind() == reflect.Slice {
pType = "array"
elementType := obj.Type().Elem()
obj = reflect.New(elementType).Elem()
name = obj.Type().Name()
schema = &Schema{
Type: pType,
Items: &Schema{
Ref: fmt.Sprintf("#/components/schemas/%s", name),
},
}
}
properties := make(map[string]Schema)
for i := 0; i < obj.NumField(); i++ {
fieldType := obj.Type().Field(i)
fieldName := fieldType.Name
jsonTag := fieldType.Tag.Get("json")
if jsonTag != "" && jsonTag != "-" {
fieldName = strings.Split(jsonTag, ",")[0]
}
fieldKind := fieldType.Type.Kind()
var typeName string
switch fieldKind {
case reflect.String:
typeName = "string"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
typeName = "integer"
case reflect.Float32, reflect.Float64:
typeName = "number"
case reflect.Bool:
typeName = "boolean"
case reflect.Struct:
typeName = "object"
case reflect.Slice, reflect.Array:
typeName = "array"
default:
typeName = "string" // Default to string if unknown
}
properties[fieldName] = Schema{
Type: typeName,
}
}
if componentSchemas == nil {
componentSchemas = make(map[string]Schema)
}
componentSchemas[name] = Schema{
Type: pType,
Properties: properties,
}
}
} else {
// Handle nil docOut.Object by setting schema to nil
schema = nil
}
if routeResponse == nil {
routeResponse = make(map[string]Response)
}
mediaType := MediaType{}
if schema != nil {
mediaType.Schema = schema
}
routeResponse[responseCode] = Response{
Description: docOut.Description,
Content: map[string]MediaType{
docOut.ApplicationType: mediaType,
},
}
}
return componentSchemas, routeResponse
}
func (r *Router) handleDocIn(do map[string]DocIn, schemas map[string]Schema) (map[string]Schema, *RequestBody) {
var (
componentSchemas map[string]Schema
requestBody *RequestBody
)
if do == nil {
return nil, nil
}
for contentType, docIn := range do {
obj := reflect.ValueOf(docIn.Object)
if obj.Kind() == reflect.Ptr {
obj = obj.Elem()
}
name := obj.Type().Name()
if _, ok := schemas[name]; !ok {
properties := make(map[string]Schema)
for i := 0; i < obj.NumField(); i++ {
field := obj.Field(i)
fieldName := obj.Type().Field(i).Name
fieldType := field.Type().Name()
properties[fieldName] = Schema{
Type: fieldType,
}
}
if componentSchemas == nil {
componentSchemas = make(map[string]Schema)
}
componentSchemas[name] = Schema{
Type: "object",
Properties: properties,
}
}
if requestBody == nil {
requestBody = &RequestBody{
Content: make(map[string]MediaType),
}
}
requestBody.Content[contentType] = MediaType{
Schema: &Schema{
Ref: fmt.Sprintf("#/components/schemas/%s", name),
},
}
}
return componentSchemas, requestBody
}
// OpenAPI returns the root documentation tree
func (r *Router) OpenAPI() *OpenAPI {
rootRouter := r.rootParent()
return rootRouter.openapi
}