feat: publish v1.0.0-rc.36-custom.1
Publish Docker image (Multi-arch) / Build & push (amd64) (push) Failing after 9m20s
Release (Linux, macOS, Windows) / Linux Release (push) Failing after 55m7s
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Canceled after 0s
Release (Linux, macOS, Windows) / macOS Release (push) Canceled after 0s
Release (Linux, macOS, Windows) / Windows Release (push) Canceled after 0s
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Canceled after 0s
Publish Docker image (Multi-arch) / Build & push (amd64) (push) Failing after 9m20s
Release (Linux, macOS, Windows) / Linux Release (push) Failing after 55m7s
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Canceled after 0s
Release (Linux, macOS, Windows) / macOS Release (push) Canceled after 0s
Release (Linux, macOS, Windows) / Windows Release (push) Canceled after 0s
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Canceled after 0s
This commit is contained in:
commit
bb7968b612
2381 files changed
+551494
No files matched your search
@@ -0,0 +1,305 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
)
|
||||
|
||||
// ConfigManager 统一管理所有配置
|
||||
type ConfigManager struct {
|
||||
configs map[string]any
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
var GlobalConfig = NewConfigManager()
|
||||
|
||||
func NewConfigManager() *ConfigManager {
|
||||
return &ConfigManager{
|
||||
configs: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
// Register 注册一个配置模块
|
||||
func (cm *ConfigManager) Register(name string, config any) {
|
||||
cm.mutex.Lock()
|
||||
defer cm.mutex.Unlock()
|
||||
cm.configs[name] = config
|
||||
}
|
||||
|
||||
// Get 获取指定配置模块
|
||||
func (cm *ConfigManager) Get(name string) any {
|
||||
cm.mutex.RLock()
|
||||
defer cm.mutex.RUnlock()
|
||||
return cm.configs[name]
|
||||
}
|
||||
|
||||
// LoadFromDB 从数据库加载配置
|
||||
func (cm *ConfigManager) LoadFromDB(options map[string]string) error {
|
||||
cm.mutex.Lock()
|
||||
defer cm.mutex.Unlock()
|
||||
|
||||
for name, config := range cm.configs {
|
||||
prefix := name + "."
|
||||
configMap := make(map[string]string)
|
||||
|
||||
// 收集属于此配置的所有选项
|
||||
for key, value := range options {
|
||||
if after, ok := strings.CutPrefix(key, prefix); ok {
|
||||
configKey := after
|
||||
configMap[configKey] = value
|
||||
}
|
||||
}
|
||||
|
||||
// 如果找到配置项,则更新配置
|
||||
if len(configMap) > 0 {
|
||||
if err := updateConfigFromMap(config, configMap); err != nil {
|
||||
common.SysError("failed to update config " + name + ": " + err.Error())
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveToDB 将配置保存到数据库
|
||||
func (cm *ConfigManager) SaveToDB(updateFunc func(key, value string) error) error {
|
||||
cm.mutex.RLock()
|
||||
defer cm.mutex.RUnlock()
|
||||
|
||||
for name, config := range cm.configs {
|
||||
configMap, err := configToMap(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for key, value := range configMap {
|
||||
dbKey := name + "." + key
|
||||
if err := updateFunc(dbKey, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 辅助函数:将配置对象转换为map
|
||||
func configToMap(config any) (map[string]string, error) {
|
||||
result := make(map[string]string)
|
||||
|
||||
val := reflect.ValueOf(config)
|
||||
if val.Kind() == reflect.Pointer {
|
||||
val = val.Elem()
|
||||
}
|
||||
|
||||
if val.Kind() != reflect.Struct {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
typ := val.Type()
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
field := val.Field(i)
|
||||
fieldType := typ.Field(i)
|
||||
|
||||
// 跳过未导出字段
|
||||
if !fieldType.IsExported() {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取json标签作为键名
|
||||
key := fieldType.Tag.Get("json")
|
||||
if key == "" || key == "-" {
|
||||
key = fieldType.Name
|
||||
}
|
||||
|
||||
// 处理不同类型的字段
|
||||
var strValue string
|
||||
switch field.Kind() {
|
||||
case reflect.String:
|
||||
strValue = field.String()
|
||||
case reflect.Bool:
|
||||
strValue = strconv.FormatBool(field.Bool())
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
strValue = strconv.FormatInt(field.Int(), 10)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
strValue = strconv.FormatUint(field.Uint(), 10)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
strValue = strconv.FormatFloat(field.Float(), 'f', -1, 64)
|
||||
case reflect.Pointer:
|
||||
// 处理指针类型:如果非 nil,序列化指向的值
|
||||
if !field.IsNil() {
|
||||
bytes, err := json.Marshal(field.Interface())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
strValue = string(bytes)
|
||||
} else {
|
||||
// nil 指针序列化为 "null"
|
||||
strValue = "null"
|
||||
}
|
||||
case reflect.Map, reflect.Slice, reflect.Struct:
|
||||
// 复杂类型使用JSON序列化
|
||||
bytes, err := json.Marshal(field.Interface())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
strValue = string(bytes)
|
||||
default:
|
||||
// 跳过不支持的类型
|
||||
continue
|
||||
}
|
||||
|
||||
result[key] = strValue
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 辅助函数:从map更新配置对象
|
||||
func updateConfigFromMap(config any, configMap map[string]string) error {
|
||||
val := reflect.ValueOf(config)
|
||||
if val.Kind() != reflect.Pointer {
|
||||
return nil
|
||||
}
|
||||
val = val.Elem()
|
||||
|
||||
if val.Kind() != reflect.Struct {
|
||||
return nil
|
||||
}
|
||||
|
||||
typ := val.Type()
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
field := val.Field(i)
|
||||
fieldType := typ.Field(i)
|
||||
|
||||
// 跳过未导出字段
|
||||
if !fieldType.IsExported() {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取json标签作为键名
|
||||
key := fieldType.Tag.Get("json")
|
||||
if key == "" || key == "-" {
|
||||
key = fieldType.Name
|
||||
}
|
||||
|
||||
// 检查map中是否有对应的值
|
||||
strValue, ok := configMap[key]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// 根据字段类型设置值
|
||||
if !field.CanSet() {
|
||||
continue
|
||||
}
|
||||
|
||||
switch field.Kind() {
|
||||
case reflect.String:
|
||||
field.SetString(strValue)
|
||||
case reflect.Bool:
|
||||
boolValue, err := strconv.ParseBool(strValue)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
field.SetBool(boolValue)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
intValue, err := strconv.ParseInt(strValue, 10, 64)
|
||||
if err != nil {
|
||||
// 兼容 float 格式的字符串(如 "2.000000")
|
||||
floatValue, fErr := strconv.ParseFloat(strValue, 64)
|
||||
if fErr != nil {
|
||||
continue
|
||||
}
|
||||
intValue = int64(floatValue)
|
||||
}
|
||||
field.SetInt(intValue)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
uintValue, err := strconv.ParseUint(strValue, 10, 64)
|
||||
if err != nil {
|
||||
// 兼容 float 格式的字符串
|
||||
floatValue, fErr := strconv.ParseFloat(strValue, 64)
|
||||
if fErr != nil || floatValue < 0 {
|
||||
continue
|
||||
}
|
||||
uintValue = uint64(floatValue)
|
||||
}
|
||||
field.SetUint(uintValue)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
floatValue, err := strconv.ParseFloat(strValue, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
field.SetFloat(floatValue)
|
||||
case reflect.Pointer:
|
||||
// 处理指针类型
|
||||
if strValue == "null" {
|
||||
field.Set(reflect.Zero(field.Type()))
|
||||
} else {
|
||||
// 如果指针是 nil,需要先初始化
|
||||
if field.IsNil() {
|
||||
field.Set(reflect.New(field.Type().Elem()))
|
||||
}
|
||||
// 反序列化到指针指向的值
|
||||
err := json.Unmarshal([]byte(strValue), field.Interface())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
case reflect.Map:
|
||||
// json.Unmarshal merges into existing maps (keeps old keys that are
|
||||
// absent from the new JSON). Allocate a fresh map so removed keys
|
||||
// are properly cleared.
|
||||
fresh := reflect.New(field.Type())
|
||||
if err := json.Unmarshal([]byte(strValue), fresh.Interface()); err != nil {
|
||||
continue
|
||||
}
|
||||
field.Set(fresh.Elem())
|
||||
case reflect.Slice, reflect.Struct:
|
||||
err := json.Unmarshal([]byte(strValue), field.Addr().Interface())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigToMap 将配置对象转换为map(导出函数)
|
||||
func ConfigToMap(config any) (map[string]string, error) {
|
||||
return configToMap(config)
|
||||
}
|
||||
|
||||
// UpdateConfigFromMap 从map更新配置对象(导出函数)
|
||||
func UpdateConfigFromMap(config any, configMap map[string]string) error {
|
||||
return updateConfigFromMap(config, configMap)
|
||||
}
|
||||
|
||||
// ExportAllConfigs 导出所有已注册的配置为扁平结构
|
||||
func (cm *ConfigManager) ExportAllConfigs() map[string]string {
|
||||
cm.mutex.RLock()
|
||||
defer cm.mutex.RUnlock()
|
||||
|
||||
result := make(map[string]string)
|
||||
|
||||
for name, cfg := range cm.configs {
|
||||
configMap, err := ConfigToMap(cfg)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 使用 "模块名.配置项" 的格式添加到结果中
|
||||
for key, value := range configMap {
|
||||
result[name+"."+key] = value
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testConfigWithMap struct {
|
||||
Modes map[string]string `json:"modes"`
|
||||
Exprs map[string]string `json:"exprs"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func TestUpdateConfigFromMap_MapReplacement(t *testing.T) {
|
||||
cfg := &testConfigWithMap{
|
||||
Modes: map[string]string{
|
||||
"model-a": "tiered_expr",
|
||||
"model-b": "tiered_expr",
|
||||
},
|
||||
Exprs: map[string]string{
|
||||
"model-a": "p * 5 + c * 25",
|
||||
"model-b": "p * 10 + c * 50",
|
||||
},
|
||||
Name: "billing",
|
||||
}
|
||||
|
||||
// Simulate removing model-a: new value only has model-b
|
||||
err := UpdateConfigFromMap(cfg, map[string]string{
|
||||
"modes": `{"model-b": "tiered_expr"}`,
|
||||
"exprs": `{"model-b": "p * 10 + c * 50"}`,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("UpdateConfigFromMap failed: %v", err)
|
||||
}
|
||||
|
||||
if _, ok := cfg.Modes["model-a"]; ok {
|
||||
t.Errorf("Modes still contains model-a after it was removed from the update; got %v", cfg.Modes)
|
||||
}
|
||||
if _, ok := cfg.Exprs["model-a"]; ok {
|
||||
t.Errorf("Exprs still contains model-a after it was removed from the update; got %v", cfg.Exprs)
|
||||
}
|
||||
|
||||
if cfg.Modes["model-b"] != "tiered_expr" {
|
||||
t.Errorf("Modes[model-b] = %q, want %q", cfg.Modes["model-b"], "tiered_expr")
|
||||
}
|
||||
if cfg.Exprs["model-b"] != "p * 10 + c * 50" {
|
||||
t.Errorf("Exprs[model-b] = %q, want %q", cfg.Exprs["model-b"], "p * 10 + c * 50")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateConfigFromMap_EmptyMapClearsAll(t *testing.T) {
|
||||
cfg := &testConfigWithMap{
|
||||
Modes: map[string]string{
|
||||
"model-a": "tiered_expr",
|
||||
},
|
||||
Exprs: map[string]string{
|
||||
"model-a": "p * 5 + c * 25",
|
||||
},
|
||||
}
|
||||
|
||||
err := UpdateConfigFromMap(cfg, map[string]string{
|
||||
"modes": `{}`,
|
||||
"exprs": `{}`,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("UpdateConfigFromMap failed: %v", err)
|
||||
}
|
||||
|
||||
if len(cfg.Modes) != 0 {
|
||||
t.Errorf("Modes should be empty after updating with {}, got %v", cfg.Modes)
|
||||
}
|
||||
if len(cfg.Exprs) != 0 {
|
||||
t.Errorf("Exprs should be empty after updating with {}, got %v", cfg.Exprs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateConfigFromMap_ScalarFieldsUnchanged(t *testing.T) {
|
||||
cfg := &testConfigWithMap{
|
||||
Modes: map[string]string{"m": "v"},
|
||||
Name: "old",
|
||||
}
|
||||
|
||||
err := UpdateConfigFromMap(cfg, map[string]string{
|
||||
"name": "new",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("UpdateConfigFromMap failed: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Name != "new" {
|
||||
t.Errorf("Name = %q, want %q", cfg.Name, "new")
|
||||
}
|
||||
// modes was not in configMap, should remain unchanged
|
||||
if cfg.Modes["m"] != "v" {
|
||||
t.Errorf("Modes should be unchanged, got %v", cfg.Modes)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user