-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
84 lines (73 loc) · 2.37 KB
/
options.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
package expose
import (
"net/http"
"reflect"
"github.com/getkin/kin-openapi/openapi3"
)
// WithSwaggerUI, adds a SwaggerUI handler at the provided `path`
func WithSwaggerUI(path string) HandlerOption {
return func(settings *handlerSettings) {
settings.swaggerUIPath = path
}
}
// WithErrorHandler registers a custom [ErrorHandler]
func WithErrorHandler(h ErrorHandler) HandlerOption {
return func(settings *handlerSettings) {
settings.errorHandler = h
}
}
// WithSwaggerJSONPath overrides the default path (/swagger.json), where the spec is served
func WithSwaggerJSONPath(path string) HandlerOption {
return func(settings *handlerSettings) {
settings.swaggerPath = path
}
}
// WithEncodings registers additional encodings.
// Encodings are selected based on the provided "Content-Type" and "Accept" headers
func WithEncodings(encodings ...Encoding) HandlerOption {
return func(settings *handlerSettings) {
for _, enc := range encodings {
settings.encoding[enc.MimeType] = enc
}
}
}
// WithDefaultSpec allows you to define a base spec.
// The handler fills this base spec with the operations and schemas
// reflected from the exposed functions.
func WithDefaultSpec(spec *openapi3.T) HandlerOption {
return func(settings *handlerSettings) {
settings.defaultSpec = *spec
}
}
// WithPathPrefix defines the path prefix of the handler.
// When using it with WithSwaggerUI, make sure that your `Servers` section in
// the default spec [WithDefaultSpec] adds this prefix as well
func WithPathPrefix(prefixPath string) HandlerOption {
return func(settings *handlerSettings) {
settings.basePath = prefixPath
settings.middlewares = append([]Middleware{
func(next http.Handler) http.Handler {
return http.StripPrefix(prefixPath, next)
},
}, settings.middlewares...)
}
}
// WithReflection sets options for the schema reflection
func WithReflection(opts ...reflectSpecOpt) HandlerOption {
return func(settings *handlerSettings) {
for _, opt := range opts {
if opt == nil {
continue
}
opt(settings.reflectSettings)
}
}
}
// WithSchemaIdentifier sets an alternative [SchemaIdentifier]. Default: [DefaultSchemaIdentifier]
func WithSchemaIdentifier(namer SchemaIdentifier) reflectSpecOpt {
return func(s *reflectSettings) {
s.typeNamer = namer
}
}
// TypeNamers are used to generate a schema identifier for a go type
type SchemaIdentifier func(t reflect.Type) string