Browse Source

♻️ refactor(setting): move API info functions to dedicated module

Move validateApiInfo and getApiInfo functions from controller layer to
setting/api_info.go to improve code organization and separation of concerns.

Changes:
- Create setting/api_info.go with ValidateApiInfo() and GetApiInfo() functions
- Remove validateApiInfo function from controller/option.go
- Remove getApiInfo function from controller/misc.go
- Update function calls to use setting package
- Clean up unused imports (net/url, regexp, fmt) in controller/option.go

This refactoring aligns the API info configuration management with the
existing pattern used by other setting modules (chat.go, group_ratio.go,
rate_limit.go, etc.) and improves code reusability and maintainability.
Apple\Apple 9 months ago
parent
commit
cbb6bcc4ac
4 changed files with 127 additions and 118 deletions
  1. 1 22
      controller/misc.go
  2. 1 95
      controller/option.go
  3. 124 0
      setting/api_info.go
  4. 1 1
      web/src/pages/Detail/index.js

+ 1 - 22
controller/misc.go

@@ -74,33 +74,12 @@ func GetStatus(c *gin.Context) {
 			"oidc_client_id":              system_setting.GetOIDCSettings().ClientId,
 			"oidc_client_id":              system_setting.GetOIDCSettings().ClientId,
 			"oidc_authorization_endpoint": system_setting.GetOIDCSettings().AuthorizationEndpoint,
 			"oidc_authorization_endpoint": system_setting.GetOIDCSettings().AuthorizationEndpoint,
 			"setup":                       constant.Setup,
 			"setup":                       constant.Setup,
-			"api_info":                    getApiInfo(),
+			"api_info":                    setting.GetApiInfo(),
 		},
 		},
 	})
 	})
 	return
 	return
 }
 }
 
 
