diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-28 02:11:02 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-28 02:11:02 +0900 |
| commit | 6603d3ea5a54647e8eda8ec253835eb7a36e5eb2 (patch) | |
| tree | 3a1e823bf464d5b291c0a1cdf004af72eeb10de1 /backend/main.go | |
| parent | ec5fa50956c7f7f7f57c2dde4fb85ad0f3eb441f (diff) | |
| download | iosdc-japan-2024-albatross-6603d3ea5a54647e8eda8ec253835eb7a36e5eb2.tar.gz iosdc-japan-2024-albatross-6603d3ea5a54647e8eda8ec253835eb7a36e5eb2.tar.zst iosdc-japan-2024-albatross-6603d3ea5a54647e8eda8ec253835eb7a36e5eb2.zip | |
backend: openapi
Diffstat (limited to 'backend/main.go')
| -rw-r--r-- | backend/main.go | 59 |
1 files changed, 15 insertions, 44 deletions
diff --git a/backend/main.go b/backend/main.go index e878bed..fa5c079 100644 --- a/backend/main.go +++ b/backend/main.go @@ -10,8 +10,9 @@ import ( "github.com/jackc/pgx/v5" "github.com/labstack/echo/v4" + oapimiddleware "github.com/oapi-codegen/echo-middleware" - "github.com/nsfisis/iosdc-2024-albatross-backend/auth" + "github.com/nsfisis/iosdc-2024-albatross-backend/api" "github.com/nsfisis/iosdc-2024-albatross-backend/db" ) @@ -117,45 +118,6 @@ func handleGolfPost(w http.ResponseWriter, r *http.Request) { } */ -func handleApiLogin(c echo.Context, queries *db.Queries) error { - type LoginRequestData struct { - Username string `json:"username"` - Password string `json:"password"` - } - - type LoginResponseData struct { - Token string `json:"token"` - } - - ctx := c.Request().Context() - - requestData := new(LoginRequestData) - if err := c.Bind(requestData); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, err.Error()) - } - - userId, err := auth.Login(ctx, queries, requestData.Username, requestData.Password) - if err != nil { - return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) - } - - user, err := queries.GetUserById(ctx, int32(userId)) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) - } - - jwt, err := auth.NewJWT(&user) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) - } - - responseData := LoginResponseData{ - Token: jwt, - } - - return c.JSON(http.StatusOK, responseData) -} - func main() { var err error config, err = loadEnv() @@ -164,6 +126,12 @@ func main() { return } + openApiSpec, err := api.GetSwagger() + if err != nil { + fmt.Printf("Error loading OpenAPI spec\n: %s", err) + return + } + ctx := context.Background() conn, err := pgx.Connect(ctx, fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", config.dbHost, config.dbPort, config.dbUser, config.dbPassword, config.dbName)) @@ -176,6 +144,13 @@ func main() { e := echo.New() + { + apiGroup := e.Group("/api") + apiGroup.Use(oapimiddleware.OapiRequestValidator(openApiSpec)) + apiHandler := api.NewHandler(queries) + api.RegisterHandlers(apiGroup, api.NewStrictHandler(apiHandler, nil)) + } + e.GET("/sock/golf/:gameId/watch", func(c echo.Context) error { gameId := c.Param("gameId") gameIdInt, err := strconv.Atoi(gameId) @@ -214,10 +189,6 @@ func main() { return serveWs(hub, c.Response(), c.Request(), "a") }) - e.POST("/api/login", func(c echo.Context) error { - return handleApiLogin(c, queries) - }) - defer func() { for _, hub := range gameHubs { hub.Close() |
