nquest/api/server.go

325 lines
11 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"
openapi_types "github.com/oapi-codegen/runtime/types"
2023-11-01 23:21:12 +03:00
)
// ServerInterface represents all server handlers.
type ServerInterface interface {
2024-01-05 03:50:33 +03:00
// (GET /engine/{uid})
GameEngine(ctx echo.Context, uid openapi_types.UUID) error
2024-01-05 03:50:33 +03:00
// (POST /engine/{uid}/code)
EnterCode(ctx echo.Context, uid openapi_types.UUID) error
2023-11-19 22:54:54 +03:00
2024-01-20 21:37:49 +03:00
// (POST /file/upload)
UploadFile(ctx echo.Context) error
// (GET /file/{uid})
GetFile(ctx echo.Context, uid openapi_types.UUID) error
// (GET /games)
GetGames(ctx echo.Context) error
2024-01-05 03:50:33 +03:00
// (POST /games)
CreateGame(ctx echo.Context) error
2023-11-01 23:21:12 +03:00
// (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
}
2024-01-05 03:50:33 +03:00
// GameEngine converts echo context to params.
func (w *ServerInterfaceWrapper) GameEngine(ctx echo.Context) error {
2023-11-01 23:21:12 +03:00
var err error
2024-01-05 03:50:33 +03:00
// ------------- Path parameter "uid" -------------
var uid openapi_types.UUID
2023-11-01 23:21:12 +03:00
2024-01-05 03:50:33 +03:00
err = runtime.BindStyledParameterWithLocation("simple", false, "uid", runtime.ParamLocationPath, ctx.Param("uid"), &uid)
2023-11-01 23:21:12 +03:00
if err != nil {
2024-01-05 03:50:33 +03:00
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter uid: %s", err))
2023-11-01 23:21:12 +03:00
}
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
2024-01-05 03:50:33 +03:00
err = w.Handler.GameEngine(ctx, uid)
2023-11-01 23:21:12 +03:00
return err
}
2024-01-05 03:50:33 +03:00
// EnterCode converts echo context to params.
func (w *ServerInterfaceWrapper) EnterCode(ctx echo.Context) error {
2023-11-01 23:21:12 +03:00
var err error
2024-01-05 03:50:33 +03:00
// ------------- Path parameter "uid" -------------
var uid openapi_types.UUID
2023-11-01 23:21:12 +03:00
2024-01-05 03:50:33 +03:00
err = runtime.BindStyledParameterWithLocation("simple", false, "uid", runtime.ParamLocationPath, ctx.Param("uid"), &uid)
2023-11-01 23:21:12 +03:00
if err != nil {
2024-01-05 03:50:33 +03:00
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter uid: %s", err))
2023-11-01 23:21:12 +03:00
}
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
2024-01-05 03:50:33 +03:00
err = w.Handler.EnterCode(ctx, uid)
2023-11-01 23:21:12 +03:00
return err
}
2024-01-20 21:37:49 +03:00
// UploadFile converts echo context to params.
func (w *ServerInterfaceWrapper) UploadFile(ctx echo.Context) error {
var err error
2024-01-21 02:20:59 +03:00
ctx.Set(CookieAuthScopes, []string{"creator", "admin"})
2024-01-20 21:37:49 +03:00
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.UploadFile(ctx)
return err
}
// GetFile converts echo context to params.
func (w *ServerInterfaceWrapper) GetFile(ctx echo.Context) error {
var err error
// ------------- Path parameter "uid" -------------
var uid openapi_types.UUID
err = runtime.BindStyledParameterWithLocation("simple", false, "uid", runtime.ParamLocationPath, ctx.Param("uid"), &uid)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter uid: %s", err))
}
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetFile(ctx, uid)
return err
}
2024-01-05 03:50:33 +03:00
// GetGames converts echo context to params.
func (w *ServerInterfaceWrapper) GetGames(ctx echo.Context) error {
var err error
2024-01-21 02:20:59 +03:00
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
2024-01-05 03:50:33 +03:00
err = w.Handler.GetGames(ctx)
return err
}
2024-01-05 03:50:33 +03:00
// CreateGame converts echo context to params.
func (w *ServerInterfaceWrapper) CreateGame(ctx echo.Context) error {
var err error
2024-01-05 03:50:33 +03:00
ctx.Set(CookieAuthScopes, []string{"creator", "admin"})
// Invoke the callback with all the unmarshaled arguments
2024-01-05 03:50:33 +03:00
err = w.Handler.CreateGame(ctx)
return err
}
2023-11-01 23:21:12 +03:00
// 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
2024-01-21 02:20:59 +03:00
ctx.Set(CookieAuthScopes, []string{})
2023-11-01 23:21:12 +03:00
// 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,
}
2024-01-05 03:50:33 +03:00
router.GET(baseURL+"/engine/:uid", wrapper.GameEngine)
router.POST(baseURL+"/engine/:uid/code", wrapper.EnterCode)
2024-01-20 21:37:49 +03:00
router.POST(baseURL+"/file/upload", wrapper.UploadFile)
router.GET(baseURL+"/file/:uid", wrapper.GetFile)
router.GET(baseURL+"/games", wrapper.GetGames)
2024-01-05 03:50:33 +03:00
router.POST(baseURL+"/games", wrapper.CreateGame)
2023-11-01 23:21:12 +03:00
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{
2024-01-21 02:20:59 +03:00
"H4sIAAAAAAAC/8xYX2/bNhD/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vL1YXKyJHlSB5KVjC3l68vljZIRQfLIwY5EZIiJ8rkdW0sAFbIpSn",
"9n79a8YS9pGugFbQfqt5AQh0tbj10EnfHriLfH+QjToX9KmhYX0w875ZrcYSs5WLe/NUbTnqORc3t+VS",
"mYCLH5pnpJfzsHnI2o0713nrigcPXfW5aLoXOcRuNBwn6JPd/0XYDngAvjOaFVWOouQaY+LgVcYxMD2S",
"wR5Nn4XkehecH4NPJKd6fTD3dmvShrhbjbfDhlOv9zxNVAqgp+i7lcnInKxSBHxlUAMv+vPydBQGo7IN",
"oM+e9gQZI+Rj0x9PDtvgqchaDOfnpb1Lf3RN/eTiOnyorZfC/dYcs2feETI/uTNxQQl0n1PqiL1bvZ3+",
"qP/a6ANOmuL2iTkcjT+UsVCvrNiCgDj99Xk8XZ3uaS94657fqsJZjpPcAP+7wcMJM1WagjE/eNVLEe8x",
"dp/Qj6O8biQXRKi18v8J0tGSW1PDNKC3TUuudM4SFtPNqF7X/wYAAP//R+pUyGkaAAA=",
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
}