mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-02 20:40:52 +08:00
refactor(api): remove legacy generative-language-api-key endpoints and duplicate GetConfigYAML
This commit is contained in:
@@ -20,25 +20,6 @@ func (h *Handler) GetConfig(c *gin.Context) {
|
|||||||
c.JSON(200, &cfgCopy)
|
c.JSON(200, &cfgCopy)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) GetConfigYAML(c *gin.Context) {
|
|
||||||
data, err := os.ReadFile(h.configFilePath)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "read_failed", "message": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var node yaml.Node
|
|
||||||
if err = yaml.Unmarshal(data, &node); err != nil {
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "parse_failed", "message": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.Header("Content-Type", "application/yaml; charset=utf-8")
|
|
||||||
c.Header("Vary", "format, Accept")
|
|
||||||
enc := yaml.NewEncoder(c.Writer)
|
|
||||||
enc.SetIndent(2)
|
|
||||||
_ = enc.Encode(&node)
|
|
||||||
_ = enc.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func WriteConfig(path string, data []byte) error {
|
func WriteConfig(path string, data []byte) error {
|
||||||
data = config.NormalizeCommentIndentation(data)
|
data = config.NormalizeCommentIndentation(data)
|
||||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
@@ -110,9 +91,9 @@ func (h *Handler) PutConfigYAML(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"ok": true, "changed": []string{"config"}})
|
c.JSON(http.StatusOK, gin.H{"ok": true, "changed": []string{"config"}})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConfigFile returns the raw config.yaml file bytes without re-encoding.
|
// GetConfigYAML returns the raw config.yaml file bytes without re-encoding.
|
||||||
// It preserves comments and original formatting/styles.
|
// It preserves comments and original formatting/styles.
|
||||||
func (h *Handler) GetConfigFile(c *gin.Context) {
|
func (h *Handler) GetConfigYAML(c *gin.Context) {
|
||||||
data, err := os.ReadFile(h.configFilePath)
|
data, err := os.ReadFile(h.configFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
|
|||||||
@@ -104,52 +104,6 @@ func (h *Handler) deleteFromStringList(c *gin.Context, target *[]string, after f
|
|||||||
c.JSON(400, gin.H{"error": "missing index or value"})
|
c.JSON(400, gin.H{"error": "missing index or value"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func sanitizeStringSlice(in []string) []string {
|
|
||||||
out := make([]string, 0, len(in))
|
|
||||||
for i := range in {
|
|
||||||
if trimmed := strings.TrimSpace(in[i]); trimmed != "" {
|
|
||||||
out = append(out, trimmed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func geminiKeyStringsFromConfig(cfg *config.Config) []string {
|
|
||||||
if cfg == nil || len(cfg.GeminiKey) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
out := make([]string, 0, len(cfg.GeminiKey))
|
|
||||||
for i := range cfg.GeminiKey {
|
|
||||||
if key := strings.TrimSpace(cfg.GeminiKey[i].APIKey); key != "" {
|
|
||||||
out = append(out, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) applyLegacyKeys(keys []string) {
|
|
||||||
if h == nil || h.cfg == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sanitized := sanitizeStringSlice(keys)
|
|
||||||
existing := make(map[string]config.GeminiKey, len(h.cfg.GeminiKey))
|
|
||||||
for _, entry := range h.cfg.GeminiKey {
|
|
||||||
if key := strings.TrimSpace(entry.APIKey); key != "" {
|
|
||||||
existing[key] = entry
|
|
||||||
}
|
|
||||||
}
|
|
||||||
newList := make([]config.GeminiKey, 0, len(sanitized))
|
|
||||||
for _, key := range sanitized {
|
|
||||||
if entry, ok := existing[key]; ok {
|
|
||||||
newList = append(newList, entry)
|
|
||||||
} else {
|
|
||||||
newList = append(newList, config.GeminiKey{APIKey: key})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
h.cfg.GeminiKey = newList
|
|
||||||
h.cfg.SanitizeGeminiKeys()
|
|
||||||
}
|
|
||||||
|
|
||||||
// api-keys
|
// api-keys
|
||||||
func (h *Handler) GetAPIKeys(c *gin.Context) { c.JSON(200, gin.H{"api-keys": h.cfg.APIKeys}) }
|
func (h *Handler) GetAPIKeys(c *gin.Context) { c.JSON(200, gin.H{"api-keys": h.cfg.APIKeys}) }
|
||||||
func (h *Handler) PutAPIKeys(c *gin.Context) {
|
func (h *Handler) PutAPIKeys(c *gin.Context) {
|
||||||
@@ -165,24 +119,6 @@ func (h *Handler) DeleteAPIKeys(c *gin.Context) {
|
|||||||
h.deleteFromStringList(c, &h.cfg.APIKeys, func() { h.cfg.Access.Providers = nil })
|
h.deleteFromStringList(c, &h.cfg.APIKeys, func() { h.cfg.Access.Providers = nil })
|
||||||
}
|
}
|
||||||
|
|
||||||
// generative-language-api-key
|
|
||||||
func (h *Handler) GetGlKeys(c *gin.Context) {
|
|
||||||
c.JSON(200, gin.H{"generative-language-api-key": geminiKeyStringsFromConfig(h.cfg)})
|
|
||||||
}
|
|
||||||
func (h *Handler) PutGlKeys(c *gin.Context) {
|
|
||||||
h.putStringList(c, func(v []string) {
|
|
||||||
h.applyLegacyKeys(v)
|
|
||||||
}, nil)
|
|
||||||
}
|
|
||||||
func (h *Handler) PatchGlKeys(c *gin.Context) {
|
|
||||||
target := append([]string(nil), geminiKeyStringsFromConfig(h.cfg)...)
|
|
||||||
h.patchStringList(c, &target, func() { h.applyLegacyKeys(target) })
|
|
||||||
}
|
|
||||||
func (h *Handler) DeleteGlKeys(c *gin.Context) {
|
|
||||||
target := append([]string(nil), geminiKeyStringsFromConfig(h.cfg)...)
|
|
||||||
h.deleteFromStringList(c, &target, func() { h.applyLegacyKeys(target) })
|
|
||||||
}
|
|
||||||
|
|
||||||
// gemini-api-key: []GeminiKey
|
// gemini-api-key: []GeminiKey
|
||||||
func (h *Handler) GetGeminiKeys(c *gin.Context) {
|
func (h *Handler) GetGeminiKeys(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{"gemini-api-key": h.cfg.GeminiKey})
|
c.JSON(200, gin.H{"gemini-api-key": h.cfg.GeminiKey})
|
||||||
|
|||||||
@@ -470,8 +470,8 @@ func (s *Server) registerManagementRoutes() {
|
|||||||
{
|
{
|
||||||
mgmt.GET("/usage", s.mgmt.GetUsageStatistics)
|
mgmt.GET("/usage", s.mgmt.GetUsageStatistics)
|
||||||
mgmt.GET("/config", s.mgmt.GetConfig)
|
mgmt.GET("/config", s.mgmt.GetConfig)
|
||||||
|
mgmt.GET("/config.yaml", s.mgmt.GetConfigYAML)
|
||||||
mgmt.PUT("/config.yaml", s.mgmt.PutConfigYAML)
|
mgmt.PUT("/config.yaml", s.mgmt.PutConfigYAML)
|
||||||
mgmt.GET("/config.yaml", s.mgmt.GetConfigFile)
|
|
||||||
|
|
||||||
mgmt.GET("/debug", s.mgmt.GetDebug)
|
mgmt.GET("/debug", s.mgmt.GetDebug)
|
||||||
mgmt.PUT("/debug", s.mgmt.PutDebug)
|
mgmt.PUT("/debug", s.mgmt.PutDebug)
|
||||||
@@ -503,11 +503,6 @@ func (s *Server) registerManagementRoutes() {
|
|||||||
mgmt.PATCH("/api-keys", s.mgmt.PatchAPIKeys)
|
mgmt.PATCH("/api-keys", s.mgmt.PatchAPIKeys)
|
||||||
mgmt.DELETE("/api-keys", s.mgmt.DeleteAPIKeys)
|
mgmt.DELETE("/api-keys", s.mgmt.DeleteAPIKeys)
|
||||||
|
|
||||||
mgmt.GET("/generative-language-api-key", s.mgmt.GetGlKeys)
|
|
||||||
mgmt.PUT("/generative-language-api-key", s.mgmt.PutGlKeys)
|
|
||||||
mgmt.PATCH("/generative-language-api-key", s.mgmt.PatchGlKeys)
|
|
||||||
mgmt.DELETE("/generative-language-api-key", s.mgmt.DeleteGlKeys)
|
|
||||||
|
|
||||||
mgmt.GET("/gemini-api-key", s.mgmt.GetGeminiKeys)
|
mgmt.GET("/gemini-api-key", s.mgmt.GetGeminiKeys)
|
||||||
mgmt.PUT("/gemini-api-key", s.mgmt.PutGeminiKeys)
|
mgmt.PUT("/gemini-api-key", s.mgmt.PutGeminiKeys)
|
||||||
mgmt.PATCH("/gemini-api-key", s.mgmt.PatchGeminiKey)
|
mgmt.PATCH("/gemini-api-key", s.mgmt.PatchGeminiKey)
|
||||||
|
|||||||
Reference in New Issue
Block a user