Files
new-api/setting/operation_setting/monitor_setting.go
T
CaIon 14a9acd368 feat(custom): CNY native billing, daily check-in button, subscription plans page, usage docs and promo banner
Based on QuantumNous/new-api v1.0.0-rc.36 (shallow history squashed).

- CNY quota display mode: RMB-denominated top-up and crediting across Epay/Waffo channels
- New CheckinButton in both authenticated and public headers (Turnstile supported)
- New /subscribe page with public plan snapshot, Alipay/balance payment, auto group upgrade/reset
- New /docs 12-section beginner guide with anchor navigation
- New home PromoBanner linking to /docs; i18n synced (en/zh/zh-TW/fr/ja/ru/vi)
- README: document custom features, live demo https://ai.88531.cn
2026-09-23 16:28:52 +08:00

82 lines
2.4 KiB
Go

package operation_setting
import (
"fmt"
"os"
"strconv"
"github.com/QuantumNous/new-api/setting/config"
)
type MonitorSetting struct {
AutoTestChannelEnabled bool `json:"auto_test_channel_enabled"`
AutoTestChannelMinutes float64 `json:"auto_test_channel_minutes"`
ChannelTestMode string `json:"channel_test_mode"`
ChannelTestConcurrency int `json:"channel_test_concurrency"`
}
const (
ChannelTestModeScheduledAll = "scheduled_all"
ChannelTestModeAutoBanOnly = "auto_ban_only"
ChannelTestModePassiveRecovery = "passive_recovery"
ChannelTestConcurrencyOptionKey = "monitor_setting.channel_test_concurrency"
DefaultChannelTestConcurrency = 1
MaxChannelTestConcurrency = 32
)
// 默认配置
var monitorSetting = MonitorSetting{
AutoTestChannelEnabled: false,
AutoTestChannelMinutes: 10,
ChannelTestMode: ChannelTestModeScheduledAll,
ChannelTestConcurrency: DefaultChannelTestConcurrency,
}
func init() {
// 注册到全局配置管理器
config.GlobalConfig.Register("monitor_setting", &monitorSetting)
}
func GetMonitorSetting() *MonitorSetting {
if os.Getenv("CHANNEL_TEST_FREQUENCY") != "" {
frequency, err := strconv.Atoi(os.Getenv("CHANNEL_TEST_FREQUENCY"))
if err == nil && frequency > 0 {
monitorSetting.AutoTestChannelEnabled = true
monitorSetting.AutoTestChannelMinutes = float64(frequency)
monitorSetting.ChannelTestMode = ChannelTestModeScheduledAll
}
}
if enabled, ok := os.LookupEnv("CHANNEL_TEST_ENABLED"); ok {
parsed, err := strconv.ParseBool(enabled)
if err == nil {
monitorSetting.AutoTestChannelEnabled = parsed
}
}
switch monitorSetting.ChannelTestMode {
case ChannelTestModeAutoBanOnly, ChannelTestModePassiveRecovery:
default:
monitorSetting.ChannelTestMode = ChannelTestModeScheduledAll
}
monitorSetting.ChannelTestConcurrency = NormalizeChannelTestConcurrency(monitorSetting.ChannelTestConcurrency)
return &monitorSetting
}
func NormalizeChannelTestConcurrency(concurrency int) int {
if concurrency < 1 {
return DefaultChannelTestConcurrency
}
if concurrency > MaxChannelTestConcurrency {
return MaxChannelTestConcurrency
}
return concurrency
}
func ValidateChannelTestConcurrency(value string) error {
concurrency, err := strconv.Atoi(value)
if err != nil || concurrency < 1 || concurrency > MaxChannelTestConcurrency {
return fmt.Errorf("channel test concurrency must be between 1 and %d", MaxChannelTestConcurrency)
}
return nil
}