veza/veza-backend-api/internal/handlers/feature_flag_handler.go

75 lines
2.2 KiB
Go
Raw Normal View History

package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
2026-03-06 23:54:35 +00:00
apperrors "veza-backend-api/internal/errors"
"veza-backend-api/internal/services"
)
// FeatureFlagHandler handles feature flag endpoints (v0.803 ADM1-05)
type FeatureFlagHandler struct {
svc *services.FeatureFlagService
}
// NewFeatureFlagHandler creates a new FeatureFlagHandler
func NewFeatureFlagHandler(svc *services.FeatureFlagService) *FeatureFlagHandler {
return &FeatureFlagHandler{svc: svc}
}
// List returns all feature flags (admin)
// @Summary List all feature flags
// @Description Get a list of all feature flags and their current status. Admin only.
// @Tags Admin
// @Accept json
// @Produce json
// @Security BearerAuth
// @Success 200 {object} object{feature_flags=array}
// @Failure 401 {object} handlers.APIResponse "Unauthorized"
// @Router /api/v1/admin/feature-flags [get]
func (h *FeatureFlagHandler) List(c *gin.Context) {
list, err := h.svc.List(c.Request.Context())
if err != nil {
2026-03-06 23:54:35 +00:00
RespondWithAppError(c, apperrors.NewInternalErrorWrap("Failed to list feature flags", err))
return
}
RespondSuccess(c, http.StatusOK, gin.H{"feature_flags": list})
}
// Toggle enables or disables a feature flag (admin)
// @Summary Toggle feature flag
// @Description Enable or disable a specific feature flag. Admin only.
// @Tags Admin
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param name path string true "Flag name"
// @Param data body object{enabled=boolean} true "Toggle data"
// @Success 200 {object} models.FeatureFlag
// @Failure 401 {object} handlers.APIResponse "Unauthorized"
// @Router /api/v1/admin/feature-flags/{name}/toggle [put]
func (h *FeatureFlagHandler) Toggle(c *gin.Context) {
name := c.Param("name")
if name == "" {
2026-03-06 23:54:35 +00:00
RespondWithAppError(c, apperrors.NewValidationError("name is required"))
return
}
var req struct {
Enabled bool `json:"enabled"`
}
if err := c.ShouldBindJSON(&req); err != nil {
2026-03-06 23:54:35 +00:00
RespondWithAppError(c, apperrors.NewValidationError("enabled is required"))
return
}
flag, err := h.svc.Toggle(c.Request.Context(), name, req.Enabled)
if err != nil {
2026-03-06 23:54:35 +00:00
RespondWithAppError(c, apperrors.NewInternalErrorWrap("Failed to toggle feature flag", err))
return
}
c.JSON(http.StatusOK, flag)
}