mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
refactor(config): rename SyncGeminiKeys; use Sanitize* methods
This commit is contained in:
@@ -148,7 +148,7 @@ func (h *Handler) applyLegacyKeys(keys []string) {
|
|||||||
}
|
}
|
||||||
h.cfg.GeminiKey = newList
|
h.cfg.GeminiKey = newList
|
||||||
h.cfg.GlAPIKey = sanitized
|
h.cfg.GlAPIKey = sanitized
|
||||||
h.cfg.SyncGeminiKeys()
|
h.cfg.SanitizeGeminiKeys()
|
||||||
}
|
}
|
||||||
|
|
||||||
// api-keys
|
// api-keys
|
||||||
@@ -206,7 +206,7 @@ func (h *Handler) PutGeminiKeys(c *gin.Context) {
|
|||||||
arr = obj.Items
|
arr = obj.Items
|
||||||
}
|
}
|
||||||
h.cfg.GeminiKey = append([]config.GeminiKey(nil), arr...)
|
h.cfg.GeminiKey = append([]config.GeminiKey(nil), arr...)
|
||||||
h.cfg.SyncGeminiKeys()
|
h.cfg.SanitizeGeminiKeys()
|
||||||
h.persist(c)
|
h.persist(c)
|
||||||
}
|
}
|
||||||
func (h *Handler) PatchGeminiKey(c *gin.Context) {
|
func (h *Handler) PatchGeminiKey(c *gin.Context) {
|
||||||
@@ -227,7 +227,7 @@ func (h *Handler) PatchGeminiKey(c *gin.Context) {
|
|||||||
// Treat empty API key as delete.
|
// Treat empty API key as delete.
|
||||||
if body.Index != nil && *body.Index >= 0 && *body.Index < len(h.cfg.GeminiKey) {
|
if body.Index != nil && *body.Index >= 0 && *body.Index < len(h.cfg.GeminiKey) {
|
||||||
h.cfg.GeminiKey = append(h.cfg.GeminiKey[:*body.Index], h.cfg.GeminiKey[*body.Index+1:]...)
|
h.cfg.GeminiKey = append(h.cfg.GeminiKey[:*body.Index], h.cfg.GeminiKey[*body.Index+1:]...)
|
||||||
h.cfg.SyncGeminiKeys()
|
h.cfg.SanitizeGeminiKeys()
|
||||||
h.persist(c)
|
h.persist(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -245,7 +245,7 @@ func (h *Handler) PatchGeminiKey(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
if removed {
|
if removed {
|
||||||
h.cfg.GeminiKey = out
|
h.cfg.GeminiKey = out
|
||||||
h.cfg.SyncGeminiKeys()
|
h.cfg.SanitizeGeminiKeys()
|
||||||
h.persist(c)
|
h.persist(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -257,7 +257,7 @@ func (h *Handler) PatchGeminiKey(c *gin.Context) {
|
|||||||
|
|
||||||
if body.Index != nil && *body.Index >= 0 && *body.Index < len(h.cfg.GeminiKey) {
|
if body.Index != nil && *body.Index >= 0 && *body.Index < len(h.cfg.GeminiKey) {
|
||||||
h.cfg.GeminiKey[*body.Index] = value
|
h.cfg.GeminiKey[*body.Index] = value
|
||||||
h.cfg.SyncGeminiKeys()
|
h.cfg.SanitizeGeminiKeys()
|
||||||
h.persist(c)
|
h.persist(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -266,7 +266,7 @@ func (h *Handler) PatchGeminiKey(c *gin.Context) {
|
|||||||
for i := range h.cfg.GeminiKey {
|
for i := range h.cfg.GeminiKey {
|
||||||
if h.cfg.GeminiKey[i].APIKey == match {
|
if h.cfg.GeminiKey[i].APIKey == match {
|
||||||
h.cfg.GeminiKey[i] = value
|
h.cfg.GeminiKey[i] = value
|
||||||
h.cfg.SyncGeminiKeys()
|
h.cfg.SanitizeGeminiKeys()
|
||||||
h.persist(c)
|
h.persist(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -284,7 +284,7 @@ func (h *Handler) DeleteGeminiKey(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
if len(out) != len(h.cfg.GeminiKey) {
|
if len(out) != len(h.cfg.GeminiKey) {
|
||||||
h.cfg.GeminiKey = out
|
h.cfg.GeminiKey = out
|
||||||
h.cfg.SyncGeminiKeys()
|
h.cfg.SanitizeGeminiKeys()
|
||||||
h.persist(c)
|
h.persist(c)
|
||||||
} else {
|
} else {
|
||||||
c.JSON(404, gin.H{"error": "item not found"})
|
c.JSON(404, gin.H{"error": "item not found"})
|
||||||
@@ -295,7 +295,7 @@ func (h *Handler) DeleteGeminiKey(c *gin.Context) {
|
|||||||
var idx int
|
var idx int
|
||||||
if _, err := fmt.Sscanf(idxStr, "%d", &idx); err == nil && idx >= 0 && idx < len(h.cfg.GeminiKey) {
|
if _, err := fmt.Sscanf(idxStr, "%d", &idx); err == nil && idx >= 0 && idx < len(h.cfg.GeminiKey) {
|
||||||
h.cfg.GeminiKey = append(h.cfg.GeminiKey[:idx], h.cfg.GeminiKey[idx+1:]...)
|
h.cfg.GeminiKey = append(h.cfg.GeminiKey[:idx], h.cfg.GeminiKey[idx+1:]...)
|
||||||
h.cfg.SyncGeminiKeys()
|
h.cfg.SanitizeGeminiKeys()
|
||||||
h.persist(c)
|
h.persist(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -255,26 +255,26 @@ func LoadConfigOptional(configFile string, optional bool) (*Config, error) {
|
|||||||
// Sync request authentication providers with inline API keys for backwards compatibility.
|
// Sync request authentication providers with inline API keys for backwards compatibility.
|
||||||
syncInlineAccessProvider(&cfg)
|
syncInlineAccessProvider(&cfg)
|
||||||
|
|
||||||
// Normalize Gemini API key configuration and migrate legacy entries.
|
// Sanitize Gemini API key configuration and migrate legacy entries.
|
||||||
cfg.SyncGeminiKeys()
|
cfg.SanitizeGeminiKeys()
|
||||||
|
|
||||||
// Sanitize OpenAI compatibility providers: drop entries without base-url
|
|
||||||
sanitizeOpenAICompatibility(&cfg)
|
|
||||||
|
|
||||||
// Sanitize Codex keys: drop entries without base-url
|
// Sanitize Codex keys: drop entries without base-url
|
||||||
sanitizeCodexKeys(&cfg)
|
cfg.SanitizeCodexKeys()
|
||||||
|
|
||||||
// Normalize Claude key headers
|
// Sanitize Claude key headers
|
||||||
normalizeClaudeKeys(&cfg)
|
cfg.SanitizeClaudeKeys()
|
||||||
|
|
||||||
|
// Sanitize OpenAI compatibility providers: drop entries without base-url
|
||||||
|
cfg.SanitizeOpenAICompatibility()
|
||||||
|
|
||||||
// Return the populated configuration struct.
|
// Return the populated configuration struct.
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanitizeOpenAICompatibility removes OpenAI-compatibility provider entries that are
|
// SanitizeOpenAICompatibility removes OpenAI-compatibility provider entries that are
|
||||||
// not actionable, specifically those missing a BaseURL. It trims whitespace before
|
// not actionable, specifically those missing a BaseURL. It trims whitespace before
|
||||||
// evaluation and preserves the relative order of remaining entries.
|
// evaluation and preserves the relative order of remaining entries.
|
||||||
func sanitizeOpenAICompatibility(cfg *Config) {
|
func (cfg *Config) SanitizeOpenAICompatibility() {
|
||||||
if cfg == nil || len(cfg.OpenAICompatibility) == 0 {
|
if cfg == nil || len(cfg.OpenAICompatibility) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -293,9 +293,9 @@ func sanitizeOpenAICompatibility(cfg *Config) {
|
|||||||
cfg.OpenAICompatibility = out
|
cfg.OpenAICompatibility = out
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanitizeCodexKeys removes Codex API key entries missing a BaseURL.
|
// SanitizeCodexKeys removes Codex API key entries missing a BaseURL.
|
||||||
// It trims whitespace and preserves order for remaining entries.
|
// It trims whitespace and preserves order for remaining entries.
|
||||||
func sanitizeCodexKeys(cfg *Config) {
|
func (cfg *Config) SanitizeCodexKeys() {
|
||||||
if cfg == nil || len(cfg.CodexKey) == 0 {
|
if cfg == nil || len(cfg.CodexKey) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -312,7 +312,8 @@ func sanitizeCodexKeys(cfg *Config) {
|
|||||||
cfg.CodexKey = out
|
cfg.CodexKey = out
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeClaudeKeys(cfg *Config) {
|
// SanitizeClaudeKeys normalizes headers for Claude credentials.
|
||||||
|
func (cfg *Config) SanitizeClaudeKeys() {
|
||||||
if cfg == nil || len(cfg.ClaudeKey) == 0 {
|
if cfg == nil || len(cfg.ClaudeKey) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -322,7 +323,8 @@ func normalizeClaudeKeys(cfg *Config) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *Config) SyncGeminiKeys() {
|
// SanitizeGeminiKeys deduplicates and normalizes Gemini credentials.
|
||||||
|
func (cfg *Config) SanitizeGeminiKeys() {
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user