From a5ceb0af938d32078007a6cd04ae2f613b3d1d20 Mon Sep 17 00:00:00 2001 From: Rogee Date: Thu, 13 Aug 2026 18:25:19 +0800 Subject: [PATCH] H-47: unblock connector sandbox setup (#11) Co-authored-by: Rogee --- .../handler/api/v1/platform_handler.go | 30 ++++++++++++------- .../handler/api/v1/platform_handler_test.go | 21 +++++++++++++ deploy/docker/Dockerfile | 4 +-- 3 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 backend/internal/handler/api/v1/platform_handler_test.go diff --git a/backend/internal/handler/api/v1/platform_handler.go b/backend/internal/handler/api/v1/platform_handler.go index f1660748..c7c7d622 100644 --- a/backend/internal/handler/api/v1/platform_handler.go +++ b/backend/internal/handler/api/v1/platform_handler.go @@ -21,6 +21,14 @@ type PlatformAppHandler struct { svc *service.PlatformAppService } +func platformAppID(c *gin.Context) (uint, error) { + name := "id" + if c.Param(name) == "" { + name = "platform_app_id" + } + return parseUintParam(c, name) +} + // NewPlatformAppHandler creates a new PlatformApp handler with service injection. func NewPlatformAppHandler(svc *service.PlatformAppService) *PlatformAppHandler { return &PlatformAppHandler{svc: svc} @@ -67,7 +75,7 @@ func (h *PlatformAppHandler) List(c *gin.Context) { // GET /api/v1/accounts/:account_id/platform_apps/:id // GET /platform/api/v1/apps/:id func (h *PlatformAppHandler) Get(c *gin.Context) { - id, err := parseUintParam(c, "id") + id, err := platformAppID(c) if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid platform app ID") return @@ -109,8 +117,8 @@ func (h *PlatformAppHandler) Create(c *gin.Context) { } // Return app + plaintext token (this is the only time the plaintext token is shown) response.Created(c, gin.H{ - "platform_app": app, - "access_token": plainToken, // plaintext — shown only once + "platform_app": app, + "access_token": plainToken, // plaintext — shown only once }) } @@ -118,7 +126,7 @@ func (h *PlatformAppHandler) Create(c *gin.Context) { // PUT /api/v1/accounts/:account_id/platform_apps/:id // PUT /platform/api/v1/apps/:id func (h *PlatformAppHandler) Update(c *gin.Context) { - id, err := parseUintParam(c, "id") + id, err := platformAppID(c) if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid platform app ID") return @@ -141,7 +149,7 @@ func (h *PlatformAppHandler) Update(c *gin.Context) { // DELETE /api/v1/accounts/:account_id/platform_apps/:id // DELETE /platform/api/v1/apps/:id func (h *PlatformAppHandler) Delete(c *gin.Context) { - id, err := parseUintParam(c, "id") + id, err := platformAppID(c) if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid platform app ID") return @@ -161,7 +169,7 @@ func (h *PlatformAppHandler) Delete(c *gin.Context) { // Reference: Chatwoot PlatformAppsController#regenerate_api_key // Returns the plaintext token (shown only once, never stored). func (h *PlatformAppHandler) RegenerateAccessToken(c *gin.Context) { - id, err := parseUintParam(c, "id") + id, err := platformAppID(c) if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid platform app ID") return @@ -181,7 +189,7 @@ func (h *PlatformAppHandler) RegenerateAccessToken(c *gin.Context) { // GET /platform/api/v1/apps/:id/access_tokens // GET /api/v1/accounts/:account_id/platform_apps/:id/access_tokens func (h *PlatformAppHandler) ListAccessTokens(c *gin.Context) { - id, err := parseUintParam(c, "id") + id, err := platformAppID(c) if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid platform app ID") return @@ -203,7 +211,7 @@ func (h *PlatformAppHandler) ListAccessTokens(c *gin.Context) { // Body: { "permissible_type": "Account|User|AgentBot", "permissible_id": 123 } // Reference: Chatwoot PlatformAppsController#add_permissible func (h *PlatformAppHandler) AddPermissible(c *gin.Context) { - id, err := parseUintParam(c, "id") + id, err := platformAppID(c) if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid platform app ID") return @@ -229,7 +237,7 @@ func (h *PlatformAppHandler) AddPermissible(c *gin.Context) { // Query params: permissible_type=Account|User|AgentBot // Reference: Chatwoot PlatformAppsController#remove_permissible func (h *PlatformAppHandler) RemovePermissible(c *gin.Context) { - id, err := parseUintParam(c, "id") + id, err := platformAppID(c) if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid platform app ID") return @@ -259,7 +267,7 @@ func (h *PlatformAppHandler) RemovePermissible(c *gin.Context) { // GET /platform/api/v1/apps/:id/permissibles // GET /api/v1/accounts/:account_id/platform_apps/:id/permissibles func (h *PlatformAppHandler) ListPermissibles(c *gin.Context) { - id, err := parseUintParam(c, "id") + id, err := platformAppID(c) if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid platform app ID") return @@ -321,4 +329,4 @@ type addPermissibleRequest struct { // --- Helper: validate permissible_type --- func isValidPermissibleTypeStr(t string) bool { return t == model.PermissibleTypeAccount || t == model.PermissibleTypeUser || t == model.PermissibleTypeAgentBot -} \ No newline at end of file +} diff --git a/backend/internal/handler/api/v1/platform_handler_test.go b/backend/internal/handler/api/v1/platform_handler_test.go new file mode 100644 index 00000000..93f979da --- /dev/null +++ b/backend/internal/handler/api/v1/platform_handler_test.go @@ -0,0 +1,21 @@ +package v1 + +import ( + "testing" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/require" +) + +func TestPlatformAppIDSupportsBothRoutes(t *testing.T) { + for _, params := range []gin.Param{ + {Key: "id", Value: "7"}, + {Key: "platform_app_id", Value: "7"}, + } { + ctx, _ := gin.CreateTestContext(nil) + ctx.Params = []gin.Param{params} + id, err := platformAppID(ctx) + require.NoError(t, err) + require.Equal(t, uint(7), id) + } +} diff --git a/deploy/docker/Dockerfile b/deploy/docker/Dockerfile index 812ef5ce..946b219f 100644 --- a/deploy/docker/Dockerfile +++ b/deploy/docker/Dockerfile @@ -48,8 +48,8 @@ WORKDIR /app # Copy binary and configs from builder COPY --from=builder /gochat /app/gochat COPY --from=builder /gochat-worker /app/gochat-worker -COPY configs/ /app/configs/ -COPY migrations/ /app/migrations/ +COPY backend/configs/ /app/configs/ +COPY backend/migrations/ /app/migrations/ # Set ownership to non-root user RUN mkdir -p /app/storage/uploads && chown -R gochat:gochat /app