package registry import ( "context" "encoding/json" "gitea.loveuer.com/loveuer/cluster/internal/model" "gitea.loveuer.com/loveuer/cluster/pkg/resp" "gitea.loveuer.com/loveuer/cluster/pkg/store" "github.com/gofiber/fiber/v3" "gorm.io/gorm" ) // RegistryConfigGet returns the registry configuration func RegistryConfigGet(ctx context.Context, db *gorm.DB, store store.Store) fiber.Handler { return func(c fiber.Ctx) error { var configs []model.RegistryConfig if err := db.Find(&configs).Error; err != nil { return resp.R500(c, "", nil, err) } // Convert to map for easier frontend access configMap := make(map[string]string) for _, config := range configs { configMap[config.Key] = config.Value } return resp.R200(c, map[string]interface{}{ "configs": configMap, }) } } // RegistryConfigSet sets a registry configuration value func RegistryConfigSet(ctx context.Context, db *gorm.DB, store store.Store) fiber.Handler { return func(c fiber.Ctx) error { var req struct { Key string `json:"key"` Value string `json:"value"` } // Parse JSON body body := c.Body() if len(body) == 0 { return resp.R400(c, "EMPTY_BODY", nil, "request body is empty") } if err := json.Unmarshal(body, &req); err != nil { return resp.R400(c, "INVALID_REQUEST", nil, "invalid request body") } if req.Key == "" { return resp.R400(c, "MISSING_KEY", nil, "key is required") } // Find or create config var config model.RegistryConfig err := db.Where("key = ?", req.Key).First(&config).Error if err == gorm.ErrRecordNotFound { // Create new config config = model.RegistryConfig{ Key: req.Key, Value: req.Value, } if err := db.Create(&config).Error; err != nil { return resp.R500(c, "", nil, err) } } else if err != nil { return resp.R500(c, "", nil, err) } else { // Update existing config config.Value = req.Value if err := db.Save(&config).Error; err != nil { return resp.R500(c, "", nil, err) } } return resp.R200(c, map[string]interface{}{ "config": config, }) } }