nquest/api/server.go

301 lines
9.9 KiB
Go
Raw Normal View History

2023-11-01 23:21:12 +03:00
// Package api provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.0.0 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/labstack/echo/v4"
"github.com/oapi-codegen/runtime"
)
// ServerInterface represents all server handlers.
type ServerInterface interface {
// (GET /games)
GetGames(ctx echo.Context) error
2023-11-01 23:21:12 +03:00
// (GET /teams)
GetTeams(ctx echo.Context) error
// (GET /teams/{teamID})
GetTeamsTeamID(ctx echo.Context, teamID int) error
// (DELETE /teams/{teamID}/members)
DeleteTeamsTeamIDMembers(ctx echo.Context, teamID int) error
// (POST /teams/{teamID}/members)
PostTeamsTeamIDMembers(ctx echo.Context, teamID int) error
// (GET /user)
GetUser(ctx echo.Context) error
// (POST /user/login)
PostUserLogin(ctx echo.Context) error
// (POST /user/logout)
PostUserLogout(ctx echo.Context) error
// (POST /user/register)
PostUserRegister(ctx echo.Context) error
}
// ServerInterfaceWrapper converts echo contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
}
// GetGames converts echo context to params.
func (w *ServerInterfaceWrapper) GetGames(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetGames(ctx)
return err
}
2023-11-01 23:21:12 +03:00
// GetTeams converts echo context to params.
func (w *ServerInterfaceWrapper) GetTeams(ctx echo.Context) error {
var err error
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetTeams(ctx)
return err
}
// GetTeamsTeamID converts echo context to params.
func (w *ServerInterfaceWrapper) GetTeamsTeamID(ctx echo.Context) error {
var err error
// ------------- Path parameter "teamID" -------------
var teamID int
err = runtime.BindStyledParameterWithLocation("simple", false, "teamID", runtime.ParamLocationPath, ctx.Param("teamID"), &teamID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter teamID: %s", err))
}
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetTeamsTeamID(ctx, teamID)
return err
}
// DeleteTeamsTeamIDMembers converts echo context to params.
func (w *ServerInterfaceWrapper) DeleteTeamsTeamIDMembers(ctx echo.Context) error {
var err error
// ------------- Path parameter "teamID" -------------
var teamID int
err = runtime.BindStyledParameterWithLocation("simple", false, "teamID", runtime.ParamLocationPath, ctx.Param("teamID"), &teamID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter teamID: %s", err))
}
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.DeleteTeamsTeamIDMembers(ctx, teamID)
return err
}
// PostTeamsTeamIDMembers converts echo context to params.
func (w *ServerInterfaceWrapper) PostTeamsTeamIDMembers(ctx echo.Context) error {
var err error
// ------------- Path parameter "teamID" -------------
var teamID int
err = runtime.BindStyledParameterWithLocation("simple", false, "teamID", runtime.ParamLocationPath, ctx.Param("teamID"), &teamID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter teamID: %s", err))
}
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.PostTeamsTeamIDMembers(ctx, teamID)
return err
}
// GetUser converts echo context to params.
func (w *ServerInterfaceWrapper) GetUser(ctx echo.Context) error {
var err error
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetUser(ctx)
return err
}
// PostUserLogin converts echo context to params.
func (w *ServerInterfaceWrapper) PostUserLogin(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.PostUserLogin(ctx)
return err
}
// PostUserLogout converts echo context to params.
func (w *ServerInterfaceWrapper) PostUserLogout(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.PostUserLogout(ctx)
return err
}
// PostUserRegister converts echo context to params.
func (w *ServerInterfaceWrapper) PostUserRegister(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.PostUserRegister(ctx)
return err
}
// This is a simple interface which specifies echo.Route addition functions which
// are present on both echo.Echo and echo.Group, since we want to allow using
// either of them for path registration
type EchoRouter interface {
CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}
// RegisterHandlers adds each server route to the EchoRouter.
func RegisterHandlers(router EchoRouter, si ServerInterface) {
RegisterHandlersWithBaseURL(router, si, "")
}
// Registers handlers, and prepends BaseURL to the paths, so that the paths
// can be served under a prefix.
func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) {
wrapper := ServerInterfaceWrapper{
Handler: si,
}
router.GET(baseURL+"/games", wrapper.GetGames)
2023-11-01 23:21:12 +03:00
router.GET(baseURL+"/teams", wrapper.GetTeams)
router.GET(baseURL+"/teams/:teamID", wrapper.GetTeamsTeamID)
router.DELETE(baseURL+"/teams/:teamID/members", wrapper.DeleteTeamsTeamIDMembers)
router.POST(baseURL+"/teams/:teamID/members", wrapper.PostTeamsTeamIDMembers)
router.GET(baseURL+"/user", wrapper.GetUser)
router.POST(baseURL+"/user/login", wrapper.PostUserLogin)
router.POST(baseURL+"/user/logout", wrapper.PostUserLogout)
router.POST(baseURL+"/user/register", wrapper.PostUserRegister)
}
// Base64 encoded, gzipped, json marshaled Swagger object
var swaggerSpec = []string{
"H4sIAAAAAAAC/8xYTW/jNhD9KwXbo2B5d3PSbYsAQdAEaF23F8MHRpo4TC1S5YwSGIH+ezEUJUuRZEuO",
"4+4pCjnkvPfmQ2O9idikmdGgCUX0Jiz8mwPSryZR4Ba2ZqM0P8RGE2jiR5llWxVLUkaHz2jcNsZPkEp+",
"yqzJwJI/D6lUW36gXQYiEkhW6Y0oApFJxFdjk57NInBAlIVERCt/R+PEOqhOmIdniEkU7SNkc3ALG4UE",
"9tLw95tfe3dzBKtlCseZ15ZBV4Sml1GCuBXMjEbPzVpjF37lAxrFJmlSUZpgA5aJpoAoNyN4uiv29v10",
"EsDYqowxiUjw/RuZwp1COomEIkgd/l8sPIpI/BzuSyEszTBkD38reGVvHpK0Vu6GEBHI9BySWpAEyXfq",
"zR+VDMmdPoDF0eQY7L0706UXiIEMDaoeMc3PojzUo2M7ExQntk/5ik/DZdCQZmyWsHv83DRhF1PShOv6",
"DGky3J2GUoSBHiPD4JZsN6lVucB1+tW4GBWBp+hY1TXX4ds6N4E1krQ0UEwuO84Q6ECQou1YoUrbthJ7",
"nBWqrnrlji/aiW3DmhLemNgv2NbHf8yZUpOet5fwfg8XbtU4yx4xkddFMfYn5mGAcW4taFr62vP7D8Zs",
"QeqRDb27OaEsfUkeZleXfYfdELzhN8TkTDuA2l12CPDCewOdp3y4VI35yoyk0o3D7aTpj+UQ21NbYU8L",
"DARCnFtFuz9ZjWqMMv8o+J7Tk0PBnbFcqqSIBAJi2Smq3pOp38C/ZZR+NA5b2YaE/sMVUyBewGLZab/M",
"5rM5czEZaJkpEYlvsy+zuZsl6cnBcCNP2YfBpTNr415Ht4mIxA3QjTN4N0x+nc+HIl7bhZ15rSmFiFZr",
"/j+sO/IQgKUzOAVAdxQoAnE1/3b8ZHtYLoo90vCN/9xeF0chL52dE9vKFMiV9srHmgOwjzRVpu0ZPmiM",
"Au8ztFifqkhbjKuziBE2elcCWyDo6nLt1hvS3NcD3ycqVP3A3U0avLicb697by06v6wuqHwgMoM9Ofe7",
"Qbq0sv9f7lUjwFD5/eXf8pMhtgb1D/UKvimsv6YMB42h3jmzTrL2e218sPH3F+dhOp/OtKebV7xNTqOI",
"s10H/1Vn+heYxzEg/uSvPjfi5rejw5gXleUJ8aq9/Dghay+0x5LVmgsdwb5U7SO3WxGJkGeJYl38FwAA",
"//8/OTdEThQAAA==",
2023-11-01 23:21:12 +03:00
}
// 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
}