-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathop.go
324 lines (278 loc) · 7.9 KB
/
op.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
package openapi
import (
"net/http"
"reflect"
"sync"
kin "github.com/getkin/kin-openapi/openapi3"
)
// Parameter documents a query or path parameter.
type Parameter struct {
in string
name string
description string
required bool
typ string
dataType any
}
// PathParameter returns a path parameter with the given name and description.
func PathParameter(name, description string) Parameter {
return Parameter{
in: kin.ParameterInPath,
name: name,
description: description,
required: true,
dataType: "",
}
}
// QueryParameter returns a query parameter where the type will be resolved.
func QueryParameter(name, description string, typ any) Parameter {
return Parameter{
in: kin.ParameterInQuery,
name: name,
description: description,
dataType: typ,
}
}
// QueryParameterWithType returns a query parameter with the given type.
func QueryParameterWithType(name, description, typ string) Parameter {
return Parameter{
in: kin.ParameterInQuery,
name: name,
description: description,
typ: typ,
}
}
// HeaderParameter returns a header parameter with the given type.
func HeaderParameter(name, description string) Parameter {
return Parameter{
in: kin.ParameterInHeader,
name: name,
description: description,
}
}
// Response documents a request response.
type Response struct {
code int
description string
writes any
headers []string
mediaTypes []string
}
// ResponseOptFunc is an option function for configuration the response.
type ResponseOptFunc func(*Response)
// WithResponseHeader adds a header to the response.
func WithResponseHeader(name string) ResponseOptFunc {
return func(resp *Response) {
resp.headers = append(resp.headers, name)
}
}
// WithMediaTypes sets the specific media types for this response.
func WithMediaTypes(mediaTypes ...string) ResponseOptFunc {
return func(resp *Response) {
resp.mediaTypes = mediaTypes
}
}
const (
secTypeBearer = "bearer"
secTypeBasic = "basic"
secTypeAPIKey = "apiKey"
)
// Security represents a security configuration.
type Security struct {
// Type is the security type, valid values are "bearer", "auth" and "apiKey".
Type string
// BearerFormat is a hint to the client to identify how the bearer token is formatted.
// Bearer tokens are usually generated by an authorization server,
// so this information is primarily for documentation purposes.
BearerFormat string
// APIKeyName contains the name of the header, query or cookie parameter to be used.
APIKeyName string
// APIKeyIn is required for type "apiKey", valid values are "query", "header" or "cookie".
APIKeyIn string
}
// SecurityNone means no security is required for this endpoint.
var (
SecurityNone = Security{}
SecurityBearer = Security{
Type: secTypeBearer,
// BearerFormat is a hint to the client to identify how the bearer token is formatted.
// Bearer tokens are usually generated by an authorization server,
// so this information is primarily for documentation purposes.
BearerFormat: "",
}
SecurityBasic = Security{
Type: secTypeBasic,
}
)
// Operation documents a request.
type Operation struct {
id string
tags []string
doc string
params []Parameter
consumes []string
reads any
produces []string
returns []Response
security map[string]Security
}
// Merge merges the operation with the given operation.
func (o Operation) Merge(newOp Operation) Operation {
if newOp.id != "" {
o.id = newOp.id
}
if newOp.doc != "" {
o.doc = newOp.doc
}
if len(newOp.tags) > 0 {
o.tags = append([]string{}, o.tags...)
o.tags = append(o.tags, newOp.tags...)
}
if len(newOp.params) > 0 {
o.params = append([]Parameter{}, o.params...)
o.params = append(o.params, newOp.params...)
}
if len(newOp.consumes) > 0 {
o.consumes = append([]string{}, o.consumes...)
o.consumes = append(o.consumes, newOp.consumes...)
}
if newOp.reads != nil {
o.reads = newOp.reads
}
if len(newOp.produces) > 0 {
o.produces = append([]string{}, o.produces...)
o.produces = append(o.produces, newOp.produces...)
}
if len(newOp.returns) > 0 {
o.returns = append([]Response{}, o.returns...)
o.returns = append(o.returns, newOp.returns...)
}
if len(newOp.security) != 0 {
if o.security == nil {
o.security = map[string]Security{}
}
for k, v := range newOp.security {
o.security[k] = v
}
}
return o
}
// OpBuilder builds an operation. An operation describes a request route.
type OpBuilder struct {
op *Operation
}
// Op returns an op builder.
func Op() *OpBuilder {
return &OpBuilder{op: &Operation{}}
}
// ID set the operation id.
func (o *OpBuilder) ID(id string) *OpBuilder {
o.op.id = id
return o
}
// Doc sets the operation summary.
func (o *OpBuilder) Doc(doc string) *OpBuilder {
o.op.doc = doc
return o
}
// Tag appends the given tag to the operation.
func (o *OpBuilder) Tag(tag string) *OpBuilder {
o.op.tags = append(o.op.tags, tag)
return o
}
// Param appends the given parameter to the operation.
func (o *OpBuilder) Param(param Parameter) *OpBuilder {
o.op.params = append(o.op.params, param)
return o
}
// Params appends the given parameters to the operation.
func (o *OpBuilder) Params(params ...Parameter) *OpBuilder {
o.op.params = append(o.op.params, params...)
return o
}
// Consumes appends the given consumable media types to the operation.
func (o *OpBuilder) Consumes(mediaTypes ...string) *OpBuilder {
o.op.consumes = mediaTypes
return o
}
// Reads sets the request body type on the operation.
func (o *OpBuilder) Reads(obj any) *OpBuilder {
o.op.reads = obj
return o
}
// Produces appends the given producible media types to the operation.
func (o *OpBuilder) Produces(mediaTypes ...string) *OpBuilder {
o.op.produces = mediaTypes
return o
}
// Returns appends the given response to the operation.
func (o *OpBuilder) Returns(code int, description string, obj any, opts ...ResponseOptFunc) *OpBuilder {
resp := Response{
code: code,
description: description,
writes: obj,
}
for _, opt := range opts {
opt(&resp)
}
o.op.returns = append(o.op.returns, resp)
return o
}
// RequiresAuth requires authentication for this endpoint.
//
// The supported authentication types are "bearer", "basic" and "apiKey".
// The same name requires the same Security object, as all security schemes are registered under its name. The behavior for not
// following this directive is undetermined.
func (o *OpBuilder) RequiresAuth(name string, sec Security) *OpBuilder {
if o.op.security == nil {
o.op.security = map[string]Security{}
}
o.op.security[name] = sec
return o
}
// Build builds a middleware that will return an Operation when queried.
// In all other situations, the given handler is returned, effectively
// removing the middleware from the stack.
func (o *OpBuilder) Build() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
if _, ok := next.(opHandler); ok {
return opHandler{Op: *o.op}
}
return next
}
}
// BuildHandler builds a wrapper handler that contains an Operation.
//
// The operation is registered globally with the operation registrar
// as there is no other way to retrieve the operation from a handler func.
func (o *OpBuilder) BuildHandler() func(http.HandlerFunc) http.HandlerFunc {
return func(next http.HandlerFunc) http.HandlerFunc {
opReg.Register(next, *o.op)
return next
}
}
type opHandler struct {
Op Operation
}
func (o opHandler) ServeHTTP(_ http.ResponseWriter, _ *http.Request) {}
var opReg = registrar{}
type registrar struct {
mu sync.Mutex
ops map[reflect.Value]Operation
}
func (r *registrar) Register(i any, op Operation) {
r.mu.Lock()
defer r.mu.Unlock()
if r.ops == nil {
r.ops = map[reflect.Value]Operation{}
}
v := reflect.ValueOf(i)
r.ops[v] = op
}
func (r *registrar) Op(i any) (Operation, bool) {
r.mu.Lock()
defer r.mu.Unlock()
v := reflect.ValueOf(i)
op, ok := r.ops[v]
return op, ok
}