-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute_config.go
More file actions
85 lines (71 loc) · 2.1 KB
/
route_config.go
File metadata and controls
85 lines (71 loc) · 2.1 KB
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
package echopen
import (
"fmt"
"strings"
"github.com/labstack/echo/v4"
v310 "github.com/richjyoung/echopen/openapi/v3.1.0"
)
type RouteConfigFunc func(*RouteWrapper) *RouteWrapper
func WithMiddlewares(m ...echo.MiddlewareFunc) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
rw.Middlewares = append(rw.Middlewares, m...)
return rw
}
}
func WithTags(tags ...string) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
for _, tag := range tags {
if rw.API.Spec.GetTagByName(tag) == nil {
panic(fmt.Sprintf("echopen: tag '%s' not registered", tag))
}
}
rw.Operation.AddTags(tags...)
return rw
}
}
func WithOperationID(id string) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
rw.Operation.OperationID = id
return rw
}
}
func WithDescription(desc string) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
rw.Operation.Description = strings.TrimSpace(desc)
return rw
}
}
func WithSummary(sum string) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
rw.Operation.Summary = strings.TrimSpace(sum)
return rw
}
}
func WithDeprecated() RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
rw.Operation.Deprecated = true
return rw
}
}
func WithOptionalSecurity() RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
rw.Operation.AddSecurityRequirement(&v310.SecurityRequirement{})
return rw
}
}
// WithSecurityRequirement attaches a requirement to a route that the matching security scheme is fulfilled.
// Attaches middleware that adds the security scheme value and scopes to the context at security.<name> and security.<name>.scopes
func WithSecurityRequirement(name string, scopes []string) RouteConfigFunc {
return func(rw *RouteWrapper) *RouteWrapper {
// Lookup the matching scheme
scheme := rw.API.Spec.GetComponents().GetSecurityScheme(name)
if scheme == nil {
panic("echopen: security scheme not registered")
}
// Add the requirement to the operation definition
rw.Operation.AddSecurityRequirement(&v310.SecurityRequirement{
name: scopes,
})
return rw
}
}