-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptions_openapi.go
More file actions
58 lines (51 loc) · 1.6 KB
/
options_openapi.go
File metadata and controls
58 lines (51 loc) · 1.6 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
package frame
import (
"context"
"fmt"
"io/fs"
"strings"
"github.com/pitabwire/frame/openapi"
)
// WithOpenAPISpec registers a single OpenAPI spec with the service.
// Content should be provided from compile-time embedded assets.
func WithOpenAPISpec(name, filename string, content []byte) Option {
return func(_ context.Context, s *Service) {
if s.openapiRegistry == nil {
s.openapiRegistry = openapi.NewRegistry()
}
s.openapiRegistry.Add(openapi.Spec{Name: name, Filename: filename, Content: content})
}
}
// WithOpenAPISpecsFromFS registers all .json OpenAPI specs from an embedded FS directory.
// Use //go:embed to provide the fs.FS at compile time.
func WithOpenAPISpecsFromFS(f fs.FS, dir string) Option {
return func(ctx context.Context, s *Service) {
if s.openapiRegistry == nil {
s.openapiRegistry = openapi.NewRegistry()
}
if err := openapi.RegisterFromFS(s.openapiRegistry, f, dir); err != nil {
s.AddStartupError(fmt.Errorf("openapi register from fs: %w", err))
s.Log(ctx).Error(err.Error())
}
}
}
// WithOpenAPIBasePath sets the base path used to serve OpenAPI specs.
// The default is /debug/frame/openapi.
func WithOpenAPIBasePath(path string) Option {
return func(_ context.Context, s *Service) {
s.openapiBasePath = normalizeOpenAPIBasePath(path)
}
}
func (s *Service) OpenAPIRegistry() *openapi.Registry {
return s.openapiRegistry
}
func normalizeOpenAPIBasePath(path string) string {
path = strings.TrimSpace(path)
if path == "" {
return defaultOpenAPIBasePath
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return strings.TrimSuffix(path, "/")
}