From ff963e36d7dbb385cfba6aa333dc7da7c63e5479 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 24 Jun 2023 22:57:16 +0800 Subject: [PATCH] chore: update workspace api --- api/v1/jwt.go | 2 +- api/v1/system.go | 39 ------------------- api/v1/v1.go | 3 +- api/v1/{workspace_setting.go => workspace.go} | 27 ++++++++++++- web/src/helpers/api.ts | 2 +- 5 files changed, 29 insertions(+), 44 deletions(-) delete mode 100644 api/v1/system.go rename api/v1/{workspace_setting.go => workspace.go} (81%) diff --git a/api/v1/jwt.go b/api/v1/jwt.go index 3b22748..7dd54b5 100644 --- a/api/v1/jwt.go +++ b/api/v1/jwt.go @@ -84,7 +84,7 @@ func JWTMiddleware(server *APIV1Service, next echo.HandlerFunc, secret string) e token := findAccessToken(c) if token == "" { // When the request is not authenticated, we allow the user to access the shortcut endpoints for those public shortcuts. - if util.HasPrefixes(path, "/api/v1/status", "/s/*") && method == http.MethodGet { + if util.HasPrefixes(path, "/api/v1/workspace/status", "/s/*") && method == http.MethodGet { return next(c) } return echo.NewHTTPError(http.StatusUnauthorized, "Missing access token") diff --git a/api/v1/system.go b/api/v1/system.go deleted file mode 100644 index 71b25c0..0000000 --- a/api/v1/system.go +++ /dev/null @@ -1,39 +0,0 @@ -package v1 - -import ( - "net/http" - - "github.com/boojack/shortify/server/profile" - "github.com/boojack/shortify/store" - "github.com/labstack/echo/v4" -) - -type SystemStatus struct { - Profile *profile.Profile `json:"profile"` - DisallowSignUp bool `json:"disallowSignUp"` -} - -func (s *APIV1Service) registerSystemRoutes(g *echo.Group) { - g.GET("/ping", func(c echo.Context) error { - return c.JSON(http.StatusOK, s.Profile) - }) - - g.GET("/status", func(c echo.Context) error { - ctx := c.Request().Context() - systemStatus := SystemStatus{ - Profile: s.Profile, - } - - disallowSignUpSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{ - Key: WorkspaceDisallowSignUp.String(), - }) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get workspace setting") - } - if disallowSignUpSetting != nil { - systemStatus.DisallowSignUp = disallowSignUpSetting.Value == "true" - } - - return c.JSON(http.StatusOK, systemStatus) - }) -} diff --git a/api/v1/v1.go b/api/v1/v1.go index 7f3f72d..ec9fcb5 100644 --- a/api/v1/v1.go +++ b/api/v1/v1.go @@ -24,8 +24,7 @@ func (s *APIV1Service) Start(apiGroup *echo.Group, secret string) { apiV1Group.Use(func(next echo.HandlerFunc) echo.HandlerFunc { return JWTMiddleware(s, next, string(secret)) }) - s.registerSystemRoutes(apiV1Group) - s.registerWorkspaceSettingRoutes(apiV1Group) + s.registerWorkspaceRoutes(apiV1Group) s.registerAuthRoutes(apiV1Group, secret) s.registerUserRoutes(apiV1Group) s.registerShortcutRoutes(apiV1Group) diff --git a/api/v1/workspace_setting.go b/api/v1/workspace.go similarity index 81% rename from api/v1/workspace_setting.go rename to api/v1/workspace.go index cc448f6..d2cc034 100644 --- a/api/v1/workspace_setting.go +++ b/api/v1/workspace.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" + "github.com/boojack/shortify/server/profile" "github.com/boojack/shortify/store" "github.com/labstack/echo/v4" ) @@ -48,7 +49,31 @@ func (upsert WorkspaceSettingUpsert) Validate() error { return nil } -func (s *APIV1Service) registerWorkspaceSettingRoutes(g *echo.Group) { +type WorkspaceStatus struct { + Profile *profile.Profile `json:"profile"` + DisallowSignUp bool `json:"disallowSignUp"` +} + +func (s *APIV1Service) registerWorkspaceRoutes(g *echo.Group) { + g.GET("/workspace/status", func(c echo.Context) error { + ctx := c.Request().Context() + workspaceStatus := WorkspaceStatus{ + Profile: s.Profile, + } + + disallowSignUpSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{ + Key: WorkspaceDisallowSignUp.String(), + }) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get workspace setting") + } + if disallowSignUpSetting != nil { + workspaceStatus.DisallowSignUp = disallowSignUpSetting.Value == "true" + } + + return c.JSON(http.StatusOK, workspaceStatus) + }) + g.POST("/workspace/setting", func(c echo.Context) error { ctx := c.Request().Context() userID, ok := c.Get(getUserIDContextKey()).(int) diff --git a/web/src/helpers/api.ts b/web/src/helpers/api.ts index 50adfca..f5ed61d 100644 --- a/web/src/helpers/api.ts +++ b/web/src/helpers/api.ts @@ -1,7 +1,7 @@ import axios from "axios"; export function getSystemStatus() { - return axios.get("/api/v1/status"); + return axios.get("/api/v1/workspace/status"); } export function signin(email: string, password: string) {