-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserverOptions.go
More file actions
141 lines (129 loc) · 3.95 KB
/
serverOptions.go
File metadata and controls
141 lines (129 loc) · 3.95 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
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
package shiftapi
import (
"reflect"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3gen"
)
// Info describes the API and is rendered into the OpenAPI spec's info object.
type Info struct {
Title string
Description string
TermsOfService string
Contact *Contact
License *License
Version string
}
// Contact describes the API contact information.
type Contact struct {
Name string
URL string
Email string
}
// License describes the API license.
type License struct {
Name string
URL string
}
// ExternalDocs links to external documentation.
type ExternalDocs struct {
Description string
URL string
}
// WithInfo configures the API metadata that appears in the OpenAPI spec
// and documentation UI.
//
// api := shiftapi.New(shiftapi.WithInfo(shiftapi.Info{
// Title: "My API",
// Version: "1.0.0",
// }))
func WithInfo(info Info) apiOptionFunc {
return func(api *API) {
api.spec.Info = &openapi3.Info{
Title: info.Title,
Description: info.Description,
TermsOfService: info.TermsOfService,
Version: info.Version,
}
if info.Contact != nil {
api.spec.Info.Contact = &openapi3.Contact{
Name: info.Contact.Name,
URL: info.Contact.URL,
Email: info.Contact.Email,
}
}
if info.License != nil {
api.spec.Info.License = &openapi3.License{
Name: info.License.Name,
URL: info.License.URL,
}
}
}
}
// WithMaxUploadSize sets the maximum memory used for parsing multipart form data.
// The default is 32 MB.
func WithMaxUploadSize(size int64) apiOptionFunc {
return func(api *API) {
api.maxUploadSize = size
}
}
// WithBadRequestError customizes the 400 Bad Request response returned when
// the framework cannot parse the request (malformed JSON, invalid query
// parameters, invalid form data). The function receives the parse error and
// returns the value to serialize as the response body. T's type determines the
// BadRequestError schema in the OpenAPI spec.
//
// api := shiftapi.New(
// shiftapi.WithBadRequestError(func(err error) *MyBadRequest {
// return &MyBadRequest{Code: "BAD_REQUEST", Message: err.Error()}
// }),
// )
func WithBadRequestError[T any](fn func(error) T) apiOptionFunc {
return func(api *API) {
api.badRequestFn = func(err error) any { return fn(err) }
registerErrorSchema[T](api, "BadRequestError")
}
}
// WithInternalServerError customizes the 500 Internal Server Error response
// returned when a handler returns an error that doesn't match any registered
// error type. The function receives the unhandled error and returns the value
// to serialize as the response body. T's type determines the InternalServerError
// schema in the OpenAPI spec.
//
// api := shiftapi.New(
// shiftapi.WithInternalServerError(func(err error) *MyServerError {
// log.Error("unhandled", "err", err)
// return &MyServerError{Code: "INTERNAL_ERROR", Message: "internal server error"}
// }),
// )
func WithInternalServerError[T any](fn func(error) T) apiOptionFunc {
return func(api *API) {
api.internalServerFn = func(err error) any { return fn(err) }
registerErrorSchema[T](api, "InternalServerError")
}
}
// registerErrorSchema generates and registers a component schema for the given type.
func registerErrorSchema[T any](api *API, name string) {
t := reflect.TypeFor[T]()
for t.Kind() == reflect.Pointer {
t = t.Elem()
}
gen := openapi3gen.NewGenerator(
openapi3gen.SchemaCustomizer(api.schemaCustomizer),
)
schema, err := gen.GenerateSchemaRef(t)
if err != nil {
panic("shiftapi: failed to generate " + name + " schema: " + err.Error())
}
api.spec.Components.Schemas[name] = &openapi3.SchemaRef{
Value: schema.Value,
}
}
// WithExternalDocs links to external documentation.
func WithExternalDocs(docs ExternalDocs) apiOptionFunc {
return func(api *API) {
api.spec.ExternalDocs = &openapi3.ExternalDocs{
Description: docs.Description,
URL: docs.URL,
}
}
}