nquest/api/server.go

407 lines
14 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 {
2023-11-19 22:54:54 +03:00
// (GET /admin/games)
GetAdminGames(ctx echo.Context) error
// (GET /games)
GetGames(ctx echo.Context) error
2023-11-01 23:21:12 +03:00
// (GET /teams)
GetTeams(ctx echo.Context) error
2023-11-02 23:38:50 +03:00
// (POST /teams)
PostTeams(ctx echo.Context) error
// (DELETE /teams/{teamID})
DeleteTeamsTeamID(ctx echo.Context, teamID int) error
2023-11-01 23:21:12 +03:00
// (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
// (POST /teams/{teamID}/requests)
PostTeamsTeamIDRequests(ctx echo.Context, teamID int) error
// (POST /teams/{teamID}/requests/{userID})
PostTeamsTeamIDRequestsUserID(ctx echo.Context, teamID int, userID int) 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
}
2023-11-19 22:54:54 +03:00
// GetAdminGames converts echo context to params.
func (w *ServerInterfaceWrapper) GetAdminGames(ctx echo.Context) error {
var err error
ctx.Set(CookieAuthScopes, []string{"creator", "admin"})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetAdminGames(ctx)
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
}
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
}
2023-11-02 23:38:50 +03:00
// PostTeams converts echo context to params.
func (w *ServerInterfaceWrapper) PostTeams(ctx echo.Context) error {
var err error
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.PostTeams(ctx)
return err
}
// DeleteTeamsTeamID converts echo context to params.
func (w *ServerInterfaceWrapper) DeleteTeamsTeamID(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.DeleteTeamsTeamID(ctx, teamID)
return err
}
2023-11-01 23:21:12 +03:00
// 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
}
// PostTeamsTeamIDRequests converts echo context to params.
func (w *ServerInterfaceWrapper) PostTeamsTeamIDRequests(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.PostTeamsTeamIDRequests(ctx, teamID)
return err
}
// PostTeamsTeamIDRequestsUserID converts echo context to params.
func (w *ServerInterfaceWrapper) PostTeamsTeamIDRequestsUserID(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))
}
// ------------- Path parameter "userID" -------------
var userID int
err = runtime.BindStyledParameterWithLocation("simple", false, "userID", runtime.ParamLocationPath, ctx.Param("userID"), &userID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter userID: %s", err))
}
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.PostTeamsTeamIDRequestsUserID(ctx, teamID, userID)
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
// 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,
}
2023-11-19 22:54:54 +03:00
router.GET(baseURL+"/admin/games", wrapper.GetAdminGames)
router.GET(baseURL+"/games", wrapper.GetGames)
2023-11-01 23:21:12 +03:00
router.GET(baseURL+"/teams", wrapper.GetTeams)
2023-11-02 23:38:50 +03:00
router.POST(baseURL+"/teams", wrapper.PostTeams)
router.DELETE(baseURL+"/teams/:teamID", wrapper.DeleteTeamsTeamID)
2023-11-01 23:21:12 +03:00
router.GET(baseURL+"/teams/:teamID", wrapper.GetTeamsTeamID)
router.DELETE(baseURL+"/teams/:teamID/members", wrapper.DeleteTeamsTeamIDMembers)
router.POST(baseURL+"/teams/:teamID/members", wrapper.PostTeamsTeamIDMembers)
router.POST(baseURL+"/teams/:teamID/requests", wrapper.PostTeamsTeamIDRequests)
router.POST(baseURL+"/teams/:teamID/requests/:userID", wrapper.PostTeamsTeamIDRequestsUserID)
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{
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oE8P2pTA+g798fn3F/",
"i6uPqbIYNZrRRA05Oj+Coy6a6RP+uXzftLeXLVgYM/ee3hN312RNFaR5CZbE/8YVMFbVrnytNx3210mP",
"zn3ZadYjSs5H1yr2nNSYzdwXd+4HxjvtHd3Rcf/UdXT/O4YOqsYLMnAKbQp9TAj0RnO9vF/i1WtX2v+k",
"ERXGq90HiZ+h1j0/6RPe05zaLyLqM837bnQlwZVqv+mPKCBeVVo9QKgf2qsTb/kK68R3nlPH3mfXXC6G",
"OPgCdvR57CGm3c8n00mJUD+S2SjC4V17v9C49ZvTeLpa7mngMu39VrWNchztYm5Hps5zMOYXt/SpEfd/",
"LJrHfOUtj4hXt8vrCdlsz7ZG3TGgH7wu1nrLMpZi19msm/8CAAD//7N+kRk/HAAA",
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
}