From d9461a477dfd557b30254c06f75431bb882efd6d Mon Sep 17 00:00:00 2001 From: "Apple\\Apple" Date: Tue, 10 Jun 2025 12:20:26 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20refactor(console):=20enhance=20U?= =?UTF-8?q?RL=20validation=20and=20restructure=20settings=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Refactor api_info.go to console.go for broader console settings support - Update URL regex pattern to accept both domain names and IP addresses - Add support for IPv4 addresses with optional port numbers - Improve validation to handle formats like http://192.168.1.1:8080 - Add ValidateConsoleSettings function for extensible settings validation - Maintain backward compatibility with existing ValidateApiInfo function - Add comprehensive comments explaining supported URL formats Fixes issue where IP-based URLs were incorrectly rejected as invalid format. Prepares infrastructure for additional console settings validation. --- setting/{api_info.go => console.go} | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) rename setting/{api_info.go => console.go} (88%) diff --git a/setting/api_info.go b/setting/console.go similarity index 88% rename from setting/api_info.go rename to setting/console.go index 552dabf5..e7847213 100644 --- a/setting/api_info.go +++ b/setting/console.go @@ -9,12 +9,22 @@ import ( "strings" ) -// ValidateApiInfo 验证API信息格式 -func ValidateApiInfo(apiInfoStr string) error { - if apiInfoStr == "" { +// ValidateConsoleSettings 验证控制台设置信息格式 +func ValidateConsoleSettings(settingsStr string, settingType string) error { + if settingsStr == "" { return nil // 空字符串是合法的 } + switch settingType { + case "ApiInfo": + return validateApiInfo(settingsStr) + default: + return fmt.Errorf("未知的设置类型:%s", settingType) + } +} + +// validateApiInfo 验证API信息格式 +func validateApiInfo(apiInfoStr string) error { var apiInfoList []map[string]interface{} if err := json.Unmarshal([]byte(apiInfoStr), &apiInfoList); err != nil { return fmt.Errorf("API信息格式错误:%s", err.Error()) @@ -103,6 +113,11 @@ func ValidateApiInfo(apiInfoStr string) error { return nil } +// ValidateApiInfo 保持向后兼容的函数 +func ValidateApiInfo(apiInfoStr string) error { + return validateApiInfo(apiInfoStr) +} + // GetApiInfo 获取API信息列表 func GetApiInfo() []map[string]interface{} { // 从OptionMap中获取API信息,如果不存在则返回空数组