mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(core): implement sync outbox mechanism and refactor provider validation
- Introduce `SyncOutboxService` and model to retry failed CP-to-Redis sync operations - Update `SyncService` to handle sync failures by enqueuing tasks to the outbox - Centralize provider group and API key validation logic into `ProviderGroupManager` - Refactor API handlers to utilize the new manager and robust sync methods - Add configuration options for sync outbox (interval, batch size, retries)
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
"github.com/ez-api/ez-api/internal/dto"
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/ez-api/foundation/provider"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -37,64 +36,35 @@ func (h *Handler) CreateProviderGroup(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ptype := provider.NormalizeType(req.Type)
|
||||
if ptype == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "type required"})
|
||||
return
|
||||
}
|
||||
|
||||
baseURL := strings.TrimSpace(req.BaseURL)
|
||||
googleLocation := provider.DefaultGoogleLocation(ptype, req.GoogleLocation)
|
||||
|
||||
switch ptype {
|
||||
case provider.TypeOpenAI:
|
||||
if baseURL == "" {
|
||||
baseURL = "https://api.openai.com/v1"
|
||||
}
|
||||
case provider.TypeAnthropic, provider.TypeClaude:
|
||||
if baseURL == "" {
|
||||
baseURL = "https://api.anthropic.com"
|
||||
}
|
||||
case provider.TypeCompatible:
|
||||
if baseURL == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "base_url required for compatible providers"})
|
||||
return
|
||||
}
|
||||
default:
|
||||
if provider.IsVertexFamily(ptype) && strings.TrimSpace(googleLocation) == "" {
|
||||
googleLocation = provider.DefaultGoogleLocation(ptype, "")
|
||||
}
|
||||
}
|
||||
|
||||
status := strings.TrimSpace(req.Status)
|
||||
if status == "" {
|
||||
status = "active"
|
||||
}
|
||||
|
||||
group := model.ProviderGroup{
|
||||
Name: name,
|
||||
Type: strings.TrimSpace(req.Type),
|
||||
BaseURL: baseURL,
|
||||
BaseURL: strings.TrimSpace(req.BaseURL),
|
||||
GoogleProject: strings.TrimSpace(req.GoogleProject),
|
||||
GoogleLocation: googleLocation,
|
||||
GoogleLocation: strings.TrimSpace(req.GoogleLocation),
|
||||
Models: strings.Join(req.Models, ","),
|
||||
Status: status,
|
||||
Status: strings.TrimSpace(req.Status),
|
||||
}
|
||||
if err := h.db.Create(&group).Error; err != nil {
|
||||
normalized, err := h.groupManager.NormalizeGroup(group)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.db.Create(&normalized).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create provider group", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProviders(h.db); err != nil {
|
||||
if err := h.sync.SyncProvidersForGroup(h.db, normalized.ID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync providers", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
if err := h.sync.SyncBindingsForGroup(h.db, normalized.ID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, group)
|
||||
c.JSON(http.StatusCreated, normalized)
|
||||
}
|
||||
|
||||
// ListProviderGroups godoc
|
||||
@@ -181,71 +151,52 @@ func (h *Handler) UpdateProviderGroup(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
nextType := strings.TrimSpace(group.Type)
|
||||
if t := strings.TrimSpace(req.Type); t != "" {
|
||||
nextType = t
|
||||
}
|
||||
nextTypeLower := provider.NormalizeType(nextType)
|
||||
nextBaseURL := strings.TrimSpace(group.BaseURL)
|
||||
if strings.TrimSpace(req.BaseURL) != "" {
|
||||
nextBaseURL = strings.TrimSpace(req.BaseURL)
|
||||
}
|
||||
|
||||
update := map[string]any{}
|
||||
next := group
|
||||
if strings.TrimSpace(req.Name) != "" {
|
||||
update["name"] = strings.TrimSpace(req.Name)
|
||||
next.Name = strings.TrimSpace(req.Name)
|
||||
}
|
||||
if strings.TrimSpace(req.Type) != "" {
|
||||
update["type"] = strings.TrimSpace(req.Type)
|
||||
next.Type = strings.TrimSpace(req.Type)
|
||||
}
|
||||
if strings.TrimSpace(req.BaseURL) != "" {
|
||||
update["base_url"] = strings.TrimSpace(req.BaseURL)
|
||||
next.BaseURL = strings.TrimSpace(req.BaseURL)
|
||||
}
|
||||
if strings.TrimSpace(req.GoogleProject) != "" {
|
||||
update["google_project"] = strings.TrimSpace(req.GoogleProject)
|
||||
next.GoogleProject = strings.TrimSpace(req.GoogleProject)
|
||||
}
|
||||
if strings.TrimSpace(req.GoogleLocation) != "" {
|
||||
update["google_location"] = strings.TrimSpace(req.GoogleLocation)
|
||||
} else if provider.IsVertexFamily(nextTypeLower) && strings.TrimSpace(group.GoogleLocation) == "" {
|
||||
update["google_location"] = provider.DefaultGoogleLocation(nextTypeLower, "")
|
||||
next.GoogleLocation = strings.TrimSpace(req.GoogleLocation)
|
||||
}
|
||||
if req.Models != nil {
|
||||
update["models"] = strings.Join(req.Models, ",")
|
||||
next.Models = strings.Join(req.Models, ",")
|
||||
}
|
||||
if strings.TrimSpace(req.Status) != "" {
|
||||
update["status"] = strings.TrimSpace(req.Status)
|
||||
next.Status = strings.TrimSpace(req.Status)
|
||||
}
|
||||
|
||||
switch nextTypeLower {
|
||||
case provider.TypeOpenAI:
|
||||
if nextBaseURL == "" {
|
||||
update["base_url"] = "https://api.openai.com/v1"
|
||||
}
|
||||
case provider.TypeAnthropic, provider.TypeClaude:
|
||||
if nextBaseURL == "" {
|
||||
update["base_url"] = "https://api.anthropic.com"
|
||||
}
|
||||
case provider.TypeCompatible:
|
||||
if nextBaseURL == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "base_url required for compatible providers"})
|
||||
return
|
||||
}
|
||||
normalized, err := h.groupManager.NormalizeGroup(next)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
group.Name = normalized.Name
|
||||
group.Type = normalized.Type
|
||||
group.BaseURL = normalized.BaseURL
|
||||
group.GoogleProject = normalized.GoogleProject
|
||||
group.GoogleLocation = normalized.GoogleLocation
|
||||
group.Models = normalized.Models
|
||||
group.Status = normalized.Status
|
||||
|
||||
if err := h.db.Model(&group).Updates(update).Error; err != nil {
|
||||
if err := h.db.Save(&group).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update provider group", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.db.First(&group, id).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to reload provider group", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProviders(h.db); err != nil {
|
||||
if err := h.sync.SyncProvidersForGroup(h.db, group.ID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync providers", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
if err := h.sync.SyncBindingsForGroup(h.db, group.ID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -289,11 +240,11 @@ func (h *Handler) DeleteProviderGroup(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProviders(h.db); err != nil {
|
||||
if err := h.sync.SyncProvidersForGroup(h.db, group.ID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync providers", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
if err := h.sync.SyncBindingsForGroup(h.db, group.ID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user