mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
refactor(api): split Provider into ProviderGroup and APIKey models
Restructure the provider management system by separating the monolithic Provider model into two distinct entities: - ProviderGroup: defines shared upstream configuration (type, base_url, google settings, models, status) - APIKey: represents individual credentials within a group (api_key, weight, status, auto_ban, ban settings) This change also updates: - Binding model to reference GroupID instead of RouteGroup string - All CRUD handlers for the new provider-group and api-key endpoints - Sync service to rebuild provider snapshots from joined tables - Model registry to aggregate capabilities across group/key pairs - Access handler to validate namespace existence and subset constraints - Migration importer to handle the new schema structure - All related tests to use the new model relationships BREAKING CHANGE: Provider API endpoints replaced with /provider-groups and /api-keys endpoints; Binding.RouteGroup replaced with Binding.GroupID
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AccessResponse struct {
|
||||
@@ -94,6 +96,10 @@ func (h *Handler) UpdateMasterAccess(c *gin.Context) {
|
||||
}
|
||||
nsList := normalizeNamespaces(nextNamespaces, nextDefault)
|
||||
nextNamespaces = strings.Join(nsList, ",")
|
||||
if err := ensureNamespacesExist(h.db, nsList); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.Model(&m).Updates(map[string]any{
|
||||
"default_namespace": nextDefault,
|
||||
@@ -203,6 +209,21 @@ func (h *Handler) UpdateKeyAccess(c *gin.Context) {
|
||||
}
|
||||
nsList := normalizeNamespaces(nextNamespaces, nextDefault)
|
||||
nextNamespaces = strings.Join(nsList, ",")
|
||||
if err := ensureNamespacesExist(h.db, nsList); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var master model.Master
|
||||
if err := h.db.First(&master, k.MasterID).Error; err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "master not found"})
|
||||
return
|
||||
}
|
||||
masterNamespaces := normalizeNamespaces(master.Namespaces, master.DefaultNamespace)
|
||||
if !isSubset(nsList, masterNamespaces) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "namespaces must be a subset of master namespaces"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.Model(&k).Updates(map[string]any{
|
||||
"default_namespace": nextDefault,
|
||||
@@ -264,6 +285,39 @@ func normalizeNamespaces(raw string, defaultNamespace string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
func ensureNamespacesExist(db *gorm.DB, namespaces []string) error {
|
||||
if db == nil {
|
||||
return fmt.Errorf("db required")
|
||||
}
|
||||
if len(namespaces) == 0 {
|
||||
return fmt.Errorf("namespaces required")
|
||||
}
|
||||
var rows []model.Namespace
|
||||
if err := db.Where("name IN ?", namespaces).Find(&rows).Error; err != nil {
|
||||
return fmt.Errorf("failed to load namespaces")
|
||||
}
|
||||
if len(rows) != len(namespaces) {
|
||||
return fmt.Errorf("namespace not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isSubset(child, parent []string) bool {
|
||||
if len(child) == 0 {
|
||||
return true
|
||||
}
|
||||
parentSet := make(map[string]struct{}, len(parent))
|
||||
for _, p := range parent {
|
||||
parentSet[strings.TrimSpace(p)] = struct{}{}
|
||||
}
|
||||
for _, c := range child {
|
||||
if _, ok := parentSet[strings.TrimSpace(c)]; !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func parseUintParam(c *gin.Context, name string) (uint, bool) {
|
||||
idRaw := strings.TrimSpace(c.Param(name))
|
||||
if idRaw == "" {
|
||||
|
||||
259
internal/api/api_key_handler.go
Normal file
259
internal/api/api_key_handler.go
Normal file
@@ -0,0 +1,259 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// CreateAPIKey godoc
|
||||
// @Summary Create an API key
|
||||
// @Description Create an API key for a provider group
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param key body dto.APIKeyDTO true "API key payload"
|
||||
// @Success 201 {object} model.APIKey
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/api-keys [post]
|
||||
func (h *Handler) CreateAPIKey(c *gin.Context) {
|
||||
var req dto.APIKeyDTO
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if req.GroupID == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "group_id required"})
|
||||
return
|
||||
}
|
||||
var group model.ProviderGroup
|
||||
if err := h.db.First(&group, req.GroupID).Error; err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "provider group not found"})
|
||||
return
|
||||
}
|
||||
|
||||
apiKey := strings.TrimSpace(req.APIKey)
|
||||
ptype := provider.NormalizeType(group.Type)
|
||||
if provider.IsGoogleFamily(ptype) && !provider.IsVertexFamily(ptype) && apiKey == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "api_key required for gemini api providers"})
|
||||
return
|
||||
}
|
||||
|
||||
status := strings.TrimSpace(req.Status)
|
||||
if status == "" {
|
||||
status = "active"
|
||||
}
|
||||
autoBan := true
|
||||
if req.AutoBan != nil {
|
||||
autoBan = *req.AutoBan
|
||||
}
|
||||
|
||||
key := model.APIKey{
|
||||
GroupID: req.GroupID,
|
||||
APIKey: apiKey,
|
||||
Weight: normalizeWeight(req.Weight),
|
||||
Status: status,
|
||||
AutoBan: autoBan,
|
||||
BanReason: strings.TrimSpace(req.BanReason),
|
||||
}
|
||||
if !req.BanUntil.IsZero() {
|
||||
tu := req.BanUntil.UTC()
|
||||
key.BanUntil = &tu
|
||||
}
|
||||
|
||||
if err := h.db.Create(&key).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create api key", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProviders(h.db); 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 {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, key)
|
||||
}
|
||||
|
||||
// ListAPIKeys godoc
|
||||
// @Summary List API keys
|
||||
// @Description List API keys
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param page query int false "page (1-based)"
|
||||
// @Param limit query int false "limit (default 50, max 200)"
|
||||
// @Param group_id query int false "filter by group_id"
|
||||
// @Success 200 {array} model.APIKey
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/api-keys [get]
|
||||
func (h *Handler) ListAPIKeys(c *gin.Context) {
|
||||
var keys []model.APIKey
|
||||
q := h.db.Model(&model.APIKey{}).Order("id desc")
|
||||
if groupID := strings.TrimSpace(c.Query("group_id")); groupID != "" {
|
||||
q = q.Where("group_id = ?", groupID)
|
||||
}
|
||||
query := parseListQuery(c)
|
||||
q = applyListPagination(q, query)
|
||||
if err := q.Find(&keys).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list api keys", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, keys)
|
||||
}
|
||||
|
||||
// GetAPIKey godoc
|
||||
// @Summary Get API key
|
||||
// @Description Get an API key by id
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "APIKey ID"
|
||||
// @Success 200 {object} model.APIKey
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/api-keys/{id} [get]
|
||||
func (h *Handler) GetAPIKey(c *gin.Context) {
|
||||
id, ok := parseUintParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var key model.APIKey
|
||||
if err := h.db.First(&key, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "api key not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, key)
|
||||
}
|
||||
|
||||
// UpdateAPIKey godoc
|
||||
// @Summary Update API key
|
||||
// @Description Update an API key
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "APIKey ID"
|
||||
// @Param key body dto.APIKeyDTO true "API key payload"
|
||||
// @Success 200 {object} model.APIKey
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/api-keys/{id} [put]
|
||||
func (h *Handler) UpdateAPIKey(c *gin.Context) {
|
||||
id, ok := parseUintParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var key model.APIKey
|
||||
if err := h.db.First(&key, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "api key not found"})
|
||||
return
|
||||
}
|
||||
var req dto.APIKeyDTO
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
update := map[string]any{}
|
||||
if req.GroupID != 0 {
|
||||
var group model.ProviderGroup
|
||||
if err := h.db.First(&group, req.GroupID).Error; err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "provider group not found"})
|
||||
return
|
||||
}
|
||||
update["group_id"] = req.GroupID
|
||||
}
|
||||
if strings.TrimSpace(req.APIKey) != "" {
|
||||
update["api_key"] = strings.TrimSpace(req.APIKey)
|
||||
}
|
||||
if req.Weight > 0 {
|
||||
update["weight"] = normalizeWeight(req.Weight)
|
||||
}
|
||||
if strings.TrimSpace(req.Status) != "" {
|
||||
update["status"] = strings.TrimSpace(req.Status)
|
||||
}
|
||||
if req.AutoBan != nil {
|
||||
update["auto_ban"] = *req.AutoBan
|
||||
}
|
||||
if req.BanReason != "" || strings.TrimSpace(req.Status) == "active" {
|
||||
update["ban_reason"] = strings.TrimSpace(req.BanReason)
|
||||
}
|
||||
if !req.BanUntil.IsZero() {
|
||||
tu := req.BanUntil.UTC()
|
||||
update["ban_until"] = &tu
|
||||
}
|
||||
if req.BanUntil.IsZero() && strings.TrimSpace(req.Status) == "active" {
|
||||
update["ban_until"] = nil
|
||||
}
|
||||
|
||||
if err := h.db.Model(&key).Updates(update).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update api key", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.db.First(&key, id).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to reload api key", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProviders(h.db); 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 {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, key)
|
||||
}
|
||||
|
||||
// DeleteAPIKey godoc
|
||||
// @Summary Delete API key
|
||||
// @Description Delete an API key
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "APIKey ID"
|
||||
// @Success 200 {object} gin.H
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/api-keys/{id} [delete]
|
||||
func (h *Handler) DeleteAPIKey(c *gin.Context) {
|
||||
id, ok := parseUintParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var key model.APIKey
|
||||
if err := h.db.First(&key, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "api key not found"})
|
||||
return
|
||||
}
|
||||
if err := h.db.Delete(&key).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete api key", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProviders(h.db); 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 {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
||||
}
|
||||
@@ -104,9 +104,9 @@ func (h *AdminHandler) BatchMasters(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// BatchProviders godoc
|
||||
// @Summary Batch providers
|
||||
// @Description Batch delete or status update for providers
|
||||
// BatchAPIKeys godoc
|
||||
// @Summary Batch api keys
|
||||
// @Description Batch delete or status update for api keys
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -115,8 +115,8 @@ func (h *AdminHandler) BatchMasters(c *gin.Context) {
|
||||
// @Success 200 {object} BatchResponse
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers/batch [post]
|
||||
func (h *Handler) BatchProviders(c *gin.Context) {
|
||||
// @Router /admin/api-keys/batch [post]
|
||||
func (h *Handler) BatchAPIKeys(c *gin.Context) {
|
||||
var req BatchActionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
|
||||
@@ -140,8 +140,8 @@ func (h *Handler) BatchProviders(c *gin.Context) {
|
||||
}
|
||||
needsBindingSync := false
|
||||
for _, id := range req.IDs {
|
||||
var p model.Provider
|
||||
if err := h.db.First(&p, id).Error; err != nil {
|
||||
var key model.APIKey
|
||||
if err := h.db.First(&key, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: "not found"})
|
||||
continue
|
||||
@@ -151,11 +151,7 @@ func (h *Handler) BatchProviders(c *gin.Context) {
|
||||
}
|
||||
switch action {
|
||||
case "delete":
|
||||
if err := h.db.Delete(&p).Error; err != nil {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
if err := h.sync.SyncProviderDelete(&p); err != nil {
|
||||
if err := h.db.Delete(&key).Error; err != nil {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
@@ -167,15 +163,11 @@ func (h *Handler) BatchProviders(c *gin.Context) {
|
||||
update["ban_reason"] = ""
|
||||
update["ban_until"] = nil
|
||||
}
|
||||
if err := h.db.Model(&p).Updates(update).Error; err != nil {
|
||||
if err := h.db.Model(&key).Updates(update).Error; err != nil {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
if err := h.db.First(&p, id).Error; err != nil {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
if err := h.sync.SyncProvider(&p); err != nil {
|
||||
if err := h.db.First(&key, id).Error; err != nil {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
@@ -184,6 +176,10 @@ func (h *Handler) BatchProviders(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
if needsBindingSync {
|
||||
if err := h.sync.SyncProviders(h.db); 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 {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/dto"
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
groupx "github.com/ez-api/foundation/group"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// CreateBinding godoc
|
||||
// @Summary Create a new binding
|
||||
// @Description Create a new (namespace, public_model) binding to a route group and selector
|
||||
// @Description Create a new (namespace, public_model) binding to a provider group and selector
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -32,14 +32,14 @@ func (h *Handler) CreateBinding(c *gin.Context) {
|
||||
|
||||
ns := strings.TrimSpace(req.Namespace)
|
||||
pm := strings.TrimSpace(req.PublicModel)
|
||||
if ns == "" || pm == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "namespace and public_model required"})
|
||||
if ns == "" || pm == "" || req.GroupID == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "namespace, public_model, and group_id required"})
|
||||
return
|
||||
}
|
||||
|
||||
rg := groupx.Normalize(req.RouteGroup)
|
||||
if strings.TrimSpace(rg) == "" {
|
||||
rg = "default"
|
||||
if err := h.ensureActiveGroup(req.GroupID); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
st := strings.TrimSpace(req.Status)
|
||||
@@ -55,7 +55,8 @@ func (h *Handler) CreateBinding(c *gin.Context) {
|
||||
b := model.Binding{
|
||||
Namespace: ns,
|
||||
PublicModel: pm,
|
||||
RouteGroup: rg,
|
||||
GroupID: req.GroupID,
|
||||
Weight: normalizeWeight(req.Weight),
|
||||
SelectorType: selectorType,
|
||||
SelectorValue: strings.TrimSpace(req.SelectorValue),
|
||||
Status: st,
|
||||
@@ -82,7 +83,7 @@ func (h *Handler) CreateBinding(c *gin.Context) {
|
||||
// @Security AdminAuth
|
||||
// @Param page query int false "page (1-based)"
|
||||
// @Param limit query int false "limit (default 50, max 200)"
|
||||
// @Param search query string false "search by namespace/public_model/route_group"
|
||||
// @Param search query string false "search by namespace/public_model"
|
||||
// @Success 200 {array} model.Binding
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/bindings [get]
|
||||
@@ -90,7 +91,7 @@ func (h *Handler) ListBindings(c *gin.Context) {
|
||||
var out []model.Binding
|
||||
q := h.db.Model(&model.Binding{}).Order("id desc")
|
||||
query := parseListQuery(c)
|
||||
q = applyListSearch(q, query.Search, "namespace", "public_model", "route_group")
|
||||
q = applyListSearch(q, query.Search, "namespace", "public_model")
|
||||
q = applyListPagination(q, query)
|
||||
if err := q.Find(&out).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list bindings", "details": err.Error()})
|
||||
@@ -139,8 +140,15 @@ func (h *Handler) UpdateBinding(c *gin.Context) {
|
||||
if pm := strings.TrimSpace(req.PublicModel); pm != "" {
|
||||
existing.PublicModel = pm
|
||||
}
|
||||
if rg := strings.TrimSpace(req.RouteGroup); rg != "" {
|
||||
existing.RouteGroup = groupx.Normalize(rg)
|
||||
if req.GroupID != 0 {
|
||||
if err := h.ensureActiveGroup(req.GroupID); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
existing.GroupID = req.GroupID
|
||||
}
|
||||
if req.Weight > 0 {
|
||||
existing.Weight = normalizeWeight(req.Weight)
|
||||
}
|
||||
if st := strings.TrimSpace(req.Status); st != "" {
|
||||
existing.Status = st
|
||||
@@ -229,3 +237,30 @@ func (h *Handler) DeleteBinding(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
||||
}
|
||||
|
||||
func normalizeWeight(weight int) int {
|
||||
if weight <= 0 {
|
||||
return 1
|
||||
}
|
||||
return weight
|
||||
}
|
||||
|
||||
func (h *Handler) ensureActiveGroup(groupID uint) error {
|
||||
var group model.ProviderGroup
|
||||
if err := h.db.First(&group, groupID).Error; err != nil {
|
||||
return fmt.Errorf("provider group not found")
|
||||
}
|
||||
if strings.TrimSpace(group.Status) != "" && strings.TrimSpace(group.Status) != "active" {
|
||||
return fmt.Errorf("provider group not active")
|
||||
}
|
||||
var count int64
|
||||
if err := h.db.Model(&model.APIKey{}).
|
||||
Where("group_id = ? AND status = ?", groupID, "active").
|
||||
Count(&count).Error; err != nil {
|
||||
return fmt.Errorf("failed to check api keys")
|
||||
}
|
||||
if count == 0 {
|
||||
return fmt.Errorf("provider group has no active api keys")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -9,8 +9,6 @@ import (
|
||||
"github.com/ez-api/ez-api/internal/dto"
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/ez-api/ez-api/internal/service"
|
||||
groupx "github.com/ez-api/foundation/group"
|
||||
"github.com/ez-api/foundation/provider"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
@@ -54,255 +52,6 @@ func (h *Handler) logBaseQuery() *gorm.DB {
|
||||
|
||||
// CreateKey is now handled by MasterHandler
|
||||
|
||||
// CreateProvider godoc
|
||||
// @Summary Create a new provider
|
||||
// @Description Register a new upstream AI provider
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param provider body dto.ProviderDTO true "Provider Info"
|
||||
// @Success 201 {object} model.Provider
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers [post]
|
||||
func (h *Handler) CreateProvider(c *gin.Context) {
|
||||
var req dto.ProviderDTO
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
providerType := provider.NormalizeType(req.Type)
|
||||
baseURL := strings.TrimSpace(req.BaseURL)
|
||||
googleLocation := provider.DefaultGoogleLocation(providerType, req.GoogleLocation)
|
||||
|
||||
group := strings.TrimSpace(req.Group)
|
||||
if group == "" {
|
||||
group = "default"
|
||||
}
|
||||
|
||||
status := strings.TrimSpace(req.Status)
|
||||
if status == "" {
|
||||
status = "active"
|
||||
}
|
||||
autoBan := true
|
||||
if req.AutoBan != nil {
|
||||
autoBan = *req.AutoBan
|
||||
}
|
||||
|
||||
// CP-side defaults + validation to prevent DP runtime errors.
|
||||
switch providerType {
|
||||
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:
|
||||
// Google SDK providers: base_url is not required.
|
||||
if provider.IsVertexFamily(providerType) && strings.TrimSpace(googleLocation) == "" {
|
||||
googleLocation = provider.DefaultGoogleLocation(providerType, "")
|
||||
}
|
||||
// For Gemini API providers, api_key is required.
|
||||
if provider.IsGoogleFamily(providerType) && !provider.IsVertexFamily(providerType) && strings.TrimSpace(req.APIKey) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "api_key required for gemini api providers"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
provider := model.Provider{
|
||||
Name: req.Name,
|
||||
Type: strings.TrimSpace(req.Type),
|
||||
BaseURL: baseURL,
|
||||
APIKey: req.APIKey,
|
||||
GoogleProject: strings.TrimSpace(req.GoogleProject),
|
||||
GoogleLocation: googleLocation,
|
||||
Group: group,
|
||||
Models: strings.Join(req.Models, ","),
|
||||
Status: status,
|
||||
AutoBan: autoBan,
|
||||
BanReason: req.BanReason,
|
||||
Weight: req.Weight,
|
||||
}
|
||||
if !req.BanUntil.IsZero() {
|
||||
tu := req.BanUntil.UTC()
|
||||
provider.BanUntil = &tu
|
||||
}
|
||||
|
||||
if err := h.db.Create(&provider).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProvider(&provider); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
// Provider model list changes can affect binding upstream mappings; rebuild bindings snapshot.
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, provider)
|
||||
}
|
||||
|
||||
// UpdateProvider godoc
|
||||
// @Summary Update a provider
|
||||
// @Description Update provider attributes including status/auto-ban flags
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "Provider ID"
|
||||
// @Param provider body dto.ProviderDTO true "Provider Info"
|
||||
// @Success 200 {object} model.Provider
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers/{id} [put]
|
||||
func (h *Handler) UpdateProvider(c *gin.Context) {
|
||||
idParam := c.Param("id")
|
||||
id, err := strconv.Atoi(idParam)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
|
||||
var existing model.Provider
|
||||
if err := h.db.First(&existing, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "provider not found"})
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.ProviderDTO
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
nextType := strings.TrimSpace(existing.Type)
|
||||
if t := strings.TrimSpace(req.Type); t != "" {
|
||||
nextType = t
|
||||
}
|
||||
nextTypeLower := provider.NormalizeType(nextType)
|
||||
nextBaseURL := strings.TrimSpace(existing.BaseURL)
|
||||
if strings.TrimSpace(req.BaseURL) != "" {
|
||||
nextBaseURL = strings.TrimSpace(req.BaseURL)
|
||||
}
|
||||
|
||||
update := map[string]any{}
|
||||
if strings.TrimSpace(req.Name) != "" {
|
||||
update["name"] = req.Name
|
||||
}
|
||||
if strings.TrimSpace(req.Type) != "" {
|
||||
update["type"] = strings.TrimSpace(req.Type)
|
||||
}
|
||||
if strings.TrimSpace(req.BaseURL) != "" {
|
||||
update["base_url"] = req.BaseURL
|
||||
}
|
||||
if req.APIKey != "" {
|
||||
update["api_key"] = req.APIKey
|
||||
}
|
||||
if strings.TrimSpace(req.GoogleProject) != "" {
|
||||
update["google_project"] = strings.TrimSpace(req.GoogleProject)
|
||||
}
|
||||
if strings.TrimSpace(req.GoogleLocation) != "" {
|
||||
update["google_location"] = strings.TrimSpace(req.GoogleLocation)
|
||||
} else if provider.IsVertexFamily(nextTypeLower) && strings.TrimSpace(existing.GoogleLocation) == "" {
|
||||
update["google_location"] = provider.DefaultGoogleLocation(nextTypeLower, "")
|
||||
}
|
||||
if req.Models != nil {
|
||||
update["models"] = strings.Join(req.Models, ",")
|
||||
}
|
||||
if req.Weight > 0 {
|
||||
update["weight"] = req.Weight
|
||||
}
|
||||
if strings.TrimSpace(req.Group) != "" {
|
||||
update["group"] = groupx.Normalize(req.Group)
|
||||
}
|
||||
if req.AutoBan != nil {
|
||||
update["auto_ban"] = *req.AutoBan
|
||||
}
|
||||
if strings.TrimSpace(req.Status) != "" {
|
||||
update["status"] = req.Status
|
||||
}
|
||||
if req.BanReason != "" || strings.TrimSpace(req.Status) == "active" {
|
||||
update["ban_reason"] = req.BanReason
|
||||
}
|
||||
if !req.BanUntil.IsZero() {
|
||||
tu := req.BanUntil.UTC()
|
||||
update["ban_until"] = &tu
|
||||
}
|
||||
if req.BanUntil.IsZero() && strings.TrimSpace(req.Status) == "active" {
|
||||
update["ban_until"] = nil
|
||||
}
|
||||
|
||||
// Defaults/validation after considering intended type/base_url.
|
||||
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
|
||||
}
|
||||
default:
|
||||
if provider.IsGoogleFamily(nextTypeLower) && !provider.IsVertexFamily(nextTypeLower) {
|
||||
// Ensure Gemini API providers have api_key.
|
||||
// If update does not include api_key, keep existing; otherwise require new one not empty.
|
||||
apiKey := existing.APIKey
|
||||
if req.APIKey != "" {
|
||||
apiKey = req.APIKey
|
||||
}
|
||||
if strings.TrimSpace(apiKey) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "api_key required for gemini api providers"})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(update) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "no fields to update"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.Model(&existing).Updates(update).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.First(&existing, id).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to reload provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProvider(&existing); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, existing)
|
||||
}
|
||||
|
||||
// CreateModel godoc
|
||||
// @Summary Register a new model
|
||||
// @Description Register a supported model with its capabilities
|
||||
|
||||
@@ -26,7 +26,7 @@ func newTestHandlerWithRedis(t *testing.T) (*Handler, *gorm.DB, *miniredis.Minir
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.Provider{}, &model.Binding{}, &model.Model{}); err != nil {
|
||||
if err := db.AutoMigrate(&model.ProviderGroup{}, &model.APIKey{}, &model.Binding{}, &model.Model{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
|
||||
@@ -204,10 +204,18 @@ func TestBatchModels_Delete(t *testing.T) {
|
||||
func TestBatchBindings_Status(t *testing.T) {
|
||||
h, db := newTestHandler(t)
|
||||
|
||||
group := model.ProviderGroup{Name: "default", Type: "openai", BaseURL: "https://api.openai.com/v1", Models: "m1", Status: "active"}
|
||||
if err := db.Create(&group).Error; err != nil {
|
||||
t.Fatalf("create group: %v", err)
|
||||
}
|
||||
if err := db.Create(&model.APIKey{GroupID: group.ID, APIKey: "k", Status: "active"}).Error; err != nil {
|
||||
t.Fatalf("create api key: %v", err)
|
||||
}
|
||||
b := &model.Binding{
|
||||
Namespace: "ns",
|
||||
PublicModel: "m1",
|
||||
RouteGroup: "default",
|
||||
GroupID: group.ID,
|
||||
Weight: 1,
|
||||
SelectorType: "exact",
|
||||
SelectorValue: "m1",
|
||||
Status: "active",
|
||||
|
||||
@@ -26,7 +26,7 @@ func newTestHandlerWithNamespace(t *testing.T) (*Handler, *gorm.DB, *miniredis.M
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.Provider{}, &model.Binding{}, &model.Namespace{}); err != nil {
|
||||
if err := db.AutoMigrate(&model.ProviderGroup{}, &model.APIKey{}, &model.Binding{}, &model.Namespace{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
|
||||
@@ -39,10 +39,23 @@ func newTestHandlerWithNamespace(t *testing.T) (*Handler, *gorm.DB, *miniredis.M
|
||||
func TestNamespaceCRUD_DeleteCleansBindings(t *testing.T) {
|
||||
h, db, _ := newTestHandlerWithNamespace(t)
|
||||
|
||||
group := model.ProviderGroup{Name: "g1", Type: "openai", BaseURL: "https://api.openai.com/v1", Models: "m1", Status: "active"}
|
||||
if err := db.Create(&group).Error; err != nil {
|
||||
t.Fatalf("create group: %v", err)
|
||||
}
|
||||
if err := db.Create(&model.APIKey{
|
||||
GroupID: group.ID,
|
||||
APIKey: "k1",
|
||||
Status: "active",
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("create api key: %v", err)
|
||||
}
|
||||
|
||||
if err := db.Create(&model.Binding{
|
||||
Namespace: "ns1",
|
||||
PublicModel: "m1",
|
||||
RouteGroup: "default",
|
||||
GroupID: group.ID,
|
||||
Weight: 1,
|
||||
SelectorType: "exact",
|
||||
SelectorValue: "m1",
|
||||
Status: "active",
|
||||
|
||||
@@ -1,350 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/ez-api/foundation/provider"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ListProviders godoc
|
||||
// @Summary List providers
|
||||
// @Description List all configured upstream providers
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param page query int false "page (1-based)"
|
||||
// @Param limit query int false "limit (default 50, max 200)"
|
||||
// @Param search query string false "search by name/type/base_url/group"
|
||||
// @Success 200 {array} model.Provider
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers [get]
|
||||
func (h *Handler) ListProviders(c *gin.Context) {
|
||||
var providers []model.Provider
|
||||
q := h.db.Model(&model.Provider{}).Order("id desc")
|
||||
query := parseListQuery(c)
|
||||
q = applyListSearch(q, query.Search, "name", `"type"`, "base_url", `"group"`)
|
||||
q = applyListPagination(q, query)
|
||||
if err := q.Find(&providers).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list providers", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, providers)
|
||||
}
|
||||
|
||||
// GetProvider godoc
|
||||
// @Summary Get provider
|
||||
// @Description Get a provider by id
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "Provider ID"
|
||||
// @Success 200 {object} model.Provider
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers/{id} [get]
|
||||
func (h *Handler) GetProvider(c *gin.Context) {
|
||||
id, ok := parseUintParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var p model.Provider
|
||||
if err := h.db.First(&p, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "provider not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, p)
|
||||
}
|
||||
|
||||
// DeleteProvider godoc
|
||||
// @Summary Delete provider
|
||||
// @Description Deletes a provider and triggers a full snapshot sync to avoid stale routing
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "Provider ID"
|
||||
// @Success 200 {object} gin.H
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers/{id} [delete]
|
||||
func (h *Handler) DeleteProvider(c *gin.Context) {
|
||||
id, ok := parseUintParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var p model.Provider
|
||||
if err := h.db.First(&p, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "provider not found"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.Delete(&p).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProviderDelete(&p); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync provider delete", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
||||
}
|
||||
|
||||
type testProviderResponse struct {
|
||||
StatusCode int `json:"status_code"`
|
||||
OK bool `json:"ok"`
|
||||
URL string `json:"url"`
|
||||
Body string `json:"body,omitempty"`
|
||||
}
|
||||
|
||||
// TestProvider godoc
|
||||
// @Summary Test provider connectivity
|
||||
// @Description Performs a lightweight upstream request to verify the provider configuration
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "Provider ID"
|
||||
// @Success 200 {object} testProviderResponse
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers/{id}/test [post]
|
||||
func (h *Handler) TestProvider(c *gin.Context) {
|
||||
id, ok := parseUintParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var p model.Provider
|
||||
if err := h.db.First(&p, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "provider not found"})
|
||||
return
|
||||
}
|
||||
|
||||
req, err := buildProviderModelsRequest(&p)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, testProviderResponse{StatusCode: 0, OK: false, URL: req.URL.String(), Body: err.Error()})
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 2048))
|
||||
ok = resp.StatusCode >= 200 && resp.StatusCode < 300
|
||||
|
||||
c.JSON(http.StatusOK, testProviderResponse{
|
||||
StatusCode: resp.StatusCode,
|
||||
OK: ok,
|
||||
URL: req.URL.String(),
|
||||
Body: string(body),
|
||||
})
|
||||
}
|
||||
|
||||
// FetchProviderModels godoc
|
||||
// @Summary Fetch models from provider
|
||||
// @Description Calls upstream /models (or /v1/models) and updates provider model list
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "Provider ID"
|
||||
// @Success 200 {object} gin.H
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 502 {object} gin.H
|
||||
// @Router /admin/providers/{id}/fetch-models [post]
|
||||
func (h *Handler) FetchProviderModels(c *gin.Context) {
|
||||
id, ok := parseUintParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var p model.Provider
|
||||
if err := h.db.First(&p, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "provider not found"})
|
||||
return
|
||||
}
|
||||
|
||||
req, err := buildProviderModelsRequest(&p)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 15 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "failed to fetch models", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
c.JSON(http.StatusBadGateway, gin.H{
|
||||
"error": "upstream returned non-2xx",
|
||||
"status_code": resp.StatusCode,
|
||||
"body": string(body),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
models, err := parseProviderModelIDs(body)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "failed to parse models", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.Model(&p).Update("models", strings.Join(models, ",")).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update provider models", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.db.First(&p, id).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to reload provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncProvider(&p); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to sync provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "updated",
|
||||
"count": len(models),
|
||||
"models": models,
|
||||
})
|
||||
}
|
||||
|
||||
func buildProviderModelsRequest(p *model.Provider) (*http.Request, error) {
|
||||
if p == nil {
|
||||
return nil, fmt.Errorf("provider required")
|
||||
}
|
||||
pt := provider.NormalizeType(p.Type)
|
||||
baseURL := strings.TrimRight(strings.TrimSpace(p.BaseURL), "/")
|
||||
if baseURL == "" {
|
||||
return nil, fmt.Errorf("base_url required for provider models fetch")
|
||||
}
|
||||
|
||||
url := ""
|
||||
switch pt {
|
||||
case provider.TypeOpenAI, provider.TypeCompatible:
|
||||
if strings.HasSuffix(baseURL, "/v1") {
|
||||
url = baseURL + "/models"
|
||||
} else {
|
||||
url = baseURL + "/v1/models"
|
||||
}
|
||||
case provider.TypeAnthropic, provider.TypeClaude:
|
||||
url = baseURL + "/v1/models"
|
||||
default:
|
||||
return nil, fmt.Errorf("provider type not supported for model fetch")
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build request: %w", err)
|
||||
}
|
||||
|
||||
apiKey := strings.TrimSpace(p.APIKey)
|
||||
switch pt {
|
||||
case provider.TypeOpenAI, provider.TypeCompatible:
|
||||
if apiKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
}
|
||||
case provider.TypeAnthropic, provider.TypeClaude:
|
||||
if apiKey != "" {
|
||||
req.Header.Set("x-api-key", apiKey)
|
||||
}
|
||||
req.Header.Set("anthropic-version", "2023-06-01")
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
type providerModelsResponse struct {
|
||||
Data []json.RawMessage `json:"data"`
|
||||
Models []string `json:"models"`
|
||||
}
|
||||
|
||||
func parseProviderModelIDs(payload []byte) ([]string, error) {
|
||||
var resp providerModelsResponse
|
||||
if err := json.Unmarshal(payload, &resp); err != nil {
|
||||
return nil, fmt.Errorf("decode response: %w", err)
|
||||
}
|
||||
|
||||
models := make([]string, 0, len(resp.Data)+len(resp.Models))
|
||||
for _, name := range resp.Models {
|
||||
name = strings.TrimSpace(name)
|
||||
if name != "" {
|
||||
models = append(models, name)
|
||||
}
|
||||
}
|
||||
for _, raw := range resp.Data {
|
||||
var item struct {
|
||||
ID string `json:"id"`
|
||||
Model string `json:"model"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &item); err == nil {
|
||||
if item.ID != "" {
|
||||
models = append(models, strings.TrimSpace(item.ID))
|
||||
continue
|
||||
}
|
||||
if item.Model != "" {
|
||||
models = append(models, strings.TrimSpace(item.Model))
|
||||
continue
|
||||
}
|
||||
if item.Name != "" {
|
||||
models = append(models, strings.TrimSpace(item.Name))
|
||||
continue
|
||||
}
|
||||
}
|
||||
var name string
|
||||
if err := json.Unmarshal(raw, &name); err == nil {
|
||||
name = strings.TrimSpace(name)
|
||||
if name != "" {
|
||||
models = append(models, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unique := make(map[string]struct{}, len(models))
|
||||
out := make([]string, 0, len(models))
|
||||
for _, name := range models {
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := unique[name]; ok {
|
||||
continue
|
||||
}
|
||||
unique[name] = struct{}{}
|
||||
out = append(out, name)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil, fmt.Errorf("no models found in response")
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, nil
|
||||
}
|
||||
@@ -1,162 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestAdmin_TestProvider_OpenAICompatible(t *testing.T) {
|
||||
h, db := newTestHandler(t)
|
||||
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/models" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if got := r.Header.Get("Authorization"); got != "Bearer k" {
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"object":"list","data":[]}`))
|
||||
}))
|
||||
defer upstream.Close()
|
||||
|
||||
p := &model.Provider{
|
||||
Name: "p1",
|
||||
Type: "openai",
|
||||
BaseURL: upstream.URL + "/v1",
|
||||
APIKey: "k",
|
||||
Group: "default",
|
||||
Models: "gpt-4o-mini",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(p).Error; err != nil {
|
||||
t.Fatalf("create provider: %v", err)
|
||||
}
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers/:id/test", h.TestProvider)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers/1/test", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if ok, _ := payload["ok"].(bool); !ok {
|
||||
t.Fatalf("expected ok=true, got %v body=%s", payload["ok"], rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdmin_FetchProviderModels_OpenAICompatible(t *testing.T) {
|
||||
h, db := newTestHandler(t)
|
||||
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/models" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if got := r.Header.Get("Authorization"); got != "Bearer k" {
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"object":"list","data":[{"id":"gpt-4o-mini"},{"id":"gpt-4o"}]}`))
|
||||
}))
|
||||
defer upstream.Close()
|
||||
|
||||
p := &model.Provider{
|
||||
Name: "p1",
|
||||
Type: "openai",
|
||||
BaseURL: upstream.URL + "/v1",
|
||||
APIKey: "k",
|
||||
Group: "default",
|
||||
Models: "old-model",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(p).Error; err != nil {
|
||||
t.Fatalf("create provider: %v", err)
|
||||
}
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers/:id/fetch-models", h.FetchProviderModels)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers/1/fetch-models", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
var updated model.Provider
|
||||
if err := db.First(&updated, p.ID).Error; err != nil {
|
||||
t.Fatalf("reload provider: %v", err)
|
||||
}
|
||||
if updated.Models != "gpt-4o,gpt-4o-mini" {
|
||||
t.Fatalf("expected models to update, got %q", updated.Models)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdmin_BatchProviders_Status(t *testing.T) {
|
||||
h, db := newTestHandler(t)
|
||||
|
||||
banUntil := time.Now().Add(2 * time.Hour).UTC()
|
||||
p := &model.Provider{
|
||||
Name: "p1",
|
||||
Type: "openai",
|
||||
BaseURL: "https://api.openai.com/v1",
|
||||
Group: "default",
|
||||
Models: "gpt-4o-mini",
|
||||
Status: "manual_disabled",
|
||||
BanReason: "bad",
|
||||
BanUntil: &banUntil,
|
||||
}
|
||||
if err := db.Create(p).Error; err != nil {
|
||||
t.Fatalf("create provider: %v", err)
|
||||
}
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers/batch", h.BatchProviders)
|
||||
|
||||
payload := map[string]any{
|
||||
"action": "status",
|
||||
"status": "active",
|
||||
"ids": []uint{p.ID},
|
||||
}
|
||||
b, _ := json.Marshal(payload)
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers/batch", bytes.NewReader(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
var updated model.Provider
|
||||
if err := db.First(&updated, p.ID).Error; err != nil {
|
||||
t.Fatalf("reload provider: %v", err)
|
||||
}
|
||||
if updated.Status != "active" {
|
||||
t.Fatalf("expected status active, got %q", updated.Status)
|
||||
}
|
||||
if updated.BanReason != "" {
|
||||
t.Fatalf("expected ban_reason cleared, got %q", updated.BanReason)
|
||||
}
|
||||
if updated.BanUntil != nil {
|
||||
t.Fatalf("expected ban_until cleared, got %v", updated.BanUntil)
|
||||
}
|
||||
}
|
||||
@@ -1,288 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/dto"
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
groupx "github.com/ez-api/foundation/group"
|
||||
providerx "github.com/ez-api/foundation/provider"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// CreateProviderPreset godoc
|
||||
// @Summary Create a preset provider
|
||||
// @Description Create an official OpenAI/Anthropic provider (only api_key is typically required)
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param provider body dto.ProviderPresetCreateDTO true "Provider preset payload"
|
||||
// @Success 201 {object} model.Provider
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers/preset [post]
|
||||
func (h *Handler) CreateProviderPreset(c *gin.Context) {
|
||||
var req dto.ProviderPresetCreateDTO
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
preset := providerx.NormalizeType(req.Preset)
|
||||
if preset == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "preset required"})
|
||||
return
|
||||
}
|
||||
|
||||
var providerType string
|
||||
var baseURL string
|
||||
switch preset {
|
||||
case providerx.TypeOpenAI:
|
||||
providerType = providerx.TypeOpenAI
|
||||
baseURL = "https://api.openai.com/v1"
|
||||
case providerx.TypeAnthropic, providerx.TypeClaude:
|
||||
providerType = providerx.TypeAnthropic
|
||||
baseURL = "https://api.anthropic.com"
|
||||
default:
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported preset: " + preset + " (use /admin/providers/google for Google SDK providers)"})
|
||||
return
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(req.Name)
|
||||
if name == "" {
|
||||
name = providerType + "-" + randomSuffix(4)
|
||||
}
|
||||
group := strings.TrimSpace(req.Group)
|
||||
if group == "" {
|
||||
group = "default"
|
||||
}
|
||||
status := strings.TrimSpace(req.Status)
|
||||
if status == "" {
|
||||
status = "active"
|
||||
}
|
||||
autoBan := true
|
||||
if req.AutoBan != nil {
|
||||
autoBan = *req.AutoBan
|
||||
}
|
||||
|
||||
googleLocation := providerx.DefaultGoogleLocation(providerType, req.GoogleLocation)
|
||||
|
||||
p := model.Provider{
|
||||
Name: name,
|
||||
Type: providerType,
|
||||
BaseURL: baseURL,
|
||||
APIKey: strings.TrimSpace(req.APIKey),
|
||||
GoogleProject: strings.TrimSpace(req.GoogleProject),
|
||||
GoogleLocation: googleLocation,
|
||||
Group: groupx.Normalize(group),
|
||||
Models: strings.Join(req.Models, ","),
|
||||
Status: status,
|
||||
AutoBan: autoBan,
|
||||
}
|
||||
if req.Weight > 0 {
|
||||
p.Weight = req.Weight
|
||||
}
|
||||
|
||||
if err := h.db.Create(&p).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProvider(&p); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, p)
|
||||
}
|
||||
|
||||
// CreateProviderGoogle godoc
|
||||
// @Summary Create a Google SDK provider
|
||||
// @Description Create a Google SDK provider (Gemini API key or Vertex project/location); base_url is not used
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param provider body dto.ProviderGoogleCreateDTO true "Google provider payload"
|
||||
// @Success 201 {object} model.Provider
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers/google [post]
|
||||
func (h *Handler) CreateProviderGoogle(c *gin.Context) {
|
||||
var req dto.ProviderGoogleCreateDTO
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
pt := providerx.NormalizeType(req.Type)
|
||||
if pt == "" {
|
||||
pt = providerx.TypeGemini
|
||||
}
|
||||
if !providerx.IsGoogleFamily(pt) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "type must be google family"})
|
||||
return
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(req.Name)
|
||||
if name == "" {
|
||||
name = pt + "-" + randomSuffix(4)
|
||||
}
|
||||
group := strings.TrimSpace(req.Group)
|
||||
if group == "" {
|
||||
group = "default"
|
||||
}
|
||||
status := strings.TrimSpace(req.Status)
|
||||
if status == "" {
|
||||
status = "active"
|
||||
}
|
||||
autoBan := true
|
||||
if req.AutoBan != nil {
|
||||
autoBan = *req.AutoBan
|
||||
}
|
||||
|
||||
// Validate fields by type.
|
||||
apiKey := strings.TrimSpace(req.APIKey)
|
||||
googleProject := strings.TrimSpace(req.GoogleProject)
|
||||
googleLocation := providerx.DefaultGoogleLocation(pt, req.GoogleLocation)
|
||||
|
||||
if providerx.IsVertexFamily(pt) {
|
||||
// Vertex uses ADC and project/location; api_key is not required.
|
||||
if strings.TrimSpace(googleLocation) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "google_location required"})
|
||||
return
|
||||
}
|
||||
apiKey = ""
|
||||
} else {
|
||||
// Gemini API requires api_key.
|
||||
if apiKey == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "api_key required for gemini api"})
|
||||
return
|
||||
}
|
||||
googleProject = ""
|
||||
googleLocation = ""
|
||||
}
|
||||
|
||||
p := model.Provider{
|
||||
Name: name,
|
||||
Type: pt,
|
||||
BaseURL: "", // intentionally unused for Google SDK
|
||||
APIKey: apiKey,
|
||||
GoogleProject: googleProject,
|
||||
GoogleLocation: googleLocation,
|
||||
Group: groupx.Normalize(group),
|
||||
Models: strings.Join(req.Models, ","),
|
||||
Status: status,
|
||||
AutoBan: autoBan,
|
||||
}
|
||||
if req.Weight > 0 {
|
||||
p.Weight = req.Weight
|
||||
}
|
||||
|
||||
if err := h.db.Create(&p).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncProvider(&p); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, p)
|
||||
}
|
||||
|
||||
// CreateProviderCustom godoc
|
||||
// @Summary Create a custom provider
|
||||
// @Description Create an OpenAI-compatible provider (base_url + api_key required)
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param provider body dto.ProviderCustomCreateDTO true "Provider custom payload"
|
||||
// @Success 201 {object} model.Provider
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers/custom [post]
|
||||
func (h *Handler) CreateProviderCustom(c *gin.Context) {
|
||||
var req dto.ProviderCustomCreateDTO
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(req.Name)
|
||||
if name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "name required"})
|
||||
return
|
||||
}
|
||||
baseURL := strings.TrimSpace(req.BaseURL)
|
||||
if baseURL == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "base_url required"})
|
||||
return
|
||||
}
|
||||
|
||||
group := strings.TrimSpace(req.Group)
|
||||
if group == "" {
|
||||
group = "default"
|
||||
}
|
||||
status := strings.TrimSpace(req.Status)
|
||||
if status == "" {
|
||||
status = "active"
|
||||
}
|
||||
autoBan := true
|
||||
if req.AutoBan != nil {
|
||||
autoBan = *req.AutoBan
|
||||
}
|
||||
|
||||
p := model.Provider{
|
||||
Name: name,
|
||||
Type: providerx.TypeCompatible,
|
||||
BaseURL: baseURL,
|
||||
APIKey: strings.TrimSpace(req.APIKey),
|
||||
Group: groupx.Normalize(group),
|
||||
Models: strings.Join(req.Models, ","),
|
||||
Status: status,
|
||||
AutoBan: autoBan,
|
||||
}
|
||||
if req.Weight > 0 {
|
||||
p.Weight = req.Weight
|
||||
}
|
||||
|
||||
if err := h.db.Create(&p).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProvider(&p); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync provider", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, p)
|
||||
}
|
||||
|
||||
func randomSuffix(bytesLen int) string {
|
||||
if bytesLen <= 0 {
|
||||
bytesLen = 4
|
||||
}
|
||||
b := make([]byte, bytesLen)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "rand"
|
||||
}
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestCreateProviderPreset_OpenAI_SetsBaseURL(t *testing.T) {
|
||||
h, _ := newTestHandler(t)
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers/preset", h.CreateProviderPreset)
|
||||
|
||||
reqBody := map[string]any{
|
||||
"preset": "openai",
|
||||
"api_key": "k",
|
||||
"models": []string{"gpt-4o-mini"},
|
||||
}
|
||||
b, _ := json.Marshal(reqBody)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers/preset", bytes.NewReader(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusCreated {
|
||||
t.Fatalf("expected 201, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
var got model.Provider
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if got.Type != "openai" {
|
||||
t.Fatalf("expected type openai, got %q", got.Type)
|
||||
}
|
||||
if got.BaseURL != "https://api.openai.com/v1" {
|
||||
t.Fatalf("expected base_url=https://api.openai.com/v1, got %q", got.BaseURL)
|
||||
}
|
||||
if got.Name == "" {
|
||||
t.Fatalf("expected generated name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateProviderCustom_RequiresBaseURL(t *testing.T) {
|
||||
h, _ := newTestHandler(t)
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers/custom", h.CreateProviderCustom)
|
||||
|
||||
reqBody := map[string]any{
|
||||
"name": "c1",
|
||||
"api_key": "k",
|
||||
}
|
||||
b, _ := json.Marshal(reqBody)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers/custom", bytes.NewReader(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateProviderGoogle_GeminiRequiresAPIKey(t *testing.T) {
|
||||
h, _ := newTestHandler(t)
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers/google", h.CreateProviderGoogle)
|
||||
|
||||
reqBody := map[string]any{
|
||||
"type": "gemini",
|
||||
"models": []string{"gemini-2.0-flash"},
|
||||
}
|
||||
b, _ := json.Marshal(reqBody)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers/google", bytes.NewReader(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
}
|
||||
302
internal/api/provider_group_handler.go
Normal file
302
internal/api/provider_group_handler.go
Normal file
@@ -0,0 +1,302 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// CreateProviderGroup godoc
|
||||
// @Summary Create a provider group
|
||||
// @Description Create a provider group definition
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param group body dto.ProviderGroupDTO true "Provider group payload"
|
||||
// @Success 201 {object} model.ProviderGroup
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/provider-groups [post]
|
||||
func (h *Handler) CreateProviderGroup(c *gin.Context) {
|
||||
var req dto.ProviderGroupDTO
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(req.Name)
|
||||
if name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "name required"})
|
||||
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,
|
||||
GoogleProject: strings.TrimSpace(req.GoogleProject),
|
||||
GoogleLocation: googleLocation,
|
||||
Models: strings.Join(req.Models, ","),
|
||||
Status: status,
|
||||
}
|
||||
if err := h.db.Create(&group).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 {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync providers", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, group)
|
||||
}
|
||||
|
||||
// ListProviderGroups godoc
|
||||
// @Summary List provider groups
|
||||
// @Description List all provider groups
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param page query int false "page (1-based)"
|
||||
// @Param limit query int false "limit (default 50, max 200)"
|
||||
// @Param search query string false "search by name/type"
|
||||
// @Success 200 {array} model.ProviderGroup
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/provider-groups [get]
|
||||
func (h *Handler) ListProviderGroups(c *gin.Context) {
|
||||
var groups []model.ProviderGroup
|
||||
q := h.db.Model(&model.ProviderGroup{}).Order("id desc")
|
||||
query := parseListQuery(c)
|
||||
q = applyListSearch(q, query.Search, "name", "type")
|
||||
q = applyListPagination(q, query)
|
||||
if err := q.Find(&groups).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list provider groups", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, groups)
|
||||
}
|
||||
|
||||
// GetProviderGroup godoc
|
||||
// @Summary Get provider group
|
||||
// @Description Get a provider group by id
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "ProviderGroup ID"
|
||||
// @Success 200 {object} model.ProviderGroup
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/provider-groups/{id} [get]
|
||||
func (h *Handler) GetProviderGroup(c *gin.Context) {
|
||||
id, ok := parseUintParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var group model.ProviderGroup
|
||||
if err := h.db.First(&group, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "provider group not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, group)
|
||||
}
|
||||
|
||||
// UpdateProviderGroup godoc
|
||||
// @Summary Update provider group
|
||||
// @Description Update a provider group
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "ProviderGroup ID"
|
||||
// @Param group body dto.ProviderGroupDTO true "Provider group payload"
|
||||
// @Success 200 {object} model.ProviderGroup
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/provider-groups/{id} [put]
|
||||
func (h *Handler) UpdateProviderGroup(c *gin.Context) {
|
||||
idParam := c.Param("id")
|
||||
id, err := strconv.Atoi(idParam)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
|
||||
var group model.ProviderGroup
|
||||
if err := h.db.First(&group, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "provider group not found"})
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.ProviderGroupDTO
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
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{}
|
||||
if strings.TrimSpace(req.Name) != "" {
|
||||
update["name"] = strings.TrimSpace(req.Name)
|
||||
}
|
||||
if strings.TrimSpace(req.Type) != "" {
|
||||
update["type"] = strings.TrimSpace(req.Type)
|
||||
}
|
||||
if strings.TrimSpace(req.BaseURL) != "" {
|
||||
update["base_url"] = strings.TrimSpace(req.BaseURL)
|
||||
}
|
||||
if strings.TrimSpace(req.GoogleProject) != "" {
|
||||
update["google_project"] = 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, "")
|
||||
}
|
||||
if req.Models != nil {
|
||||
update["models"] = strings.Join(req.Models, ",")
|
||||
}
|
||||
if strings.TrimSpace(req.Status) != "" {
|
||||
update["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
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.db.Model(&group).Updates(update).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 {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync providers", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, group)
|
||||
}
|
||||
|
||||
// DeleteProviderGroup godoc
|
||||
// @Summary Delete provider group
|
||||
// @Description Delete a provider group and its api keys/bindings
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param id path int true "ProviderGroup ID"
|
||||
// @Success 200 {object} gin.H
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 404 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/provider-groups/{id} [delete]
|
||||
func (h *Handler) DeleteProviderGroup(c *gin.Context) {
|
||||
id, ok := parseUintParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var group model.ProviderGroup
|
||||
if err := h.db.First(&group, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "provider group not found"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("group_id = ?", group.ID).Delete(&model.APIKey{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("group_id = ?", group.ID).Delete(&model.Binding{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Delete(&group).Error
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete provider group", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sync.SyncProviders(h.db); 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 {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/ez-api/ez-api/internal/dto"
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/ez-api/ez-api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func newTestHandler(t *testing.T) (*Handler, *gorm.DB) {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
// Use a unique in-memory DB per test to avoid cross-test interference.
|
||||
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.Master{}, &model.Key{}, &model.Provider{}, &model.Model{}, &model.Binding{}, &model.LogRecord{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
|
||||
mr := miniredis.RunT(t)
|
||||
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
sync := service.NewSyncService(rdb)
|
||||
|
||||
return NewHandler(db, db, sync, nil, rdb, nil), db
|
||||
}
|
||||
|
||||
func TestCreateProvider_DefaultsVertexLocationGlobal(t *testing.T) {
|
||||
h, _ := newTestHandler(t)
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers", h.CreateProvider)
|
||||
|
||||
reqBody := dto.ProviderDTO{
|
||||
Name: "g1",
|
||||
Type: "vertex-express",
|
||||
Group: "default",
|
||||
Models: []string{"gemini-3-pro-preview"},
|
||||
}
|
||||
b, _ := json.Marshal(reqBody)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers", bytes.NewReader(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusCreated {
|
||||
t.Fatalf("expected 201, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
var got model.Provider
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if got.GoogleLocation != "global" {
|
||||
t.Fatalf("expected google_location=global, got %q", got.GoogleLocation)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProvider_DefaultsVertexLocationGlobalWhenMissing(t *testing.T) {
|
||||
h, db := newTestHandler(t)
|
||||
|
||||
existing := &model.Provider{
|
||||
Name: "g2",
|
||||
Type: "vertex",
|
||||
Group: "default",
|
||||
Models: "gemini-3-pro-preview",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(existing).Error; err != nil {
|
||||
t.Fatalf("create provider: %v", err)
|
||||
}
|
||||
|
||||
r := gin.New()
|
||||
r.PUT("/admin/providers/:id", h.UpdateProvider)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPut, fmt.Sprintf("/admin/providers/%d", existing.ID), bytes.NewReader([]byte(`{}`)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
var got model.Provider
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if got.GoogleLocation != "global" {
|
||||
t.Fatalf("expected google_location=global, got %q", got.GoogleLocation)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user