-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions_swagger.go
More file actions
97 lines (85 loc) · 2.74 KB
/
Copy pathoptions_swagger.go
File metadata and controls
97 lines (85 loc) · 2.74 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
package servex
import "os"
// SwaggerAutoConfig holds configuration for automatic Swagger UI registration via server options.
type SwaggerAutoConfig struct {
// Enabled determines whether Swagger UI is automatically registered.
Enabled bool
// Path is the URL path where Swagger UI will be served.
// Default: "/swagger"
Path string
// SpecData is the raw OpenAPI specification content (JSON or YAML).
// Either SpecData or SpecFile must be provided when Enabled is true.
SpecData []byte
// SpecFile is the path to an OpenAPI specification file on disk.
// The file is read at server startup. Either SpecData or SpecFile must be provided.
SpecFile string
// Options are additional SwaggerOption functions applied to the handler.
Options []SwaggerOption
}
// WithSwaggerUI enables Swagger UI with the given spec data.
// The spec is served at "/swagger" by default (change with WithSwaggerUIPath).
//
// Example:
//
// spec, _ := os.ReadFile("openapi.yaml")
// server, _ := servex.NewServer(
// servex.WithSwaggerUI(spec),
// )
func WithSwaggerUI(specData []byte, opts ...SwaggerOption) Option {
return func(o *Options) {
o.Swagger.Enabled = true
o.Swagger.SpecData = specData
o.Swagger.Options = append(o.Swagger.Options, opts...)
}
}
// WithSwaggerUIFile enables Swagger UI, loading the OpenAPI spec from a file at server startup.
// The file is read once when the server is created.
//
// Example:
//
// server, _ := servex.NewServer(
// servex.WithSwaggerUIFile("./openapi.yaml"),
// )
func WithSwaggerUIFile(filePath string, opts ...SwaggerOption) Option {
return func(o *Options) {
o.Swagger.Enabled = true
o.Swagger.SpecFile = filePath
o.Swagger.Options = append(o.Swagger.Options, opts...)
}
}
// WithSwaggerUIPath sets the URL path for the auto-registered Swagger UI.
// Default: "/swagger"
//
// Example:
//
// server, _ := servex.NewServer(
// servex.WithSwaggerUI(spec),
// servex.WithSwaggerUIPath("/api-docs"),
// )
func WithSwaggerUIPath(path string) Option {
return func(o *Options) {
o.Swagger.Path = path
}
}
// WithSwaggerUIConfig sets the full Swagger UI configuration.
func WithSwaggerUIConfig(cfg SwaggerAutoConfig) Option {
return func(o *Options) {
o.Swagger = cfg
}
}
// isActive returns true if Swagger UI should be registered.
// Active when Enabled is explicitly set or when spec data/file is provided.
func (cfg SwaggerAutoConfig) isActive() bool {
return cfg.Enabled || len(cfg.SpecData) > 0 || cfg.SpecFile != ""
}
// loadSwaggerSpec reads the spec file into SpecData if SpecFile is set.
func (cfg *SwaggerAutoConfig) loadSwaggerSpec() error {
if cfg.SpecFile != "" && len(cfg.SpecData) == 0 {
data, err := os.ReadFile(cfg.SpecFile)
if err != nil {
return err
}
cfg.SpecData = data
}
return nil
}