🎨 style(go): format entire codebase
- Apply canonical Go formatting to all .go files - No functional changes; whitespace/import/struct layout only - Improves consistency, reduces diff noise, and aligns with standard tooling
This commit is contained in:
@@ -257,32 +257,32 @@ func GetAudioDuration(ctx context.Context, filename string, ext string) (float64
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.Wrap(err, "failed to get audio duration")
|
return 0, errors.Wrap(err, "failed to get audio duration")
|
||||||
}
|
}
|
||||||
durationStr := string(bytes.TrimSpace(output))
|
durationStr := string(bytes.TrimSpace(output))
|
||||||
if durationStr == "N/A" {
|
if durationStr == "N/A" {
|
||||||
// Create a temporary output file name
|
// Create a temporary output file name
|
||||||
tmpFp, err := os.CreateTemp("", "audio-*"+ext)
|
tmpFp, err := os.CreateTemp("", "audio-*"+ext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.Wrap(err, "failed to create temporary file")
|
return 0, errors.Wrap(err, "failed to create temporary file")
|
||||||
}
|
}
|
||||||
tmpName := tmpFp.Name()
|
tmpName := tmpFp.Name()
|
||||||
// Close immediately so ffmpeg can open the file on Windows.
|
// Close immediately so ffmpeg can open the file on Windows.
|
||||||
_ = tmpFp.Close()
|
_ = tmpFp.Close()
|
||||||
defer os.Remove(tmpName)
|
defer os.Remove(tmpName)
|
||||||
|
|
||||||
// ffmpeg -y -i filename -vcodec copy -acodec copy <tmpName>
|
// ffmpeg -y -i filename -vcodec copy -acodec copy <tmpName>
|
||||||
ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-vcodec", "copy", "-acodec", "copy", tmpName)
|
ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-vcodec", "copy", "-acodec", "copy", tmpName)
|
||||||
if err := ffmpegCmd.Run(); err != nil {
|
if err := ffmpegCmd.Run(); err != nil {
|
||||||
return 0, errors.Wrap(err, "failed to run ffmpeg")
|
return 0, errors.Wrap(err, "failed to run ffmpeg")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recalculate the duration of the new file
|
// Recalculate the duration of the new file
|
||||||
c = exec.CommandContext(ctx, "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", tmpName)
|
c = exec.CommandContext(ctx, "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", tmpName)
|
||||||
output, err := c.Output()
|
output, err := c.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.Wrap(err, "failed to get audio duration after ffmpeg")
|
return 0, errors.Wrap(err, "failed to get audio duration after ffmpeg")
|
||||||
}
|
}
|
||||||
durationStr = string(bytes.TrimSpace(output))
|
durationStr = string(bytes.TrimSpace(output))
|
||||||
}
|
}
|
||||||
return strconv.ParseFloat(durationStr, 64)
|
return strconv.ParseFloat(durationStr, 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +1,24 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"one-api/setting/ratio_setting"
|
"one-api/setting/ratio_setting"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetRatioConfig(c *gin.Context) {
|
func GetRatioConfig(c *gin.Context) {
|
||||||
if !ratio_setting.IsExposeRatioEnabled() {
|
if !ratio_setting.IsExposeRatioEnabled() {
|
||||||
c.JSON(http.StatusForbidden, gin.H{
|
c.JSON(http.StatusForbidden, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "倍率配置接口未启用",
|
"message": "倍率配置接口未启用",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": "",
|
"message": "",
|
||||||
"data": ratio_setting.GetExposedData(),
|
"data": ratio_setting.GetExposedData(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ func updateVideoSingleTask(ctx context.Context, adaptor channel.TaskAdaptor, cha
|
|||||||
task.StartTime = now
|
task.StartTime = now
|
||||||
}
|
}
|
||||||
case model.TaskStatusSuccess:
|
case model.TaskStatusSuccess:
|
||||||
task.Progress = "100%"
|
task.Progress = "100%"
|
||||||
if task.FinishTime == 0 {
|
if task.FinishTime == 0 {
|
||||||
task.FinishTime = now
|
task.FinishTime = now
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ type Monitor struct {
|
|||||||
|
|
||||||
type UptimeGroupResult struct {
|
type UptimeGroupResult struct {
|
||||||
CategoryName string `json:"categoryName"`
|
CategoryName string `json:"categoryName"`
|
||||||
Monitors []Monitor `json:"monitors"`
|
Monitors []Monitor `json:"monitors"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAndDecode(ctx context.Context, client *http.Client, url string, dest interface{}) error {
|
func getAndDecode(ctx context.Context, client *http.Client, url string, dest interface{}) error {
|
||||||
@@ -57,29 +57,29 @@ func fetchGroupData(ctx context.Context, client *http.Client, groupConfig map[st
|
|||||||
url, _ := groupConfig["url"].(string)
|
url, _ := groupConfig["url"].(string)
|
||||||
slug, _ := groupConfig["slug"].(string)
|
slug, _ := groupConfig["slug"].(string)
|
||||||
categoryName, _ := groupConfig["categoryName"].(string)
|
categoryName, _ := groupConfig["categoryName"].(string)
|
||||||
|
|
||||||
result := UptimeGroupResult{
|
result := UptimeGroupResult{
|
||||||
CategoryName: categoryName,
|
CategoryName: categoryName,
|
||||||
Monitors: []Monitor{},
|
Monitors: []Monitor{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if url == "" || slug == "" {
|
if url == "" || slug == "" {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
baseURL := strings.TrimSuffix(url, "/")
|
baseURL := strings.TrimSuffix(url, "/")
|
||||||
|
|
||||||
var statusData struct {
|
var statusData struct {
|
||||||
PublicGroupList []struct {
|
PublicGroupList []struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
MonitorList []struct {
|
MonitorList []struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
} `json:"monitorList"`
|
} `json:"monitorList"`
|
||||||
} `json:"publicGroupList"`
|
} `json:"publicGroupList"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var heartbeatData struct {
|
var heartbeatData struct {
|
||||||
HeartbeatList map[string][]struct {
|
HeartbeatList map[string][]struct {
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
@@ -88,11 +88,11 @@ func fetchGroupData(ctx context.Context, client *http.Client, groupConfig map[st
|
|||||||
}
|
}
|
||||||
|
|
||||||
g, gCtx := errgroup.WithContext(ctx)
|
g, gCtx := errgroup.WithContext(ctx)
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
return getAndDecode(gCtx, client, baseURL+apiStatusPath+slug, &statusData)
|
return getAndDecode(gCtx, client, baseURL+apiStatusPath+slug, &statusData)
|
||||||
})
|
})
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
return getAndDecode(gCtx, client, baseURL+apiHeartbeatPath+slug, &heartbeatData)
|
return getAndDecode(gCtx, client, baseURL+apiHeartbeatPath+slug, &heartbeatData)
|
||||||
})
|
})
|
||||||
|
|
||||||
if g.Wait() != nil {
|
if g.Wait() != nil {
|
||||||
@@ -139,7 +139,7 @@ func GetUptimeKumaStatus(c *gin.Context) {
|
|||||||
|
|
||||||
client := &http.Client{Timeout: httpTimeout}
|
client := &http.Client{Timeout: httpTimeout}
|
||||||
results := make([]UptimeGroupResult, len(groups))
|
results := make([]UptimeGroupResult, len(groups))
|
||||||
|
|
||||||
g, gCtx := errgroup.WithContext(ctx)
|
g, gCtx := errgroup.WithContext(ctx)
|
||||||
for i, group := range groups {
|
for i, group := range groups {
|
||||||
i, group := i, group
|
i, group := i, group
|
||||||
@@ -148,7 +148,7 @@ func GetUptimeKumaStatus(c *gin.Context) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Wait()
|
g.Wait()
|
||||||
c.JSON(http.StatusOK, gin.H{"success": true, "message": "", "data": results})
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": "", "data": results})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,23 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
type UpstreamDTO struct {
|
type UpstreamDTO struct {
|
||||||
ID int `json:"id,omitempty"`
|
ID int `json:"id,omitempty"`
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
BaseURL string `json:"base_url" binding:"required"`
|
BaseURL string `json:"base_url" binding:"required"`
|
||||||
Endpoint string `json:"endpoint"`
|
Endpoint string `json:"endpoint"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpstreamRequest struct {
|
type UpstreamRequest struct {
|
||||||
ChannelIDs []int64 `json:"channel_ids"`
|
ChannelIDs []int64 `json:"channel_ids"`
|
||||||
Upstreams []UpstreamDTO `json:"upstreams"`
|
Upstreams []UpstreamDTO `json:"upstreams"`
|
||||||
Timeout int `json:"timeout"`
|
Timeout int `json:"timeout"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestResult 上游测试连通性结果
|
// TestResult 上游测试连通性结果
|
||||||
type TestResult struct {
|
type TestResult struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Error string `json:"error,omitempty"`
|
Error string `json:"error,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DifferenceItem 差异项
|
// DifferenceItem 差异项
|
||||||
@@ -25,14 +25,14 @@ type TestResult struct {
|
|||||||
// Upstreams 为各渠道的上游值,具体数值 / "same" / nil
|
// Upstreams 为各渠道的上游值,具体数值 / "same" / nil
|
||||||
|
|
||||||
type DifferenceItem struct {
|
type DifferenceItem struct {
|
||||||
Current interface{} `json:"current"`
|
Current interface{} `json:"current"`
|
||||||
Upstreams map[string]interface{} `json:"upstreams"`
|
Upstreams map[string]interface{} `json:"upstreams"`
|
||||||
Confidence map[string]bool `json:"confidence"`
|
Confidence map[string]bool `json:"confidence"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SyncableChannel struct {
|
type SyncableChannel struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
BaseURL string `json:"base_url"`
|
BaseURL string `json:"base_url"`
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,12 +18,12 @@ func StatsMiddleware() gin.HandlerFunc {
|
|||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
// 增加活跃连接数
|
// 增加活跃连接数
|
||||||
atomic.AddInt64(&globalStats.activeConnections, 1)
|
atomic.AddInt64(&globalStats.activeConnections, 1)
|
||||||
|
|
||||||
// 确保在请求结束时减少连接数
|
// 确保在请求结束时减少连接数
|
||||||
defer func() {
|
defer func() {
|
||||||
atomic.AddInt64(&globalStats.activeConnections, -1)
|
atomic.AddInt64(&globalStats.activeConnections, -1)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,4 +38,4 @@ func GetStats() StatsInfo {
|
|||||||
return StatsInfo{
|
return StatsInfo{
|
||||||
ActiveConnections: atomic.LoadInt64(&globalStats.activeConnections),
|
ActiveConnections: atomic.LoadInt64(&globalStats.activeConnections),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,4 +6,4 @@ var ModelList = []string{
|
|||||||
"m3e-small",
|
"m3e-small",
|
||||||
}
|
}
|
||||||
|
|
||||||
var ChannelName = "mokaai"
|
var ChannelName = "mokaai"
|
||||||
|
|||||||
@@ -3,37 +3,37 @@ package console_setting
|
|||||||
import "one-api/setting/config"
|
import "one-api/setting/config"
|
||||||
|
|
||||||
type ConsoleSetting struct {
|
type ConsoleSetting struct {
|
||||||
ApiInfo string `json:"api_info"` // 控制台 API 信息 (JSON 数组字符串)
|
ApiInfo string `json:"api_info"` // 控制台 API 信息 (JSON 数组字符串)
|
||||||
UptimeKumaGroups string `json:"uptime_kuma_groups"` // Uptime Kuma 分组配置 (JSON 数组字符串)
|
UptimeKumaGroups string `json:"uptime_kuma_groups"` // Uptime Kuma 分组配置 (JSON 数组字符串)
|
||||||
Announcements string `json:"announcements"` // 系统公告 (JSON 数组字符串)
|
Announcements string `json:"announcements"` // 系统公告 (JSON 数组字符串)
|
||||||
FAQ string `json:"faq"` // 常见问题 (JSON 数组字符串)
|
FAQ string `json:"faq"` // 常见问题 (JSON 数组字符串)
|
||||||
ApiInfoEnabled bool `json:"api_info_enabled"` // 是否启用 API 信息面板
|
ApiInfoEnabled bool `json:"api_info_enabled"` // 是否启用 API 信息面板
|
||||||
UptimeKumaEnabled bool `json:"uptime_kuma_enabled"` // 是否启用 Uptime Kuma 面板
|
UptimeKumaEnabled bool `json:"uptime_kuma_enabled"` // 是否启用 Uptime Kuma 面板
|
||||||
AnnouncementsEnabled bool `json:"announcements_enabled"` // 是否启用系统公告面板
|
AnnouncementsEnabled bool `json:"announcements_enabled"` // 是否启用系统公告面板
|
||||||
FAQEnabled bool `json:"faq_enabled"` // 是否启用常见问答面板
|
FAQEnabled bool `json:"faq_enabled"` // 是否启用常见问答面板
|
||||||
}
|
}
|
||||||
|
|
||||||
// 默认配置
|
// 默认配置
|
||||||
var defaultConsoleSetting = ConsoleSetting{
|
var defaultConsoleSetting = ConsoleSetting{
|
||||||
ApiInfo: "",
|
ApiInfo: "",
|
||||||
UptimeKumaGroups: "",
|
UptimeKumaGroups: "",
|
||||||
Announcements: "",
|
Announcements: "",
|
||||||
FAQ: "",
|
FAQ: "",
|
||||||
ApiInfoEnabled: true,
|
ApiInfoEnabled: true,
|
||||||
UptimeKumaEnabled: true,
|
UptimeKumaEnabled: true,
|
||||||
AnnouncementsEnabled: true,
|
AnnouncementsEnabled: true,
|
||||||
FAQEnabled: true,
|
FAQEnabled: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 全局实例
|
// 全局实例
|
||||||
var consoleSetting = defaultConsoleSetting
|
var consoleSetting = defaultConsoleSetting
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// 注册到全局配置管理器,键名为 console_setting
|
// 注册到全局配置管理器,键名为 console_setting
|
||||||
config.GlobalConfig.Register("console_setting", &consoleSetting)
|
config.GlobalConfig.Register("console_setting", &consoleSetting)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConsoleSetting 获取 ConsoleSetting 配置实例
|
// GetConsoleSetting 获取 ConsoleSetting 配置实例
|
||||||
func GetConsoleSetting() *ConsoleSetting {
|
func GetConsoleSetting() *ConsoleSetting {
|
||||||
return &consoleSetting
|
return &consoleSetting
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,304 +1,304 @@
|
|||||||
package console_setting
|
package console_setting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"sort"
|
||||||
"time"
|
"strings"
|
||||||
"sort"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
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])?|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:\:[0-9]{1,5})?(?:/.*)?$`)
|
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])?|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:\:[0-9]{1,5})?(?:/.*)?$`)
|
||||||
dangerousChars = []string{"<script", "<iframe", "javascript:", "onload=", "onerror=", "onclick="}
|
dangerousChars = []string{"<script", "<iframe", "javascript:", "onload=", "onerror=", "onclick="}
|
||||||
validColors = map[string]bool{
|
validColors = map[string]bool{
|
||||||
"blue": true, "green": true, "cyan": true, "purple": true, "pink": true,
|
"blue": true, "green": true, "cyan": true, "purple": true, "pink": true,
|
||||||
"red": true, "orange": true, "amber": true, "yellow": true, "lime": true,
|
"red": true, "orange": true, "amber": true, "yellow": true, "lime": true,
|
||||||
"light-green": true, "teal": true, "light-blue": true, "indigo": true,
|
"light-green": true, "teal": true, "light-blue": true, "indigo": true,
|
||||||
"violet": true, "grey": true,
|
"violet": true, "grey": true,
|
||||||
}
|
}
|
||||||
slugRegex = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
|
slugRegex = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseJSONArray(jsonStr string, typeName string) ([]map[string]interface{}, error) {
|
func parseJSONArray(jsonStr string, typeName string) ([]map[string]interface{}, error) {
|
||||||
var list []map[string]interface{}
|
var list []map[string]interface{}
|
||||||
if err := json.Unmarshal([]byte(jsonStr), &list); err != nil {
|
if err := json.Unmarshal([]byte(jsonStr), &list); err != nil {
|
||||||
return nil, fmt.Errorf("%s格式错误:%s", typeName, err.Error())
|
return nil, fmt.Errorf("%s格式错误:%s", typeName, err.Error())
|
||||||
}
|
}
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateURL(urlStr string, index int, itemType string) error {
|
func validateURL(urlStr string, index int, itemType string) error {
|
||||||
if !urlRegex.MatchString(urlStr) {
|
if !urlRegex.MatchString(urlStr) {
|
||||||
return fmt.Errorf("第%d个%s的URL格式不正确", index, itemType)
|
return fmt.Errorf("第%d个%s的URL格式不正确", index, itemType)
|
||||||
}
|
}
|
||||||
if _, err := url.Parse(urlStr); err != nil {
|
if _, err := url.Parse(urlStr); err != nil {
|
||||||
return fmt.Errorf("第%d个%s的URL无法解析:%s", index, itemType, err.Error())
|
return fmt.Errorf("第%d个%s的URL无法解析:%s", index, itemType, err.Error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkDangerousContent(content string, index int, itemType string) error {
|
func checkDangerousContent(content string, index int, itemType string) error {
|
||||||
lower := strings.ToLower(content)
|
lower := strings.ToLower(content)
|
||||||
for _, d := range dangerousChars {
|
for _, d := range dangerousChars {
|
||||||
if strings.Contains(lower, d) {
|
if strings.Contains(lower, d) {
|
||||||
return fmt.Errorf("第%d个%s包含不允许的内容", index, itemType)
|
return fmt.Errorf("第%d个%s包含不允许的内容", index, itemType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getJSONList(jsonStr string) []map[string]interface{} {
|
func getJSONList(jsonStr string) []map[string]interface{} {
|
||||||
if jsonStr == "" {
|
if jsonStr == "" {
|
||||||
return []map[string]interface{}{}
|
return []map[string]interface{}{}
|
||||||
}
|
}
|
||||||
var list []map[string]interface{}
|
var list []map[string]interface{}
|
||||||
json.Unmarshal([]byte(jsonStr), &list)
|
json.Unmarshal([]byte(jsonStr), &list)
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateConsoleSettings(settingsStr string, settingType string) error {
|
func ValidateConsoleSettings(settingsStr string, settingType string) error {
|
||||||
if settingsStr == "" {
|
if settingsStr == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch settingType {
|
switch settingType {
|
||||||
case "ApiInfo":
|
case "ApiInfo":
|
||||||
return validateApiInfo(settingsStr)
|
return validateApiInfo(settingsStr)
|
||||||
case "Announcements":
|
case "Announcements":
|
||||||
return validateAnnouncements(settingsStr)
|
return validateAnnouncements(settingsStr)
|
||||||
case "FAQ":
|
case "FAQ":
|
||||||
return validateFAQ(settingsStr)
|
return validateFAQ(settingsStr)
|
||||||
case "UptimeKumaGroups":
|
case "UptimeKumaGroups":
|
||||||
return validateUptimeKumaGroups(settingsStr)
|
return validateUptimeKumaGroups(settingsStr)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("未知的设置类型:%s", settingType)
|
return fmt.Errorf("未知的设置类型:%s", settingType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateApiInfo(apiInfoStr string) error {
|
func validateApiInfo(apiInfoStr string) error {
|
||||||
apiInfoList, err := parseJSONArray(apiInfoStr, "API信息")
|
apiInfoList, err := parseJSONArray(apiInfoStr, "API信息")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(apiInfoList) > 50 {
|
if len(apiInfoList) > 50 {
|
||||||
return fmt.Errorf("API信息数量不能超过50个")
|
return fmt.Errorf("API信息数量不能超过50个")
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, apiInfo := range apiInfoList {
|
for i, apiInfo := range apiInfoList {
|
||||||
urlStr, ok := apiInfo["url"].(string)
|
urlStr, ok := apiInfo["url"].(string)
|
||||||
if !ok || urlStr == "" {
|
if !ok || urlStr == "" {
|
||||||
return fmt.Errorf("第%d个API信息缺少URL字段", i+1)
|
return fmt.Errorf("第%d个API信息缺少URL字段", i+1)
|
||||||
}
|
}
|
||||||
route, ok := apiInfo["route"].(string)
|
route, ok := apiInfo["route"].(string)
|
||||||
if !ok || route == "" {
|
if !ok || route == "" {
|
||||||
return fmt.Errorf("第%d个API信息缺少线路描述字段", i+1)
|
return fmt.Errorf("第%d个API信息缺少线路描述字段", i+1)
|
||||||
}
|
}
|
||||||
description, ok := apiInfo["description"].(string)
|
description, ok := apiInfo["description"].(string)
|
||||||
if !ok || description == "" {
|
if !ok || description == "" {
|
||||||
return fmt.Errorf("第%d个API信息缺少说明字段", i+1)
|
return fmt.Errorf("第%d个API信息缺少说明字段", i+1)
|
||||||
}
|
}
|
||||||
color, ok := apiInfo["color"].(string)
|
color, ok := apiInfo["color"].(string)
|
||||||
if !ok || color == "" {
|
if !ok || color == "" {
|
||||||
return fmt.Errorf("第%d个API信息缺少颜色字段", i+1)
|
return fmt.Errorf("第%d个API信息缺少颜色字段", i+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := validateURL(urlStr, i+1, "API信息"); err != nil {
|
if err := validateURL(urlStr, i+1, "API信息"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(urlStr) > 500 {
|
if len(urlStr) > 500 {
|
||||||
return fmt.Errorf("第%d个API信息的URL长度不能超过500字符", i+1)
|
return fmt.Errorf("第%d个API信息的URL长度不能超过500字符", i+1)
|
||||||
}
|
}
|
||||||
if len(route) > 100 {
|
if len(route) > 100 {
|
||||||
return fmt.Errorf("第%d个API信息的线路描述长度不能超过100字符", i+1)
|
return fmt.Errorf("第%d个API信息的线路描述长度不能超过100字符", i+1)
|
||||||
}
|
}
|
||||||
if len(description) > 200 {
|
if len(description) > 200 {
|
||||||
return fmt.Errorf("第%d个API信息的说明长度不能超过200字符", i+1)
|
return fmt.Errorf("第%d个API信息的说明长度不能超过200字符", i+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !validColors[color] {
|
if !validColors[color] {
|
||||||
return fmt.Errorf("第%d个API信息的颜色值不合法", i+1)
|
return fmt.Errorf("第%d个API信息的颜色值不合法", i+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := checkDangerousContent(description, i+1, "API信息"); err != nil {
|
if err := checkDangerousContent(description, i+1, "API信息"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := checkDangerousContent(route, i+1, "API信息"); err != nil {
|
if err := checkDangerousContent(route, i+1, "API信息"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetApiInfo() []map[string]interface{} {
|
func GetApiInfo() []map[string]interface{} {
|
||||||
return getJSONList(GetConsoleSetting().ApiInfo)
|
return getJSONList(GetConsoleSetting().ApiInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateAnnouncements(announcementsStr string) error {
|
func validateAnnouncements(announcementsStr string) error {
|
||||||
list, err := parseJSONArray(announcementsStr, "系统公告")
|
list, err := parseJSONArray(announcementsStr, "系统公告")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(list) > 100 {
|
if len(list) > 100 {
|
||||||
return fmt.Errorf("系统公告数量不能超过100个")
|
return fmt.Errorf("系统公告数量不能超过100个")
|
||||||
}
|
}
|
||||||
validTypes := map[string]bool{
|
validTypes := map[string]bool{
|
||||||
"default": true, "ongoing": true, "success": true, "warning": true, "error": true,
|
"default": true, "ongoing": true, "success": true, "warning": true, "error": true,
|
||||||
}
|
}
|
||||||
for i, ann := range list {
|
for i, ann := range list {
|
||||||
content, ok := ann["content"].(string)
|
content, ok := ann["content"].(string)
|
||||||
if !ok || content == "" {
|
if !ok || content == "" {
|
||||||
return fmt.Errorf("第%d个公告缺少内容字段", i+1)
|
return fmt.Errorf("第%d个公告缺少内容字段", i+1)
|
||||||
}
|
}
|
||||||
publishDateAny, exists := ann["publishDate"]
|
publishDateAny, exists := ann["publishDate"]
|
||||||
if !exists {
|
if !exists {
|
||||||
return fmt.Errorf("第%d个公告缺少发布日期字段", i+1)
|
return fmt.Errorf("第%d个公告缺少发布日期字段", i+1)
|
||||||
}
|
}
|
||||||
publishDateStr, ok := publishDateAny.(string)
|
publishDateStr, ok := publishDateAny.(string)
|
||||||
if !ok || publishDateStr == "" {
|
if !ok || publishDateStr == "" {
|
||||||
return fmt.Errorf("第%d个公告的发布日期不能为空", i+1)
|
return fmt.Errorf("第%d个公告的发布日期不能为空", i+1)
|
||||||
}
|
}
|
||||||
if _, err := time.Parse(time.RFC3339, publishDateStr); err != nil {
|
if _, err := time.Parse(time.RFC3339, publishDateStr); err != nil {
|
||||||
return fmt.Errorf("第%d个公告的发布日期格式错误", i+1)
|
return fmt.Errorf("第%d个公告的发布日期格式错误", i+1)
|
||||||
}
|
}
|
||||||
if t, exists := ann["type"]; exists {
|
if t, exists := ann["type"]; exists {
|
||||||
if typeStr, ok := t.(string); ok {
|
if typeStr, ok := t.(string); ok {
|
||||||
if !validTypes[typeStr] {
|
if !validTypes[typeStr] {
|
||||||
return fmt.Errorf("第%d个公告的类型值不合法", i+1)
|
return fmt.Errorf("第%d个公告的类型值不合法", i+1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(content) > 500 {
|
if len(content) > 500 {
|
||||||
return fmt.Errorf("第%d个公告的内容长度不能超过500字符", i+1)
|
return fmt.Errorf("第%d个公告的内容长度不能超过500字符", i+1)
|
||||||
}
|
}
|
||||||
if extra, exists := ann["extra"]; exists {
|
if extra, exists := ann["extra"]; exists {
|
||||||
if extraStr, ok := extra.(string); ok && len(extraStr) > 200 {
|
if extraStr, ok := extra.(string); ok && len(extraStr) > 200 {
|
||||||
return fmt.Errorf("第%d个公告的说明长度不能超过200字符", i+1)
|
return fmt.Errorf("第%d个公告的说明长度不能超过200字符", i+1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateFAQ(faqStr string) error {
|
func validateFAQ(faqStr string) error {
|
||||||
list, err := parseJSONArray(faqStr, "FAQ信息")
|
list, err := parseJSONArray(faqStr, "FAQ信息")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(list) > 100 {
|
if len(list) > 100 {
|
||||||
return fmt.Errorf("FAQ数量不能超过100个")
|
return fmt.Errorf("FAQ数量不能超过100个")
|
||||||
}
|
}
|
||||||
for i, faq := range list {
|
for i, faq := range list {
|
||||||
question, ok := faq["question"].(string)
|
question, ok := faq["question"].(string)
|
||||||
if !ok || question == "" {
|
if !ok || question == "" {
|
||||||
return fmt.Errorf("第%d个FAQ缺少问题字段", i+1)
|
return fmt.Errorf("第%d个FAQ缺少问题字段", i+1)
|
||||||
}
|
}
|
||||||
answer, ok := faq["answer"].(string)
|
answer, ok := faq["answer"].(string)
|
||||||
if !ok || answer == "" {
|
if !ok || answer == "" {
|
||||||
return fmt.Errorf("第%d个FAQ缺少答案字段", i+1)
|
return fmt.Errorf("第%d个FAQ缺少答案字段", i+1)
|
||||||
}
|
}
|
||||||
if len(question) > 200 {
|
if len(question) > 200 {
|
||||||
return fmt.Errorf("第%d个FAQ的问题长度不能超过200字符", i+1)
|
return fmt.Errorf("第%d个FAQ的问题长度不能超过200字符", i+1)
|
||||||
}
|
}
|
||||||
if len(answer) > 1000 {
|
if len(answer) > 1000 {
|
||||||
return fmt.Errorf("第%d个FAQ的答案长度不能超过1000字符", i+1)
|
return fmt.Errorf("第%d个FAQ的答案长度不能超过1000字符", i+1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPublishTime(item map[string]interface{}) time.Time {
|
func getPublishTime(item map[string]interface{}) time.Time {
|
||||||
if v, ok := item["publishDate"]; ok {
|
if v, ok := item["publishDate"]; ok {
|
||||||
if s, ok2 := v.(string); ok2 {
|
if s, ok2 := v.(string); ok2 {
|
||||||
if t, err := time.Parse(time.RFC3339, s); err == nil {
|
if t, err := time.Parse(time.RFC3339, s); err == nil {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return time.Time{}
|
return time.Time{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAnnouncements() []map[string]interface{} {
|
func GetAnnouncements() []map[string]interface{} {
|
||||||
list := getJSONList(GetConsoleSetting().Announcements)
|
list := getJSONList(GetConsoleSetting().Announcements)
|
||||||
sort.SliceStable(list, func(i, j int) bool {
|
sort.SliceStable(list, func(i, j int) bool {
|
||||||
return getPublishTime(list[i]).After(getPublishTime(list[j]))
|
return getPublishTime(list[i]).After(getPublishTime(list[j]))
|
||||||
})
|
})
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetFAQ() []map[string]interface{} {
|
func GetFAQ() []map[string]interface{} {
|
||||||
return getJSONList(GetConsoleSetting().FAQ)
|
return getJSONList(GetConsoleSetting().FAQ)
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateUptimeKumaGroups(groupsStr string) error {
|
func validateUptimeKumaGroups(groupsStr string) error {
|
||||||
groups, err := parseJSONArray(groupsStr, "Uptime Kuma分组配置")
|
groups, err := parseJSONArray(groupsStr, "Uptime Kuma分组配置")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(groups) > 20 {
|
if len(groups) > 20 {
|
||||||
return fmt.Errorf("Uptime Kuma分组数量不能超过20个")
|
return fmt.Errorf("Uptime Kuma分组数量不能超过20个")
|
||||||
}
|
}
|
||||||
|
|
||||||
nameSet := make(map[string]bool)
|
nameSet := make(map[string]bool)
|
||||||
|
|
||||||
for i, group := range groups {
|
for i, group := range groups {
|
||||||
categoryName, ok := group["categoryName"].(string)
|
categoryName, ok := group["categoryName"].(string)
|
||||||
if !ok || categoryName == "" {
|
if !ok || categoryName == "" {
|
||||||
return fmt.Errorf("第%d个分组缺少分类名称字段", i+1)
|
return fmt.Errorf("第%d个分组缺少分类名称字段", i+1)
|
||||||
}
|
}
|
||||||
if nameSet[categoryName] {
|
if nameSet[categoryName] {
|
||||||
return fmt.Errorf("第%d个分组的分类名称与其他分组重复", i+1)
|
return fmt.Errorf("第%d个分组的分类名称与其他分组重复", i+1)
|
||||||
}
|
}
|
||||||
nameSet[categoryName] = true
|
nameSet[categoryName] = true
|
||||||
urlStr, ok := group["url"].(string)
|
urlStr, ok := group["url"].(string)
|
||||||
if !ok || urlStr == "" {
|
if !ok || urlStr == "" {
|
||||||
return fmt.Errorf("第%d个分组缺少URL字段", i+1)
|
return fmt.Errorf("第%d个分组缺少URL字段", i+1)
|
||||||
}
|
}
|
||||||
slug, ok := group["slug"].(string)
|
slug, ok := group["slug"].(string)
|
||||||
if !ok || slug == "" {
|
if !ok || slug == "" {
|
||||||
return fmt.Errorf("第%d个分组缺少Slug字段", i+1)
|
return fmt.Errorf("第%d个分组缺少Slug字段", i+1)
|
||||||
}
|
}
|
||||||
description, ok := group["description"].(string)
|
description, ok := group["description"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
description = ""
|
description = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := validateURL(urlStr, i+1, "分组"); err != nil {
|
if err := validateURL(urlStr, i+1, "分组"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(categoryName) > 50 {
|
if len(categoryName) > 50 {
|
||||||
return fmt.Errorf("第%d个分组的分类名称长度不能超过50字符", i+1)
|
return fmt.Errorf("第%d个分组的分类名称长度不能超过50字符", i+1)
|
||||||
}
|
}
|
||||||
if len(urlStr) > 500 {
|
if len(urlStr) > 500 {
|
||||||
return fmt.Errorf("第%d个分组的URL长度不能超过500字符", i+1)
|
return fmt.Errorf("第%d个分组的URL长度不能超过500字符", i+1)
|
||||||
}
|
}
|
||||||
if len(slug) > 100 {
|
if len(slug) > 100 {
|
||||||
return fmt.Errorf("第%d个分组的Slug长度不能超过100字符", i+1)
|
return fmt.Errorf("第%d个分组的Slug长度不能超过100字符", i+1)
|
||||||
}
|
}
|
||||||
if len(description) > 200 {
|
if len(description) > 200 {
|
||||||
return fmt.Errorf("第%d个分组的描述长度不能超过200字符", i+1)
|
return fmt.Errorf("第%d个分组的描述长度不能超过200字符", i+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !slugRegex.MatchString(slug) {
|
if !slugRegex.MatchString(slug) {
|
||||||
return fmt.Errorf("第%d个分组的Slug只能包含字母、数字、下划线和连字符", i+1)
|
return fmt.Errorf("第%d个分组的Slug只能包含字母、数字、下划线和连字符", i+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := checkDangerousContent(description, i+1, "分组"); err != nil {
|
if err := checkDangerousContent(description, i+1, "分组"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := checkDangerousContent(categoryName, i+1, "分组"); err != nil {
|
if err := checkDangerousContent(categoryName, i+1, "分组"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUptimeKumaGroups() []map[string]interface{} {
|
func GetUptimeKumaGroups() []map[string]interface{} {
|
||||||
return getJSONList(GetConsoleSetting().UptimeKumaGroups)
|
return getJSONList(GetConsoleSetting().UptimeKumaGroups)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,13 @@ import "sync/atomic"
|
|||||||
var exposeRatioEnabled atomic.Bool
|
var exposeRatioEnabled atomic.Bool
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
exposeRatioEnabled.Store(false)
|
exposeRatioEnabled.Store(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetExposeRatioEnabled(enabled bool) {
|
func SetExposeRatioEnabled(enabled bool) {
|
||||||
exposeRatioEnabled.Store(enabled)
|
exposeRatioEnabled.Store(enabled)
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsExposeRatioEnabled() bool {
|
func IsExposeRatioEnabled() bool {
|
||||||
return exposeRatioEnabled.Load()
|
return exposeRatioEnabled.Load()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,55 +1,55 @@
|
|||||||
package ratio_setting
|
package ratio_setting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const exposedDataTTL = 30 * time.Second
|
const exposedDataTTL = 30 * time.Second
|
||||||
|
|
||||||
type exposedCache struct {
|
type exposedCache struct {
|
||||||
data gin.H
|
data gin.H
|
||||||
expiresAt time.Time
|
expiresAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
exposedData atomic.Value
|
exposedData atomic.Value
|
||||||
rebuildMu sync.Mutex
|
rebuildMu sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func InvalidateExposedDataCache() {
|
func InvalidateExposedDataCache() {
|
||||||
exposedData.Store((*exposedCache)(nil))
|
exposedData.Store((*exposedCache)(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func cloneGinH(src gin.H) gin.H {
|
func cloneGinH(src gin.H) gin.H {
|
||||||
dst := make(gin.H, len(src))
|
dst := make(gin.H, len(src))
|
||||||
for k, v := range src {
|
for k, v := range src {
|
||||||
dst[k] = v
|
dst[k] = v
|
||||||
}
|
}
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetExposedData() gin.H {
|
func GetExposedData() gin.H {
|
||||||
if c, ok := exposedData.Load().(*exposedCache); ok && c != nil && time.Now().Before(c.expiresAt) {
|
if c, ok := exposedData.Load().(*exposedCache); ok && c != nil && time.Now().Before(c.expiresAt) {
|
||||||
return cloneGinH(c.data)
|
return cloneGinH(c.data)
|
||||||
}
|
}
|
||||||
rebuildMu.Lock()
|
rebuildMu.Lock()
|
||||||
defer rebuildMu.Unlock()
|
defer rebuildMu.Unlock()
|
||||||
if c, ok := exposedData.Load().(*exposedCache); ok && c != nil && time.Now().Before(c.expiresAt) {
|
if c, ok := exposedData.Load().(*exposedCache); ok && c != nil && time.Now().Before(c.expiresAt) {
|
||||||
return cloneGinH(c.data)
|
return cloneGinH(c.data)
|
||||||
}
|
}
|
||||||
newData := gin.H{
|
newData := gin.H{
|
||||||
"model_ratio": GetModelRatioCopy(),
|
"model_ratio": GetModelRatioCopy(),
|
||||||
"completion_ratio": GetCompletionRatioCopy(),
|
"completion_ratio": GetCompletionRatioCopy(),
|
||||||
"cache_ratio": GetCacheRatioCopy(),
|
"cache_ratio": GetCacheRatioCopy(),
|
||||||
"model_price": GetModelPriceCopy(),
|
"model_price": GetModelPriceCopy(),
|
||||||
}
|
}
|
||||||
exposedData.Store(&exposedCache{
|
exposedData.Store(&exposedCache{
|
||||||
data: newData,
|
data: newData,
|
||||||
expiresAt: time.Now().Add(exposedDataTTL),
|
expiresAt: time.Now().Add(exposedDataTTL),
|
||||||
})
|
})
|
||||||
return cloneGinH(newData)
|
return cloneGinH(newData)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user