-
Notifications
You must be signed in to change notification settings - Fork 51
/
oas.gen.go
193 lines (163 loc) · 5.51 KB
/
oas.gen.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
// Package oas provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen version v1.8.3 DO NOT EDIT.
package oas
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"fmt"
"net/http"
"net/url"
"path"
"strings"
externalRef0 "github.com/cam-inc/viron/packages/golang/routes/components"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-chi/chi/v5"
)
const (
JwtScopes = "jwt.Scopes"
)
// ServerInterface represents all server handlers.
type ServerInterface interface {
// get oas
// (GET /oas)
GetOas(w http.ResponseWriter, r *http.Request)
}
// ServerInterfaceWrapper converts contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
HandlerMiddlewares []MiddlewareFunc
}
type MiddlewareFunc func(http.HandlerFunc) http.HandlerFunc
// GetOas operation middleware
func (siw *ServerInterfaceWrapper) GetOas(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, JwtScopes, []string{""})
var handler = func(w http.ResponseWriter, r *http.Request) {
siw.Handler.GetOas(w, r)
}
for _, middleware := range siw.HandlerMiddlewares {
handler = middleware(handler)
}
handler(w, r.WithContext(ctx))
}
// Handler creates http.Handler with routing matching OpenAPI spec.
func Handler(si ServerInterface) http.Handler {
return HandlerWithOptions(si, ChiServerOptions{})
}
type ChiServerOptions struct {
BaseURL string
BaseRouter chi.Router
Middlewares []MiddlewareFunc
}
// HandlerFromMux creates http.Handler with routing matching OpenAPI spec based on the provided mux.
func HandlerFromMux(si ServerInterface, r chi.Router) http.Handler {
return HandlerWithOptions(si, ChiServerOptions{
BaseRouter: r,
})
}
func HandlerFromMuxWithBaseURL(si ServerInterface, r chi.Router, baseURL string) http.Handler {
return HandlerWithOptions(si, ChiServerOptions{
BaseURL: baseURL,
BaseRouter: r,
})
}
// HandlerWithOptions creates http.Handler with additional options
func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handler {
r := options.BaseRouter
if r == nil {
r = chi.NewRouter()
}
wrapper := ServerInterfaceWrapper{
Handler: si,
HandlerMiddlewares: options.Middlewares,
}
r.Group(func(r chi.Router) {
r.Get(options.BaseURL+"/oas", wrapper.GetOas)
})
return r
}
// Base64 encoded, gzipped, json marshaled Swagger object
var swaggerSpec = []string{
"H4sIAAAAAAAC/2RSwY7TMBD9lWrOoQndm09wQqsFbaUKLlUPrneauHJs45l020Y58AXc4C/4BH4G7X+g",
"cdJSsafYbzxvXua9HkxoY/DomUD1QGi6ZPm0Mg22mKH9M8tn58Jzvts2OmtsBnXHTUj2rNkG/zk5UNAw",
"R1Jl6YLRrgnE6q6qqpJs7a2HAsiEOBLraJU2BolAwfvl/Wy6DMNQAJ8igoIgExYjZP0uSN8Tkkk2ysj8",
"gmY62hlFNHZnTZZCUABbdkLx7mBT8KWz21nQUjhgorG3mlfzt1DA8Y3uOMgmHDLKjIQUgyf8qLfoHvAE",
"CpwcobiWvmjX4Vg6yBEGIWK9dZkh6hrTSPW1Q+KlrqfXUsk8GV/Z84STPeMN/yd9jNeeVh+Xl7ax/I/P",
"dCmhzwNEw1W6JZ6UW2LZfEh8I2gaKmBeb4jodbSg4G5ezRdQQNTcZKdKWZzqoUZ+bcBjRC/urW4NmP35",
"9uvl+4+X3z8hM6eM3j+Bgg/Ij9mHi9BMvagq+ZjgGf2YrSg5y33lnmRUDySx1HLCoxa3QPU3Ydnu0Ux/",
"85/GBxDwkm5Q6ynX682wKYC6ttVJ1lEjTylhXROoteQLNmMkR6QHr9sxmwTDZvgbAAD//yeJaxNDAwAA",
}
// GetSwagger returns the content of the embedded swagger specification file
// or error if failed to decode
func decodeSpec() ([]byte, error) {
zipped, err := base64.StdEncoding.DecodeString(strings.Join(swaggerSpec, ""))
if err != nil {
return nil, fmt.Errorf("error base64 decoding spec: %s", err)
}
zr, err := gzip.NewReader(bytes.NewReader(zipped))
if err != nil {
return nil, fmt.Errorf("error decompressing spec: %s", err)
}
var buf bytes.Buffer
_, err = buf.ReadFrom(zr)
if err != nil {
return nil, fmt.Errorf("error decompressing spec: %s", err)
}
return buf.Bytes(), nil
}
var rawSpec = decodeSpecCached()
// a naive cached of a decoded swagger spec
func decodeSpecCached() func() ([]byte, error) {
data, err := decodeSpec()
return func() ([]byte, error) {
return data, err
}
}
// Constructs a synthetic filesystem for resolving external references when loading openapi specifications.
func PathToRawSpec(pathToFile string) map[string]func() ([]byte, error) {
var res = make(map[string]func() ([]byte, error))
if len(pathToFile) > 0 {
res[pathToFile] = rawSpec
}
pathPrefix := path.Dir(pathToFile)
for rawPath, rawFunc := range externalRef0.PathToRawSpec(path.Join(pathPrefix, "./components.yaml")) {
if _, ok := res[rawPath]; ok {
// it is not possible to compare functions in golang, so always overwrite the old value
}
res[rawPath] = rawFunc
}
return res
}
// GetSwagger returns the Swagger specification corresponding to the generated code
// in this file. The external references of Swagger specification are resolved.
// The logic of resolving external references is tightly connected to "import-mapping" feature.
// Externally referenced files must be embedded in the corresponding golang packages.
// Urls can be supported but this task was out of the scope.
func GetSwagger() (swagger *openapi3.T, err error) {
var resolvePath = PathToRawSpec("")
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *openapi3.Loader, url *url.URL) ([]byte, error) {
var pathToFile = url.String()
pathToFile = path.Clean(pathToFile)
getSpec, ok := resolvePath[pathToFile]
if !ok {
err1 := fmt.Errorf("path not found: %s", pathToFile)
return nil, err1
}
return getSpec()
}
var specData []byte
specData, err = rawSpec()
if err != nil {
return
}
swagger, err = loader.LoadFromData(specData)
if err != nil {
return
}
return
}