448 lines
14 KiB
Go
448 lines
14 KiB
Go
// Package api provides primitives to interact with the openapi HTTP API.
|
|
//
|
|
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT.
|
|
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/getkin/kin-openapi/openapi3"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/oapi-codegen/runtime"
|
|
)
|
|
|
|
// Error defines model for Error.
|
|
type Error struct {
|
|
// Code Error code
|
|
Code int32 `json:"code"`
|
|
|
|
// Message Error message
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// NewPet defines model for NewPet.
|
|
type NewPet struct {
|
|
// Name Name of the pet
|
|
Name string `json:"name"`
|
|
|
|
// Tag Type of the pet
|
|
Tag *string `json:"tag,omitempty"`
|
|
}
|
|
|
|
// Pet defines model for Pet.
|
|
type Pet struct {
|
|
// Id Unique id of the pet
|
|
Id int64 `gorm:"primarykey" json:"id"`
|
|
|
|
// Name Name of the pet
|
|
Name string `json:"name"`
|
|
|
|
// Tag Type of the pet
|
|
Tag *string `json:"tag,omitempty"`
|
|
}
|
|
|
|
// FindPetsParams defines parameters for FindPets.
|
|
type FindPetsParams struct {
|
|
// Tags tags to filter by
|
|
Tags *[]string `form:"tags,omitempty" json:"tags,omitempty"`
|
|
|
|
// Limit maximum number of results to return
|
|
Limit *int32 `form:"limit,omitempty" json:"limit,omitempty"`
|
|
}
|
|
|
|
// AddPetJSONRequestBody defines body for AddPet for application/json ContentType.
|
|
type AddPetJSONRequestBody = NewPet
|
|
|
|
// ServerInterface represents all server handlers.
|
|
type ServerInterface interface {
|
|
// Returns all pets
|
|
// (GET /pets)
|
|
FindPets(w http.ResponseWriter, r *http.Request, params FindPetsParams)
|
|
// Creates a new pet
|
|
// (POST /pets)
|
|
AddPet(w http.ResponseWriter, r *http.Request)
|
|
// Deletes a pet by ID
|
|
// (DELETE /pets/{id})
|
|
DeletePet(w http.ResponseWriter, r *http.Request, id int64)
|
|
// Returns a pet by ID
|
|
// (GET /pets/{id})
|
|
FindPetByID(w http.ResponseWriter, r *http.Request, id int64)
|
|
}
|
|
|
|
// Unimplemented server implementation that returns http.StatusNotImplemented for each endpoint.
|
|
|
|
type Unimplemented struct{}
|
|
|
|
// Returns all pets
|
|
// (GET /pets)
|
|
func (_ Unimplemented) FindPets(w http.ResponseWriter, r *http.Request, params FindPetsParams) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// Creates a new pet
|
|
// (POST /pets)
|
|
func (_ Unimplemented) AddPet(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// Deletes a pet by ID
|
|
// (DELETE /pets/{id})
|
|
func (_ Unimplemented) DeletePet(w http.ResponseWriter, r *http.Request, id int64) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// Returns a pet by ID
|
|
// (GET /pets/{id})
|
|
func (_ Unimplemented) FindPetByID(w http.ResponseWriter, r *http.Request, id int64) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// ServerInterfaceWrapper converts contexts to parameters.
|
|
type ServerInterfaceWrapper struct {
|
|
Handler ServerInterface
|
|
HandlerMiddlewares []MiddlewareFunc
|
|
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
|
|
}
|
|
|
|
type MiddlewareFunc func(http.Handler) http.Handler
|
|
|
|
// FindPets operation middleware
|
|
func (siw *ServerInterfaceWrapper) FindPets(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
// Parameter object where we will unmarshal all parameters from the context
|
|
var params FindPetsParams
|
|
|
|
// ------------- Optional query parameter "tags" -------------
|
|
|
|
err = runtime.BindQueryParameter("form", true, false, "tags", r.URL.Query(), ¶ms.Tags)
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "tags", Err: err})
|
|
return
|
|
}
|
|
|
|
// ------------- Optional query parameter "limit" -------------
|
|
|
|
err = runtime.BindQueryParameter("form", true, false, "limit", r.URL.Query(), ¶ms.Limit)
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "limit", Err: err})
|
|
return
|
|
}
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.FindPets(w, r, params)
|
|
}))
|
|
|
|
for i := len(siw.HandlerMiddlewares) - 1; i >= 0; i-- {
|
|
handler = siw.HandlerMiddlewares[i](handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r)
|
|
}
|
|
|
|
// AddPet operation middleware
|
|
func (siw *ServerInterfaceWrapper) AddPet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.AddPet(w, r)
|
|
}))
|
|
|
|
for i := len(siw.HandlerMiddlewares) - 1; i >= 0; i-- {
|
|
handler = siw.HandlerMiddlewares[i](handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r)
|
|
}
|
|
|
|
// DeletePet operation middleware
|
|
func (siw *ServerInterfaceWrapper) DeletePet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
// ------------- Path parameter "id" -------------
|
|
var id int64
|
|
|
|
err = runtime.BindStyledParameterWithOptions("simple", "id", chi.URLParam(r, "id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "id", Err: err})
|
|
return
|
|
}
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.DeletePet(w, r, id)
|
|
}))
|
|
|
|
for i := len(siw.HandlerMiddlewares) - 1; i >= 0; i-- {
|
|
handler = siw.HandlerMiddlewares[i](handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r)
|
|
}
|
|
|
|
// FindPetByID operation middleware
|
|
func (siw *ServerInterfaceWrapper) FindPetByID(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
// ------------- Path parameter "id" -------------
|
|
var id int64
|
|
|
|
err = runtime.BindStyledParameterWithOptions("simple", "id", chi.URLParam(r, "id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "id", Err: err})
|
|
return
|
|
}
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.FindPetByID(w, r, id)
|
|
}))
|
|
|
|
for i := len(siw.HandlerMiddlewares) - 1; i >= 0; i-- {
|
|
handler = siw.HandlerMiddlewares[i](handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r)
|
|
}
|
|
|
|
type UnescapedCookieParamError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *UnescapedCookieParamError) Error() string {
|
|
return fmt.Sprintf("error unescaping cookie parameter '%s'", e.ParamName)
|
|
}
|
|
|
|
func (e *UnescapedCookieParamError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type UnmarshalingParamError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *UnmarshalingParamError) Error() string {
|
|
return fmt.Sprintf("Error unmarshaling parameter %s as JSON: %s", e.ParamName, e.Err.Error())
|
|
}
|
|
|
|
func (e *UnmarshalingParamError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type RequiredParamError struct {
|
|
ParamName string
|
|
}
|
|
|
|
func (e *RequiredParamError) Error() string {
|
|
return fmt.Sprintf("Query argument %s is required, but not found", e.ParamName)
|
|
}
|
|
|
|
type RequiredHeaderError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *RequiredHeaderError) Error() string {
|
|
return fmt.Sprintf("Header parameter %s is required, but not found", e.ParamName)
|
|
}
|
|
|
|
func (e *RequiredHeaderError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type InvalidParamFormatError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *InvalidParamFormatError) Error() string {
|
|
return fmt.Sprintf("Invalid format for parameter %s: %s", e.ParamName, e.Err.Error())
|
|
}
|
|
|
|
func (e *InvalidParamFormatError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type TooManyValuesForParamError struct {
|
|
ParamName string
|
|
Count int
|
|
}
|
|
|
|
func (e *TooManyValuesForParamError) Error() string {
|
|
return fmt.Sprintf("Expected one value for %s, got %d", e.ParamName, e.Count)
|
|
}
|
|
|
|
// 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
|
|
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
if options.ErrorHandlerFunc == nil {
|
|
options.ErrorHandlerFunc = func(w http.ResponseWriter, r *http.Request, err error) {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
}
|
|
}
|
|
wrapper := ServerInterfaceWrapper{
|
|
Handler: si,
|
|
HandlerMiddlewares: options.Middlewares,
|
|
ErrorHandlerFunc: options.ErrorHandlerFunc,
|
|
}
|
|
|
|
r.Group(func(r chi.Router) {
|
|
r.Get(options.BaseURL+"/pets", wrapper.FindPets)
|
|
})
|
|
r.Group(func(r chi.Router) {
|
|
r.Post(options.BaseURL+"/pets", wrapper.AddPet)
|
|
})
|
|
r.Group(func(r chi.Router) {
|
|
r.Delete(options.BaseURL+"/pets/{id}", wrapper.DeletePet)
|
|
})
|
|
r.Group(func(r chi.Router) {
|
|
r.Get(options.BaseURL+"/pets/{id}", wrapper.FindPetByID)
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
// Base64 encoded, gzipped, json marshaled Swagger object
|
|
var swaggerSpec = []string{
|
|
|
|
"H4sIAAAAAAAC/+RXW48budH9KwV+32NPa2Iv8qCneD1eQEDWnsS7eVn7oYZdkmrDm8miZgRD/z0odus2",
|
|
"kmcTJAgS5EWXbrJ56pzD4umvxkafYqAgxcy/mmLX5LH9fJdzzPoj5ZgoC1O7bONA+j1QsZmTcAxmPg6G",
|
|
"dq8zy5g9ipkbDvL6lemMbBONf2lF2ew646kUXH3zQfvbh6lFMoeV2e06k+lL5UyDmf9ipgX3wz/vOvOe",
|
|
"Hu9JLnEH9FeWe4+eIC5B1gSJ5HLBzgiuLuf9tE0vz3sGtK2u8CZs6NyHpZn/8tX8f6almZv/mx2FmE0q",
|
|
"zKZadt3zYni4hPRz4C+VgIdzXKdi/P67SzE683SzijfHq23Q003ExDfK74rCDT1JxhvBVVt9FbM3c5My",
|
|
"e8zbv9LW7J7Xy4P5vPvcLnNYxtE4QdC26skjOzM3mFgI/R/KI65WlHu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pfVmOwakIMtQg0WJHORdrxtWJB6+KEWS0DSusFQ+bALtFMUS44yNzijf/cTvLqlYjOPrb5gAI8rLZnc",
|
|
"pFYPf6rjVB+d6jaqR3X0zhFKd2g+gNXqJhlHTvYcy57MMTWZw25Us6jAwKE7Qpk2buDCe8BFMViWOrBC",
|
|
"LQWhyt5nk5DjSmektfV6uD8VpjE3YUyZhKs/6VyjaWp34m9tvf0nPeM0erTzbjGYufmBw6DnSzs2shJA",
|
|
"ubQsc35YaFzQk3XJTijDw9ZoGDBz86VS3h5P+hYruil6tnQj5NsZdJnFxguYM271f5FtO/Y05LSYdI7A",
|
|
"4xN7bePVP1DWXJSpVCcNVm5n2TcwOfYsZ6B+M9TuPmsEKklbS0P/6vZ2n3sojKkvJTclh9mvRSF+vVb2",
|
|
"S5FwzIPPiNhdBKBEAnswYzxaYnXyD+F5Ccb4cnBl4RroKWlr1R48julMqV6T4pUAodhSLFeixttMKC2z",
|
|
"BXrUsfsw1nKNnsEjdh2iec65+EjDhVnfDOpVM6ZTKvJ9HLb/Mhb2+fyShnsS9RgOg34dYJvTlCy50u6f",
|
|
"9MxvWuW/xxoXgrf7LY/OvvKwGy3iSK68xo3XdW7hsHLt3QceUNtsHF2zuINStaYrHrlrs0ebvNjRFnfa",
|
|
"Q9Ko7YRl6h8aoI/tg4cLpb/VS669k13rJd9dVq1ARhTDf5KQdwcxmgpbWNwpvJdfKM4VO+i4uPvW8fP9",
|
|
"tt37+/Vaktj1v02u/9lt/EzRUf02hPJmL9P5W/H+pbw/ebPV19Pd593fAgAA//+hlBGVoBIAAA==",
|
|
}
|
|
|
|
// 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: %w", err)
|
|
}
|
|
zr, err := gzip.NewReader(bytes.NewReader(zipped))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error decompressing spec: %w", err)
|
|
}
|
|
var buf bytes.Buffer
|
|
_, err = buf.ReadFrom(zr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error decompressing spec: %w", 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) {
|
|
res := make(map[string]func() ([]byte, error))
|
|
if len(pathToFile) > 0 {
|
|
res[pathToFile] = rawSpec
|
|
}
|
|
|
|
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) {
|
|
resolvePath := PathToRawSpec("")
|
|
|
|
loader := openapi3.NewLoader()
|
|
loader.IsExternalRefsAllowed = true
|
|
loader.ReadFromURIFunc = func(loader *openapi3.Loader, url *url.URL) ([]byte, error) {
|
|
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
|
|
}
|