diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-28 16:19:52 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-28 17:12:12 +0900 |
| commit | 0dd94cbea6e857896c46d17493725f97369d99f9 (patch) | |
| tree | 8d324c16eecfb3fb510488c9339e9c4c4a0692ce /backend | |
| parent | d24c19963f8d0d17d6db93b418cc9d644693c868 (diff) | |
| download | iosdc-japan-2025-albatross-0dd94cbea6e857896c46d17493725f97369d99f9.tar.gz iosdc-japan-2025-albatross-0dd94cbea6e857896c46d17493725f97369d99f9.tar.zst iosdc-japan-2025-albatross-0dd94cbea6e857896c46d17493725f97369d99f9.zip | |
refactor: remove /api/ prefix from openapi.yaml
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/api/generated.go | 70 | ||||
| -rw-r--r-- | backend/api/handlers.go | 12 | ||||
| -rw-r--r-- | backend/api/workaround.go | 22 | ||||
| -rw-r--r-- | backend/main.go | 2 |
4 files changed, 64 insertions, 42 deletions
diff --git a/backend/api/generated.go b/backend/api/generated.go index 21aefd2..4455723 100644 --- a/backend/api/generated.go +++ b/backend/api/generated.go @@ -29,20 +29,20 @@ type JwtPayload struct { Username string `json:"username"` } -// PostApiLoginJSONBody defines parameters for PostApiLogin. -type PostApiLoginJSONBody struct { +// PostLoginJSONBody defines parameters for PostLogin. +type PostLoginJSONBody struct { Password string `json:"password"` Username string `json:"username"` } -// PostApiLoginJSONRequestBody defines body for PostApiLogin for application/json ContentType. -type PostApiLoginJSONRequestBody PostApiLoginJSONBody +// PostLoginJSONRequestBody defines body for PostLogin for application/json ContentType. +type PostLoginJSONRequestBody PostLoginJSONBody // ServerInterface represents all server handlers. type ServerInterface interface { // User login - // (POST /api/login) - PostApiLogin(ctx echo.Context) error + // (POST /login) + PostLogin(ctx echo.Context) error } // ServerInterfaceWrapper converts echo contexts to parameters. @@ -50,12 +50,12 @@ type ServerInterfaceWrapper struct { Handler ServerInterface } -// PostApiLogin converts echo context to params. -func (w *ServerInterfaceWrapper) PostApiLogin(ctx echo.Context) error { +// PostLogin converts echo context to params. +func (w *ServerInterfaceWrapper) PostLogin(ctx echo.Context) error { var err error // Invoke the callback with all the unmarshaled arguments - err = w.Handler.PostApiLogin(ctx) + err = w.Handler.PostLogin(ctx) return err } @@ -87,34 +87,34 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL Handler: si, } - router.POST(baseURL+"/api/login", wrapper.PostApiLogin) + router.POST(baseURL+"/login", wrapper.PostLogin) } -type PostApiLoginRequestObject struct { - Body *PostApiLoginJSONRequestBody +type PostLoginRequestObject struct { + Body *PostLoginJSONRequestBody } -type PostApiLoginResponseObject interface { - VisitPostApiLoginResponse(w http.ResponseWriter) error +type PostLoginResponseObject interface { + VisitPostLoginResponse(w http.ResponseWriter) error } -type PostApiLogin200JSONResponse struct { +type PostLogin200JSONResponse struct { Token string `json:"token"` } -func (response PostApiLogin200JSONResponse) VisitPostApiLoginResponse(w http.ResponseWriter) error { +func (response PostLogin200JSONResponse) VisitPostLoginResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) return json.NewEncoder(w).Encode(response) } -type PostApiLogin401JSONResponse struct { +type PostLogin401JSONResponse struct { Message string `json:"message"` } -func (response PostApiLogin401JSONResponse) VisitPostApiLoginResponse(w http.ResponseWriter) error { +func (response PostLogin401JSONResponse) VisitPostLoginResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") w.WriteHeader(401) @@ -124,8 +124,8 @@ func (response PostApiLogin401JSONResponse) VisitPostApiLoginResponse(w http.Res // StrictServerInterface represents all server handlers. type StrictServerInterface interface { // User login - // (POST /api/login) - PostApiLogin(ctx context.Context, request PostApiLoginRequestObject) (PostApiLoginResponseObject, error) + // (POST /login) + PostLogin(ctx context.Context, request PostLoginRequestObject) (PostLoginResponseObject, error) } type StrictHandlerFunc = strictecho.StrictEchoHandlerFunc @@ -140,29 +140,29 @@ type strictHandler struct { middlewares []StrictMiddlewareFunc } -// PostApiLogin operation middleware -func (sh *strictHandler) PostApiLogin(ctx echo.Context) error { - var request PostApiLoginRequestObject +// PostLogin operation middleware +func (sh *strictHandler) PostLogin(ctx echo.Context) error { + var request PostLoginRequestObject - var body PostApiLoginJSONRequestBody + var body PostLoginJSONRequestBody if err := ctx.Bind(&body); err != nil { return err } request.Body = &body handler := func(ctx echo.Context, request interface{}) (interface{}, error) { - return sh.ssi.PostApiLogin(ctx.Request().Context(), request.(PostApiLoginRequestObject)) + return sh.ssi.PostLogin(ctx.Request().Context(), request.(PostLoginRequestObject)) } for _, middleware := range sh.middlewares { - handler = middleware(handler, "PostApiLogin") + handler = middleware(handler, "PostLogin") } response, err := handler(ctx, request) if err != nil { return err - } else if validResponse, ok := response.(PostApiLoginResponseObject); ok { - return validResponse.VisitPostApiLoginResponse(ctx.Response()) + } else if validResponse, ok := response.(PostLoginResponseObject); ok { + return validResponse.VisitPostLoginResponse(ctx.Response()) } else if response != nil { return fmt.Errorf("unexpected response type: %T", response) } @@ -172,14 +172,14 @@ func (sh *strictHandler) PostApiLogin(ctx echo.Context) error { // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/6RSzY7aQAx+lcjnKISlp9yoemHVA1LVU1Uhk5gwdDKejp1lo1XevZqkZElB2kpwIJnJ", + "H4sIAAAAAAAC/6RSzY7aQAx+lcjnKAToKbetemHVA1LVU1Uhk5gwdDKejp1lo1XevZqkZElB2kpwIJnJ", "Z/v78RuU3Hh25FSgeAMpj9Tg8Pp81i12lrGKJx/YU1BDw7fKiLfY7Rw2FM/0io23BAU889ElX5ggBe18", - "vBENxtXQp2BKdjuPepyXLEyDNcnixEeXnXx9t1R2WDXGzSoPaIUm8J7ZErqIboXCzlQz8PJpNUGNU6op", - "XKC3KiKVWxp9CoF+tyZQBcWPacpVk3TuzBXvn1M33p+oVOhjO+MOHCer0WHu2u5RA4skkWJwaJMz7ZP1", - "dgMpvFAQww4KyLNllkf27MmhN1DAKsuzHFKI9g4RLdCbheV69MyzaHzGEFENu00FBWxZdO3N1wE1iiPR", - "z1x1EVuyU3JDGXpvTTkULk7C7n1VbnfDo8iZw9z96Xb5tLqX74Mx/HV7Gn3f7fcqDS0NF+LZycj7Kc8f", - "UK38i+bbCa/xl139fyhlbHKffEVSBuN13IBvbVmSyKG1tkuw1SM5jVSpim5+ypcPSGlIBOt/oti4F7Sm", - "SspAVZyFVj6Uc2n0P4Iu/S9pJhySKc4Il7ZpMHRQwHehkIyb3fd9/ycAAP//0SNLNsMEAAA=", + "vBENxtXQp2BKdjuPepyXLEyDNcnixEeXnXx9t1R2WDXGzSoPaIUm8J7ZErqIboXCzlQz8HK1nqDGKdUU", + "LtBbFZHKLY0+hUC/WxOoguLHNOWqSTp35or3z6kb709UKvSxnXEHjpPV6DD3ye5RA4skkWJwaJMz7ZOn", + "7QZSeKEghh0UkGfLLI/s2ZNDb6CAdZZnOaQQ7R0iWliuR788i8ZnDBDVsNtUUMCWRb8OkFEViX7mqovA", + "kp2SG2rQe2vKoWpxEnbvO3K7FB5Fzhzmtk+3y9X6XrAP+v/X5mn0fZvfqzS0NFyIZycj71WeP6Ba+RfN", + "1xJe4y+7+v9QytjkPvmKpAzG6xj9t7YsSeTQWtsl2OqRnEaqVEU3P+XLB6Q0JIL1P1Fs3AtaUyVloCrO", + "Qisfyrk0+h9Bl/6XNBMOyRRnhEvbNBg6KOC7UEjGte77vv8TAAD//y1cIMC8BAAA", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/backend/api/handlers.go b/backend/api/handlers.go index ee0a97a..162e30a 100644 --- a/backend/api/handlers.go +++ b/backend/api/handlers.go @@ -21,19 +21,19 @@ func NewHandler(queries *db.Queries) *ApiHandler { } } -func (h *ApiHandler) PostApiLogin(ctx context.Context, request PostApiLoginRequestObject) (PostApiLoginResponseObject, error) { +func (h *ApiHandler) PostLogin(ctx context.Context, request PostLoginRequestObject) (PostLoginResponseObject, error) { username := request.Body.Username password := request.Body.Password userId, err := auth.Login(ctx, h.q, username, password) if err != nil { - return PostApiLogin401JSONResponse{ + return PostLogin401JSONResponse{ Message: "Invalid username or password", }, echo.NewHTTPError(http.StatusUnauthorized, "Invalid username or password") } user, err := h.q.GetUserById(ctx, int32(userId)) if err != nil { - return PostApiLogin401JSONResponse{ + return PostLogin401JSONResponse{ Message: "Invalid username or password", }, echo.NewHTTPError(http.StatusUnauthorized, "Invalid username or password") } @@ -41,12 +41,12 @@ func (h *ApiHandler) PostApiLogin(ctx context.Context, request PostApiLoginReque jwt, err := auth.NewJWT(&user) if err != nil { // TODO - return PostApiLogin401JSONResponse{ + return PostLogin401JSONResponse{ Message: "Internal Server Error", }, echo.NewHTTPError(http.StatusInternalServerError, "Internal Server Error") } - return PostApiLogin200JSONResponse{ + return PostLogin200JSONResponse{ Token: jwt, }, nil } @@ -64,7 +64,7 @@ func _assertJwtPayloadIsCompatibleWithJWTClaims() { func NewJWTMiddleware() StrictMiddlewareFunc { return func(handler StrictHandlerFunc, operationID string) StrictHandlerFunc { - if operationID == "PostApiLogin" { + if operationID == "PostLogin" { return handler } else { return func(c echo.Context, request interface{}) (response interface{}, err error) { diff --git a/backend/api/workaround.go b/backend/api/workaround.go new file mode 100644 index 0000000..a3c47d7 --- /dev/null +++ b/backend/api/workaround.go @@ -0,0 +1,22 @@ +package api + +import ( + "github.com/getkin/kin-openapi/openapi3" +) + +// Work-around for this issue: +// https://stackoverflow.com/questions/70087465/echo-groups-not-working-with-openapi-generated-code-using-oapi-codegen +func GetSwaggerWithPrefix(prefix string) (*openapi3.T, error) { + spec, err := GetSwagger() + if err != nil { + return nil, err + } + + var prefixedPaths openapi3.Paths = openapi3.Paths{} + for key, value := range spec.Paths.Map() { + prefixedPaths.Set(prefix+key, value) + } + + spec.Paths = &prefixedPaths + return spec, nil +} diff --git a/backend/main.go b/backend/main.go index 1787e3c..7f87bb4 100644 --- a/backend/main.go +++ b/backend/main.go @@ -127,7 +127,7 @@ func main() { return } - openApiSpec, err := api.GetSwagger() + openApiSpec, err := api.GetSwaggerWithPrefix("/api") if err != nil { fmt.Printf("Error loading OpenAPI spec\n: %s", err) return |
