278 lines
9.1 KiB
Go
278 lines
9.1 KiB
Go
// 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 /engine/{uid})
|
|
GameEngine(ctx echo.Context, uid int) error
|
|
|
|
// (POST /engine/{uid}/code)
|
|
EnterCode(ctx echo.Context, uid int) error
|
|
|
|
// (GET /games)
|
|
GetGames(ctx echo.Context) error
|
|
|
|
// (POST /games)
|
|
CreateGame(ctx echo.Context) 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
|
|
}
|
|
|
|
// GameEngine converts echo context to params.
|
|
func (w *ServerInterfaceWrapper) GameEngine(ctx echo.Context) error {
|
|
var err error
|
|
// ------------- Path parameter "uid" -------------
|
|
var uid int
|
|
|
|
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.GameEngine(ctx, uid)
|
|
return err
|
|
}
|
|
|
|
// EnterCode converts echo context to params.
|
|
func (w *ServerInterfaceWrapper) EnterCode(ctx echo.Context) error {
|
|
var err error
|
|
// ------------- Path parameter "uid" -------------
|
|
var uid int
|
|
|
|
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.EnterCode(ctx, uid)
|
|
return err
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// CreateGame converts echo context to params.
|
|
func (w *ServerInterfaceWrapper) CreateGame(ctx echo.Context) error {
|
|
var err error
|
|
|
|
ctx.Set(CookieAuthScopes, []string{"creator", "admin"})
|
|
|
|
// Invoke the callback with all the unmarshaled arguments
|
|
err = w.Handler.CreateGame(ctx)
|
|
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+"/engine/:uid", wrapper.GameEngine)
|
|
router.POST(baseURL+"/engine/:uid/code", wrapper.EnterCode)
|
|
router.GET(baseURL+"/games", wrapper.GetGames)
|
|
router.POST(baseURL+"/games", wrapper.CreateGame)
|
|
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/8xYTY+kRgz9K5GTIxp6P07cktVqFGUPyWSTS6sPFfAwtQtVpFz0bmvEf49cfDQ0BU0T",
|
|
"ZrWnGRXGfu/Zxq5+hljnhVaoLEH0DAb/LZHsLzqR6A5QWTTvdIIP9RM+i7WyqNy/oigyGQsrtQo/kVZ8",
|
|
"RvET5oL/K4wu0NjGVawT5L/2VCBEQNZIlUJVBS6qNJhAtK+tDkFrpf/5hLGFamhmTYlVAKnI8X0i7Rps",
|
|
"Pxl8hAh+DM8ChPVTClu/E2EznUr1P4TAXMjMo0QAhSD6ok1yXabaR++NhZIZTCVZNN8a/vnha+/TktAo",
|
|
"kS8okM4yGIvQj7JIEHdChVbUcDNGm4fmZLtal8piioaJ5kgk0qWNcLb300mQYiMLxgQRQNMTHyTZVSSk",
|
|
"xZyWdMffEr9wtAaSMEac5hCtQrMMhD+oFfR586DsdC4ol+YGxTPdYPi1QCNRxRPFxarQBokMQCb+CBke",
|
|
"MfM/Wt7CkjvV08c9em2kltOy8q+CRtSuCd1nfOkounB4jUbfOJgaXPUDJ/M3gOGL302zUfz5MAEUWjZ7",
|
|
"wTjb3AvLS42t64k6LjUrbeaXoT64XsQf2e5SltptcJEl57JF3zGcku1jAwBVmbPPozS2FFyVsbSn3mtn",
|
|
"zF1T3az2VMu9kD6uCWdE8mlCOivZxl9O4rFZLDwc8Ku9Xsm1g8Z6Lr5f4I3i+wJ39evt4eV90H2SPH3Q",
|
|
"klvubZAOX2f5WU+X1EQLOT9BQ7UPdEqq6c/dbVJNTSd3K8FkE1/rZZ/y+EKyt6TnE8CMMC6NtKc/GW8r",
|
|
"vf4s8efSPjmWPC3rIwigntlASNRr/whEIX/DZquT6lE74DUDUH+4C1cARzRUT99Xd7u7HbPUBSpRSIjg",
|
|
"zd2ru51byu2TgxGiSqXC8LmUScUHKTqluErcbvRrAhHc88Byhu5dI3K0aAiifQOd/Z2Bl+5LNlzsg95y",
|
|
"dfk1qA4XS//r3W4q5Z1dOFgoKyfKgE3YDvNCk4fT+/Ye/YKU2qv7aZpN73Yfjq721Va6dGuoP71o753B",
|
|
"mnCj602/4CHaH9za4svAO4PC4n29cd4s1uVPDdVa8FPAhz26h5jhap6GIsmlgkN1cNry2jwn7V/kRujt",
|
|
"4AaXlyqAt7s3118a3peb9LOnsPuRxJ+N3zU5qB+c2YqE1P6rbZjubmd6UXV93rq0i4iz3Qj/29GqCFTG",
|
|
"MRL90LjeGnH/J6F5zA+t5Yp8dVG+n5TNNuCBv6mE5th+pkuTQQQhT7bqUP0XAAD//zkXlqI5FQAA",
|
|
}
|
|
|
|
// 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
|
|
}
|