-func getApiInfo() []map[string]interface{} {
-	// 从OptionMap中获取API信息,如果不存在则返回空数组
-	common.OptionMapRWMutex.RLock()
-	apiInfoStr, exists := common.OptionMap["ApiInfo"]
-	common.OptionMapRWMutex.RUnlock()
-	
-	if !exists || apiInfoStr == "" {
-		// 如果没有配置,返回空数组
-		return []map[string]interface{}{}
-	}
-	
-	// 解析存储的API信息
-	var apiInfo []map[string]interface{}
-	if err := json.Unmarshal([]byte(apiInfoStr), &apiInfo); err != nil {
-		// 如果解析失败,返回空数组
-		return []map[string]interface{}{}
-	}
-	
-	return apiInfo
-}
-
 func GetNotice(c *gin.Context) {
 func GetNotice(c *gin.Context) {
 	common.OptionMapRWMutex.RLock()
 	common.OptionMapRWMutex.RLock()
 	defer common.OptionMapRWMutex.RUnlock()
 	defer common.OptionMapRWMutex.RUnlock()

+ 1 - 95
controller/option.go

@@ -2,110 +2,16 @@ package controller
 
 
 import (
 import (
 	"encoding/json"
 	"encoding/json"
-	"fmt"
 	"net/http"
 	"net/http"
-	"net/url"
 	"one-api/common"
 	"one-api/common"
 	"one-api/model"
 	"one-api/model"
 	"one-api/setting"
 	"one-api/setting"
 	"one-api/setting/system_setting"
 	"one-api/setting/system_setting"
-	"regexp"
 	"strings"
 	"strings"
 
 
 	"github.com/gin-gonic/gin"
 	"github.com/gin-gonic/gin"
 )
 )
 
 
-func validateApiInfo(apiInfoStr string) error {
-	if apiInfoStr == "" {
-		return nil // 空字符串是合法的
-	}
-	
-	var apiInfoList []map[string]interface{}
-	if err := json.Unmarshal([]byte(apiInfoStr), &apiInfoList); err != nil {
-		return fmt.Errorf("API信息格式错误:%s", err.Error())
-	}
-	
-	// 验证数组长度
-	if len(apiInfoList) > 50 {
-		return fmt.Errorf("API信息数量不能超过50个")
-	}
-	
-	// 允许的颜色值
-	validColors := map[string]bool{
-		"blue": true, "green": true, "cyan": true, "purple": true, "pink": true,
-		"red": true, "orange": true, "amber": true, "yellow": true, "lime": true,
-		"light-green": true, "teal": true, "light-blue": true, "indigo": true,
-		"violet": true, "grey": true,
-	}
-	
-	// URL正则表达式
-	urlRegex := regexp.MustCompile(`^https?://[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*(/.*)?$`)
-	
-	for i, apiInfo := range apiInfoList {
-		// 检查必填字段
-		urlStr, ok := apiInfo["url"].(string)
-		if !ok || urlStr == "" {
-			return fmt.Errorf("第%d个API信息缺少URL字段", i+1)
-		}
-		
-		route, ok := apiInfo["route"].(string)
-		if !ok || route == "" {
-			return fmt.Errorf("第%d个API信息缺少线路描述字段", i+1)
-		}
-		
-		description, ok := apiInfo["description"].(string)
-		if !ok || description == "" {
-			return fmt.Errorf("第%d个API信息缺少说明字段", i+1)
-		}
-		
-		color, ok := apiInfo["color"].(string)
-		if !ok || color == "" {
-			return fmt.Errorf("第%d个API信息缺少颜色字段", i+1)
-		}
-		
-		// 验证URL格式
-		if !urlRegex.MatchString(urlStr) {
-			return fmt.Errorf("第%d个API信息的URL格式不正确", i+1)
-		}
-		
-		// 验证URL可解析性
-		if _, err := url.Parse(urlStr); err != nil {
-			return fmt.Errorf("第%d个API信息的URL无法解析:%s", i+1, err.Error())
-		}
-		
-		// 验证字段长度
-		if len(urlStr) > 500 {
-			return fmt.Errorf("第%d个API信息的URL长度不能超过500字符", i+1)
-		}
-		
-		if len(route) > 100 {
-			return fmt.Errorf("第%d个API信息的线路描述长度不能超过100字符", i+1)
-		}
-		
-		if len(description) > 200 {
-			return fmt.Errorf("第%d个API信息的说明长度不能超过200字符", i+1)
-		}
-		
-		// 验证颜色值
-		if !validColors[color] {
-			return fmt.Errorf("第%d个API信息的颜色值不合法", i+1)
-		}
-		
-		// 检查并过滤危险字符(防止XSS)
-		dangerousChars := []string{"<script", "<iframe", "javascript:", "onload=", "onerror=", "onclick="}
-		for _, dangerous := range dangerousChars {
-			if strings.Contains(strings.ToLower(description), dangerous) {
-				return fmt.Errorf("第%d个API信息的说明包含不允许的内容", i+1)
-			}
-			if strings.Contains(strings.ToLower(route), dangerous) {
-				return fmt.Errorf("第%d个API信息的线路描述包含不允许的内容", i+1)
-			}
-		}
-	}
-	
-	return nil
-}
-
 func GetOptions(c *gin.Context) {
 func GetOptions(c *gin.Context) {
 	var options []*model.Option
 	var options []*model.Option
 	common.OptionMapRWMutex.Lock()
 	common.OptionMapRWMutex.Lock()
@@ -214,7 +120,7 @@ func UpdateOption(c *gin.Context) {
 			return
 			return
 		}
 		}
 	case "ApiInfo":
 	case "ApiInfo":
-		err = validateApiInfo(option.Value)
+		err = setting.ValidateApiInfo(option.Value)
 		if err != nil {
 		if err != nil {
 			c.JSON(http.StatusOK, gin.H{
 			c.JSON(http.StatusOK, gin.H{
 				"success": false,
 				"success": false,

+ 124 - 0
setting/api_info.go

@@ -0,0 +1,124 @@
+package setting
+
+import (
+	"encoding/json"
+	"fmt"
+	"net/url"
+	"one-api/common"
+	"regexp"
+	"strings"
+)
+
+// ValidateApiInfo 验证API信息格式
+func ValidateApiInfo(apiInfoStr string) error {
+	if apiInfoStr == "" {
+		return nil // 空字符串是合法的
+	}
+	
+	var apiInfoList []map[string]interface{}
+	if err := json.Unmarshal([]byte(apiInfoStr), &apiInfoList); err != nil {
+		return fmt.Errorf("API信息格式错误:%s", err.Error())
+	}
+	
+	// 验证数组长度
+	if len(apiInfoList) > 50 {
+		return fmt.Errorf("API信息数量不能超过50个")
+	}
+	
+	// 允许的颜色值
+	validColors := map[string]bool{
+		"blue": true, "green": true, "cyan": true, "purple": true, "pink": true,
+		"red": true, "orange": true, "amber": true, "yellow": true, "lime": true,
+		"light-green": true, "teal": true, "light-blue": true, "indigo": true,
+		"violet": true, "grey": true,
+	}
+	
+	// URL正则表达式
+	urlRegex := regexp.MustCompile(`^https?://[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*(/.*)?$`)
+	
+	for i, apiInfo := range apiInfoList {
+		// 检查必填字段
+		urlStr, ok := apiInfo["url"].(string)
+		if !ok || urlStr == "" {
+			return fmt.Errorf("第%d个API信息缺少URL字段", i+1)
+		}
+		
+		route, ok := apiInfo["route"].(string)
+		if !ok || route == "" {
+			return fmt.Errorf("第%d个API信息缺少线路描述字段", i+1)
+		}
+		
+		description, ok := apiInfo["description"].(string)
+		if !ok || description == "" {
+			return fmt.Errorf("第%d个API信息缺少说明字段", i+1)
+		}
+		
+		color, ok := apiInfo["color"].(string)
+		if !ok || color == "" {
+			return fmt.Errorf("第%d个API信息缺少颜色字段", i+1)
+		}
+		
+		// 验证URL格式
+		if !urlRegex.MatchString(urlStr) {
+			return fmt.Errorf("第%d个API信息的URL格式不正确", i+1)
+		}
+		
+		// 验证URL可解析性
+		if _, err := url.Parse(urlStr); err != nil {
+			return fmt.Errorf("第%d个API信息的URL无法解析:%s", i+1, err.Error())
+		}
+		
+		// 验证字段长度
+		if len(urlStr) > 500 {
+			return fmt.Errorf("第%d个API信息的URL长度不能超过500字符", i+1)
+		}
+		
+		if len(route) > 100 {
+			return fmt.Errorf("第%d个API信息的线路描述长度不能超过100字符", i+1)
+		}
+		
+		if len(description) > 200 {
+			return fmt.Errorf("第%d个API信息的说明长度不能超过200字符", i+1)
+		}
+		
+		// 验证颜色值
+		if !validColors[color] {
+			return fmt.Errorf("第%d个API信息的颜色值不合法", i+1)
+		}
+		
+		// 检查并过滤危险字符(防止XSS)
+		dangerousChars := []string{"<script", "<iframe", "javascript:", "onload=", "onerror=", "onclick="}
+		for _, dangerous := range dangerousChars {
+			if strings.Contains(strings.ToLower(description), dangerous) {
+				return fmt.Errorf("第%d个API信息的说明包含不允许的内容", i+1)
+			}
+			if strings.Contains(strings.ToLower(route), dangerous) {
+				return fmt.Errorf("第%d个API信息的线路描述包含不允许的内容", i+1)
+			}
+		}
+	}
+	
+	return nil
+}
+
+// GetApiInfo 获取API信息列表
+func GetApiInfo() []map[string]interface{} {
+	// 从OptionMap中获取API信息,如果不存在则返回空数组
+	common.OptionMapRWMutex.RLock()
+	apiInfoStr, exists := common.OptionMap["ApiInfo"]
+	common.OptionMapRWMutex.RUnlock()
+	
+	if !exists || apiInfoStr == "" {
+		// 如果没有配置,返回空数组
+		return []map[string]interface{}{}
+	}
+	
+	// 解析存储的API信息
+	var apiInfo []map[string]interface{}
+	if err := json.Unmarshal([]byte(apiInfoStr), &apiInfo); err != nil {
+		// 如果解析失败,返回空数组
+		return []map[string]interface{}{}
+	}
+	
+	return apiInfo
+} 

+ 1 - 1
web/src/pages/Detail/index.js

@@ -986,7 +986,7 @@ const Detail = (props) => {
                         <div key={api.id} className="flex p-2 hover:bg-white rounded-lg transition-colors cursor-pointer">
                         <div key={api.id} className="flex p-2 hover:bg-white rounded-lg transition-colors cursor-pointer">
                           <div className="flex-shrink-0 mr-3">
                           <div className="flex-shrink-0 mr-3">
                             <Avatar
                             <Avatar
-                              size="extra-extra-small"
+                              size="extra-small"
                               color={api.color}
                               color={api.color}
                             >
                             >
                               {api.route.substring(0, 2)}
                               {api.route.substring(0, 2)}