-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
555 lines (517 loc) · 16.1 KB
/
api.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
package chimera
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"github.com/go-chi/chi/v5"
"github.com/invopop/jsonschema"
"github.com/swaggest/swgui/v5emb"
)
var (
default500Error = []byte("Unknown error occurred")
)
// APIError is an error that can be converted to a response
type APIError struct {
StatusCode int
Body []byte
Header http.Header
}
// Error returns the string representation of the error
func (a APIError) Error() string {
if a.StatusCode < 1 {
a.StatusCode = 500
}
return fmt.Sprintf("%v error: %s", a.StatusCode, a.Body)
}
// Nil is an empty struct that is designed to represent "nil"
// and is typically used to denote that a request/response
// has no body or parameters depending on context
type Nil struct{}
// API is a collection of routes and middleware with an associated OpenAPI spec
type API struct {
openAPISpec OpenAPI
router *chi.Mux
routes []*route
middleware []MiddlewareFunc
subAPIs []*API
basePath string
parent *API
staticPaths map[string]string
}
// OpenAPISpec returns the underlying OpenAPI structure for this API
func (a *API) OpenAPISpec() *OpenAPI {
return &a.openAPISpec
}
// ServeHTTP serves to implement support for the standard library
func (a *API) ServeHTTP(w http.ResponseWriter, req *http.Request) {
customWriter := httpResponseWriter{
writer: w,
}
a.router.ServeHTTP(&customWriter, req)
write(&customWriter, w, req)
}
func writeError(e error, w http.ResponseWriter) {
if err, ok := e.(APIError); ok {
for k, vals := range err.Header {
for _, v := range vals {
w.Header().Set(k, v)
}
}
if err.StatusCode != 0 {
w.WriteHeader(err.StatusCode)
} else {
w.WriteHeader(500)
}
w.Write(err.Body)
} else {
w.WriteHeader(500)
w.Write(default500Error)
}
}
func write(customWriter *httpResponseWriter, w http.ResponseWriter, req *http.Request) {
if customWriter.respError != nil {
writeError(customWriter.respError, customWriter.writer)
} else {
// TODO: maybe allow global default response codes for methods?
if customWriter.response != nil && !reflect.ValueOf(customWriter.response).IsNil() {
head := ResponseHead{
StatusCode: customWriter.route.context.responseCode,
Headers: customWriter.Header(),
}
err := customWriter.response.WriteHead(&head)
if err != nil {
customWriter.writer.WriteHeader(500)
customWriter.Write(default500Error)
} else {
customWriter.WriteHeader(head.StatusCode)
customWriter.response.WriteBody(customWriter.Write)
}
} else {
if customWriter.route != nil && customWriter.route.context.responseCode != 0 {
w.WriteHeader(customWriter.route.context.responseCode)
} else {
switch req.Method {
case http.MethodGet:
case http.MethodPut:
case http.MethodPatch:
w.WriteHeader(200)
case http.MethodPost:
w.WriteHeader(201)
case http.MethodOptions:
case http.MethodDelete:
w.WriteHeader(204)
}
}
}
}
}
// Start uses http.ListenAndServe to start serving requests from addr
func (a *API) Start(addr string) error {
return http.ListenAndServe(addr, a)
}
// NewAPI returns an initialized API object
func NewAPI() *API {
api := API{
router: chi.NewRouter(),
openAPISpec: OpenAPI{
OpenAPI: "3.1.0",
Paths: make(map[string]Path),
Info: Info{
Version: "v0.0.0",
Title: "API",
},
Servers: make([]Server, 0),
Components: &Components{
Schemas: make(map[string]jsonschema.Schema),
},
},
}
return &api
}
// addRoute creates a route based on method, path, handler, etc.
func addRoute[ReqPtr RequestReaderPtr[Req], Req any, RespPtr ResponseWriterPtr[Resp], Resp any](api *API, method, path string, handler HandlerFunc[ReqPtr, Req, RespPtr, Resp]) Route {
if path == "" || path[0] != '/' {
path = "/" + path
}
reqSchema := ReqPtr(new(Req)).OpenAPIRequestSpec()
operation := Operation{
RequestSpec: &reqSchema,
Responses: RespPtr(new(Resp)).OpenAPIResponsesSpec(),
}
if reqSchema.RequestBody != nil {
for k, v := range reqSchema.RequestBody.Content {
if v.Schema != nil {
// TODO: maybe implement a global schema resolver?
// otherwise some classes with the same name may clobber
// eachother in the final spec
standardizedSchemas(v.Schema, api.openAPISpec.Components.Schemas)
}
reqSchema.RequestBody.Content[k] = v
}
}
if operation.Responses != nil {
for c, r := range operation.Responses {
for k, v := range r.Content {
if v.Schema != nil {
standardizedSchemas(v.Schema, api.openAPISpec.Components.Schemas)
}
r.Content[k] = v
}
for k, v := range r.Headers {
if v.Schema != nil {
standardizedSchemas(v.Schema, api.openAPISpec.Components.Schemas)
}
r.Headers[k] = v
}
operation.Responses[c] = r
}
}
if reqSchema.Parameters != nil {
for i, p := range reqSchema.Parameters {
if p.Schema != nil {
standardizedSchemas(p.Schema, api.openAPISpec.Components.Schemas)
}
reqSchema.Parameters[i] = p
}
}
pathSchema := Path{}
if p, ok := api.openAPISpec.Paths[path]; ok {
pathSchema = p
}
defaultCode := ""
responseCode := 0
switch method {
case http.MethodGet:
if r, ok := operation.Responses[""]; ok {
operation.Responses["200"] = r
defaultCode = "200"
responseCode = 200
delete(operation.Responses, "")
} else if len(operation.Responses) == 0 {
defaultCode = "200"
responseCode = 200
}
pathSchema.Get = &operation
case http.MethodPost:
if r, ok := operation.Responses[""]; ok {
operation.Responses["201"] = r
defaultCode = "201"
responseCode = 201
delete(operation.Responses, "")
} else if len(operation.Responses) == 0 {
defaultCode = "201"
responseCode = 201
}
pathSchema.Post = &operation
case http.MethodDelete:
if r, ok := operation.Responses[""]; ok {
operation.Responses["204"] = r
defaultCode = "204"
responseCode = 204
delete(operation.Responses, "")
} else if len(operation.Responses) == 0 {
defaultCode = "204"
responseCode = 204
}
pathSchema.Delete = &operation
case http.MethodOptions:
if r, ok := operation.Responses[""]; ok {
operation.Responses["204"] = r
defaultCode = "204"
responseCode = 204
delete(operation.Responses, "")
} else if len(operation.Responses) == 0 {
defaultCode = "204"
responseCode = 204
}
pathSchema.Options = &operation
case http.MethodPatch:
if r, ok := operation.Responses[""]; ok {
operation.Responses["200"] = r
defaultCode = "200"
responseCode = 200
delete(operation.Responses, "")
} else if len(operation.Responses) == 0 {
defaultCode = "200"
responseCode = 200
}
pathSchema.Patch = &operation
case http.MethodPut:
if r, ok := operation.Responses[""]; ok {
operation.Responses["200"] = r
defaultCode = "200"
responseCode = 200
delete(operation.Responses, "")
} else if len(operation.Responses) == 0 {
defaultCode = "200"
responseCode = 200
}
pathSchema.Put = &operation
}
api.openAPISpec.Paths[api.basePath+path] = pathSchema
route := route{
operationSpec: &operation,
defaultCode: defaultCode,
context: &routeContext{
responseCode: responseCode,
method: method,
path: path,
},
api: api,
}
chiHandler := (func(w http.ResponseWriter, r *http.Request) {
request := ReqPtr(new(Req))
customWriter := w.(*httpResponseWriter)
customWriter.route = &route
customWriter.respError = request.ReadRequest(r)
if customWriter.respError != nil {
return
}
customWriter.response, customWriter.respError = handler(request)
})
route.handler = chiHandler
api.routes = append(api.routes, &route)
rebuildAPI(api)
return Route{
route: &route,
}
}
func rebuildAPI(api *API) {
a := api
for ; a.parent != nil; a = api.parent {
}
a.rebuildRouter()
}
// Get adds a "GET" route to the API object which will invode the handler function on route match
// it also returns the Route object to allow easy updates of the Operation spec
func Get[ReqPtr RequestReaderPtr[Req], Req any, RespPtr ResponseWriterPtr[Resp], Resp any](api *API, path string, handler HandlerFunc[ReqPtr, Req, RespPtr, Resp]) Route {
return addRoute(api, http.MethodGet, path, handler)
}
// Post adds a "POST" route to the API object which will invode the handler function on route match
// it also returns the Route object to allow easy updates of the Operation spec
func Post[ReqPtr RequestReaderPtr[Req], Req any, RespPtr ResponseWriterPtr[Resp], Resp any](api *API, path string, handler HandlerFunc[ReqPtr, Req, RespPtr, Resp]) Route {
return addRoute(api, http.MethodPost, path, handler)
}
// Put adds a "PUT" route to the API object which will invode the handler function on route match
// it also returns the Route object to allow easy updates of the Operation spec
func Put[ReqPtr RequestReaderPtr[Req], Req any, RespPtr ResponseWriterPtr[Resp], Resp any](api *API, path string, handler HandlerFunc[ReqPtr, Req, RespPtr, Resp]) Route {
return addRoute(api, http.MethodPut, path, handler)
}
// Patch adds a "PATCH" route to the API object which will invode the handler function on route match
// it also returns the Route object to allow easy updates of the Operation spec
func Patch[ReqPtr RequestReaderPtr[Req], Req any, RespPtr ResponseWriterPtr[Resp], Resp any](api *API, path string, handler HandlerFunc[ReqPtr, Req, RespPtr, Resp]) Route {
return addRoute(api, http.MethodPatch, path, handler)
}
// Delete adds a "DELETE" route to the API object which will invode the handler function on route match
// it also returns the Route object to allow easy updates of the Operation spec
func Delete[ReqPtr RequestReaderPtr[Req], Req any, RespPtr ResponseWriterPtr[Resp], Resp any](api *API, path string, handler HandlerFunc[ReqPtr, Req, RespPtr, Resp]) Route {
return addRoute(api, http.MethodDelete, path, handler)
}
// Options adds a "OPTIONS" route to the API object which will invode the handler function on route match
// it also returns the Route object to allow easy updates of the Operation spec
func Options[ReqPtr RequestReaderPtr[Req], Req any, RespPtr ResponseWriterPtr[Resp], Resp any](api *API, path string, handler HandlerFunc[ReqPtr, Req, RespPtr, Resp]) Route {
return addRoute(api, http.MethodOptions, path, handler)
}
// Idk what trace even does, do people actually use this?
// func Trace[ReqPtr RequestReaderPtr[Req], Req any, RespPtr ResponseWriterPtr[Resp], Resp any](api *API, path string, handler func(ReqPtr) (RespPtr, error)) Route {
// return addRoute(api, http.MethodTrace, path, handler)
// }
// Static adds support for serving static content from a directory, this route is hidden from the OpenAPI spec
func (a *API) Static(apiPath, filesPath string) {
if len(apiPath) == 0 {
apiPath = "/"
}
if apiPath[0] != '/' {
apiPath = "/" + apiPath
}
if apiPath[len(apiPath)-1] != '/' {
apiPath += "/"
}
a.staticPaths[apiPath+"*"] = filesPath
rebuildAPI(a)
}
// Use adds middleware to the API
func (a *API) Use(middleware ...MiddlewareFunc) {
a.middleware = append(a.middleware, middleware...)
rebuildAPI(a)
}
// Group creates a sub-API with seperate middleware and routes using a base path.
// The middleware of the parent API is always evaluated first and any route collisions
// are handled by chi directly
func (a *API) Group(basePath string) *API {
for _, sub := range a.subAPIs {
if sub.basePath == a.basePath+basePath {
return a
}
}
newSub := NewAPI()
a.Mount(basePath, newSub)
return newSub
}
// Mount adds an API as a child based on route. It is like a reverse Group()
func (a *API) Mount(basePath string, subAPI *API) {
subAPI.basePath = basePath
subAPI.parent = a
a.subAPIs = append(a.subAPIs, subAPI)
rebuildAPI(a)
}
// rebuildRouter rebuilds the entire router. This is not particularly efficient
// but at least this allows us to specify middleware/routes/groups in any order
// while still having a guaranteed final order
func (a *API) rebuildRouter() chi.Router {
var schema []byte
apiSpec := OpenAPI{
OpenAPI: "3.1.0",
Paths: make(map[string]Path),
Info: Info{
Version: "v0.0.0",
Title: "API",
},
Servers: make([]Server, 0),
Components: &Components{
Schemas: make(map[string]jsonschema.Schema),
},
}
apiSpec.Merge(a.openAPISpec)
if a.parent == nil {
a.openAPISpec = apiSpec
}
router := chi.NewRouter()
if a.parent == nil {
router.MethodFunc(http.MethodGet, "/openapi.json", func(w http.ResponseWriter, r *http.Request) {
if schema == nil {
schema, _ = json.Marshal(apiSpec)
}
w.Write(schema)
})
router.Handle("/docs*",
v5emb.New(
apiSpec.Info.Title,
"/openapi.json",
"/docs",
),
)
}
for _, sub := range a.subAPIs {
if sub.basePath == "" || sub.basePath[0] != '/' {
sub.basePath = "/" + sub.basePath
}
router.Mount(sub.basePath, sub.rebuildRouter())
apiSpec.Merge(sub.openAPISpec)
}
for apiPath, filesPath := range a.staticPaths {
fileServer := http.FileServer(http.Dir(filesPath))
router.Get(apiPath+"*", http.StripPrefix(apiPath, fileServer).ServeHTTP)
}
middlewareChain := make([]MiddlewareFunc, 0)
for api := a; api != nil; api = api.parent {
middlewareChain = append(api.middleware, middlewareChain...)
}
handler := func(w *httpResponseWriter, r *http.Request) (ResponseWriter, error) {
w.route.handler(w, r)
return w.response, w.respError
}
for i := len(middlewareChain) - 1; i >= 0; i-- {
h := handler
middleware := middlewareChain[i]
// switch middleware := middlewareChain[i].(type) {
// case MiddlewareFunc:
handler = (func(w *httpResponseWriter, r *http.Request) (ResponseWriter, error) {
wrapped := middlewareWrapper{
writer: w,
handler: h,
}
return middleware(r, w.route.context, wrapped.Next)
})
// case HttpMiddlewareFunc:
// next := func(w http.ResponseWriter, req *http.Request) {
// writer := w.(*httpResponseWriter)
// writer.response, writer.responseError = handler(w, req)
// }
// handler = (func(w *httpResponseWriter, r *http.Request) (ResponseWriter, error) {
// fake := Response{}
// return middleware(r, w.route.context, wrapped.Next)
// })
// }
// if true {
// middleware := (func(next http.Handler) http.Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.Write(nil)
// // fake w
// next.ServeHTTP(&Response{}, r)
// w.Write(nil)
// })
// })
// h := handler
// handler = (func(writer *httpResponseWriter, req *http.Request) (ResponseWriter, error) {
// next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// wr, ok := w.(*httpResponseWriter)
// if !ok {
// wr = &httpResponseWriter{
// writer: w,
// route: writer.route,
// }
// wr.response, wr.respError = h(wr, r)
// write(wr, w, r)
// } else {
// wr.response, wr.respError = h(wr, r)
// if wr.dirty {
// write(wr, w, r)
// }
// }
// })
// middleware(next).ServeHTTP(writer, req)
// if writer.dirty {
// write(writer, writer.writer, req)
// }
// return writer.response, writer.respError
// })
// }
}
for _, route := range a.routes {
if route.hidden {
toDelete := make([]string, 0)
for path, obj := range apiSpec.Paths {
if obj.Patch == route.operationSpec {
obj.Patch = nil
}
if obj.Get == route.operationSpec {
obj.Get = nil
}
if obj.Put == route.operationSpec {
obj.Put = nil
}
if obj.Post == route.operationSpec {
obj.Post = nil
}
if obj.Delete == route.operationSpec {
obj.Delete = nil
}
if obj.Options == route.operationSpec {
obj.Options = nil
}
if obj.Post == nil && obj.Patch == nil && obj.Put == nil && obj.Get == nil && obj.Delete == nil && obj.Options == nil {
toDelete = append(toDelete, path)
}
}
for _, p := range toDelete {
delete(apiSpec.Paths, p)
}
}
if route.context.path == "" || route.context.path[0] != '/' {
route.context.path = "/" + route.context.path
}
if len(middlewareChain) > 0 {
router.MethodFunc(route.context.method, route.context.path, func(w http.ResponseWriter, r *http.Request) {
writer := w.(*httpResponseWriter)
writer.route = route
writer.response, writer.respError = handler(writer, r)
})
} else {
router.MethodFunc(route.context.method, route.context.path, route.handler)
}
}
a.router = router
return router
}