// Package auth provides primitives to interact with the openapi HTTP API. // // Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.5.0 DO NOT EDIT. package auth import ( "context" "encoding/json" "fmt" "net/http" "github.com/gin-gonic/gin" strictgin "github.com/oapi-codegen/runtime/strictmiddleware/gin" ) // PostSignInJSONBody defines parameters for PostSignIn. type PostSignInJSONBody struct { Nickname string `json:"nickname"` Pass string `json:"pass"` } // PostSignUpJSONBody defines parameters for PostSignUp. type PostSignUpJSONBody struct { Nickname string `json:"nickname"` Pass string `json:"pass"` } // PostSignInJSONRequestBody defines body for PostSignIn for application/json ContentType. type PostSignInJSONRequestBody PostSignInJSONBody // PostSignUpJSONRequestBody defines body for PostSignUp for application/json ContentType. type PostSignUpJSONRequestBody PostSignUpJSONBody // ServerInterface represents all server handlers. type ServerInterface interface { // Sign in a user and return JWT // (POST /sign-in) PostSignIn(c *gin.Context) // Sign up a new user // (POST /sign-up) PostSignUp(c *gin.Context) } // ServerInterfaceWrapper converts contexts to parameters. type ServerInterfaceWrapper struct { Handler ServerInterface HandlerMiddlewares []MiddlewareFunc ErrorHandler func(*gin.Context, error, int) } type MiddlewareFunc func(c *gin.Context) // PostSignIn operation middleware func (siw *ServerInterfaceWrapper) PostSignIn(c *gin.Context) { for _, middleware := range siw.HandlerMiddlewares { middleware(c) if c.IsAborted() { return } } siw.Handler.PostSignIn(c) } // PostSignUp operation middleware func (siw *ServerInterfaceWrapper) PostSignUp(c *gin.Context) { for _, middleware := range siw.HandlerMiddlewares { middleware(c) if c.IsAborted() { return } } siw.Handler.PostSignUp(c) } // GinServerOptions provides options for the Gin server. type GinServerOptions struct { BaseURL string Middlewares []MiddlewareFunc ErrorHandler func(*gin.Context, error, int) } // RegisterHandlers creates http.Handler with routing matching OpenAPI spec. func RegisterHandlers(router gin.IRouter, si ServerInterface) { RegisterHandlersWithOptions(router, si, GinServerOptions{}) } // RegisterHandlersWithOptions creates http.Handler with additional options func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options GinServerOptions) { errorHandler := options.ErrorHandler if errorHandler == nil { errorHandler = func(c *gin.Context, err error, statusCode int) { c.JSON(statusCode, gin.H{"msg": err.Error()}) } } wrapper := ServerInterfaceWrapper{ Handler: si, HandlerMiddlewares: options.Middlewares, ErrorHandler: errorHandler, } router.POST(options.BaseURL+"/sign-in", wrapper.PostSignIn) router.POST(options.BaseURL+"/sign-up", wrapper.PostSignUp) } type PostSignInRequestObject struct { Body *PostSignInJSONRequestBody } type PostSignInResponseObject interface { VisitPostSignInResponse(w http.ResponseWriter) error } type PostSignIn200JSONResponse struct { UserId int64 `json:"user_id"` UserName string `json:"user_name"` } func (response PostSignIn200JSONResponse) VisitPostSignInResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) return json.NewEncoder(w).Encode(response) } type PostSignIn401JSONResponse struct { Error *string `json:"error,omitempty"` } func (response PostSignIn401JSONResponse) VisitPostSignInResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") w.WriteHeader(401) return json.NewEncoder(w).Encode(response) } type PostSignUpRequestObject struct { Body *PostSignUpJSONRequestBody } type PostSignUpResponseObject interface { VisitPostSignUpResponse(w http.ResponseWriter) error } type PostSignUp200JSONResponse struct { UserId int64 `json:"user_id"` } func (response PostSignUp200JSONResponse) VisitPostSignUpResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) return json.NewEncoder(w).Encode(response) } // StrictServerInterface represents all server handlers. type StrictServerInterface interface { // Sign in a user and return JWT // (POST /sign-in) PostSignIn(ctx context.Context, request PostSignInRequestObject) (PostSignInResponseObject, error) // Sign up a new user // (POST /sign-up) PostSignUp(ctx context.Context, request PostSignUpRequestObject) (PostSignUpResponseObject, error) } type StrictHandlerFunc = strictgin.StrictGinHandlerFunc type StrictMiddlewareFunc = strictgin.StrictGinMiddlewareFunc func NewStrictHandler(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc) ServerInterface { return &strictHandler{ssi: ssi, middlewares: middlewares} } type strictHandler struct { ssi StrictServerInterface middlewares []StrictMiddlewareFunc } // PostSignIn operation middleware func (sh *strictHandler) PostSignIn(ctx *gin.Context) { var request PostSignInRequestObject var body PostSignInJSONRequestBody if err := ctx.ShouldBindJSON(&body); err != nil { ctx.Status(http.StatusBadRequest) ctx.Error(err) return } request.Body = &body handler := func(ctx *gin.Context, request interface{}) (interface{}, error) { return sh.ssi.PostSignIn(ctx, request.(PostSignInRequestObject)) } for _, middleware := range sh.middlewares { handler = middleware(handler, "PostSignIn") } response, err := handler(ctx, request) if err != nil { ctx.Error(err) ctx.Status(http.StatusInternalServerError) } else if validResponse, ok := response.(PostSignInResponseObject); ok { if err := validResponse.VisitPostSignInResponse(ctx.Writer); err != nil { ctx.Error(err) } } else if response != nil { ctx.Error(fmt.Errorf("unexpected response type: %T", response)) } } // PostSignUp operation middleware func (sh *strictHandler) PostSignUp(ctx *gin.Context) { var request PostSignUpRequestObject var body PostSignUpJSONRequestBody if err := ctx.ShouldBindJSON(&body); err != nil { ctx.Status(http.StatusBadRequest) ctx.Error(err) return } request.Body = &body handler := func(ctx *gin.Context, request interface{}) (interface{}, error) { return sh.ssi.PostSignUp(ctx, request.(PostSignUpRequestObject)) } for _, middleware := range sh.middlewares { handler = middleware(handler, "PostSignUp") } response, err := handler(ctx, request) if err != nil { ctx.Error(err) ctx.Status(http.StatusInternalServerError) } else if validResponse, ok := response.(PostSignUpResponseObject); ok { if err := validResponse.VisitPostSignUpResponse(ctx.Writer); err != nil { ctx.Error(err) } } else if response != nil { ctx.Error(fmt.Errorf("unexpected response type: %T", response)) } }