feat: add settings schema
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"cmbone/internal/models"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type AppConfigService struct {
|
||||
store *SuiStore
|
||||
}
|
||||
@@ -22,18 +29,36 @@ func (r *AppConfigService) GetAppConfig(key string) (string, error) {
|
||||
var value string
|
||||
err := row.Scan(&value)
|
||||
if err != nil {
|
||||
return "en", err
|
||||
if err == sql.ErrNoRows {
|
||||
return "", nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// 设置或更新配置项
|
||||
func (r *AppConfigService) SetAppConfig(key, value string) error {
|
||||
configType := "user"
|
||||
description := ""
|
||||
if definition, ok := settingDefinitionByKey(key); ok {
|
||||
if definition.ReadOnly {
|
||||
return fmt.Errorf("setting %q is read only", key)
|
||||
}
|
||||
if err := validateSettingValue(definition, value); err != nil {
|
||||
return err
|
||||
}
|
||||
configType = definition.Type
|
||||
description = definition.Description
|
||||
}
|
||||
_, err := r.store.DB.Exec(`
|
||||
INSERT INTO appconfig (key, type, value, description)
|
||||
VALUES (?, 'user', ?, '')
|
||||
ON CONFLICT(key) DO UPDATE SET value=excluded.value
|
||||
`, key, value)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET
|
||||
type=excluded.type,
|
||||
value=excluded.value,
|
||||
description=excluded.description
|
||||
`, key, configType, value, description)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -50,3 +75,76 @@ func (s *AppConfigService) GetLanguage() string {
|
||||
func (s *AppConfigService) SetLanguage(lang string) error {
|
||||
return s.SetAppConfig("language", lang)
|
||||
}
|
||||
|
||||
func (s *AppConfigService) GetSettingsSchema() ([]models.AppSettingGroup, error) {
|
||||
values, err := s.getAppConfigValues()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
definitionsByCategory := make(map[string][]models.AppSettingDefinition)
|
||||
for _, definition := range settingsDefinitions() {
|
||||
value := values[definition.Key]
|
||||
if value == "" {
|
||||
value = definition.DefaultValue
|
||||
}
|
||||
definition.Value = value
|
||||
if definition.Options == nil {
|
||||
definition.Options = []models.AppSettingOption{}
|
||||
}
|
||||
definitionsByCategory[definition.Category] = append(definitionsByCategory[definition.Category], definition)
|
||||
}
|
||||
|
||||
groups := make([]models.AppSettingGroup, 0, len(settingGroups))
|
||||
for _, group := range settingGroups {
|
||||
group.Items = definitionsByCategory[group.Category]
|
||||
if len(group.Items) > 0 {
|
||||
groups = append(groups, group)
|
||||
}
|
||||
}
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func (s *AppConfigService) getAppConfigValues() (map[string]string, error) {
|
||||
rows, err := s.store.DB.Query("SELECT key, value FROM appconfig")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
values := make(map[string]string)
|
||||
for rows.Next() {
|
||||
var key string
|
||||
var value string
|
||||
if err := rows.Scan(&key, &value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values[key] = value
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func validateSettingValue(definition models.AppSettingDefinition, value string) error {
|
||||
switch definition.Type {
|
||||
case settingTypeSelect:
|
||||
for _, option := range definition.Options {
|
||||
if option.Value == value {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("setting %q value %q is not allowed", definition.Key, value)
|
||||
case settingTypeBoolean:
|
||||
if value == "true" || value == "false" {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("setting %q expects boolean string", definition.Key)
|
||||
case settingTypeNumber:
|
||||
if _, err := strconv.Atoi(value); err != nil {
|
||||
return fmt.Errorf("setting %q expects number string: %w", definition.Key, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,226 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"cmbone/internal/models"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
settingTypeSelect = "select"
|
||||
settingTypeBoolean = "boolean"
|
||||
settingTypeNumber = "number"
|
||||
settingTypeText = "text"
|
||||
settingTypePath = "path"
|
||||
settingTypeHotkey = "hotkey"
|
||||
)
|
||||
|
||||
var settingGroups = []models.AppSettingGroup{
|
||||
{Category: "appearance", Label: "外观与语言", Description: "主题和界面语言设置"},
|
||||
{Category: "window", Label: "窗口行为", Description: "主窗口默认尺寸、启动状态和恢复策略"},
|
||||
{Category: "shortcuts", Label: "快捷键", Description: "全局快捷键默认值和入口说明"},
|
||||
{Category: "data", Label: "数据目录", Description: "本地数据库、输出文件和缓存目录策略"},
|
||||
{Category: "logs", Label: "日志策略", Description: "审计日志和运行日志的级别与保留周期"},
|
||||
{Category: "auth", Label: "登录", Description: "登录 provider 和本地演示账号配置"},
|
||||
}
|
||||
|
||||
func settingsDefinitions() []models.AppSettingDefinition {
|
||||
return []models.AppSettingDefinition{
|
||||
{
|
||||
Key: "theme.mode",
|
||||
Category: "appearance",
|
||||
Label: "主题模式",
|
||||
Type: settingTypeSelect,
|
||||
DefaultValue: "light",
|
||||
Description: "主窗口和组件库主题,systemdefault 表示跟随系统。",
|
||||
Options: []models.AppSettingOption{
|
||||
{Value: "light", Label: "亮色模式"},
|
||||
{Value: "dark", Label: "暗色模式"},
|
||||
{Value: "systemdefault", Label: "跟随系统"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Key: "language",
|
||||
Category: "appearance",
|
||||
Label: "界面语言",
|
||||
Type: settingTypeSelect,
|
||||
DefaultValue: "zh",
|
||||
Description: "应用界面语言,同时影响托盘菜单语言。",
|
||||
Options: []models.AppSettingOption{
|
||||
{Value: "zh", Label: "简体中文"},
|
||||
{Value: "en", Label: "English"},
|
||||
{Value: "zh-HK", Label: "繁體中文"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Key: "window.start_state",
|
||||
Category: "window",
|
||||
Label: "启动窗口状态",
|
||||
Type: settingTypeSelect,
|
||||
DefaultValue: "normal",
|
||||
Description: "主窗口启动时默认状态;尺寸恢复由 T-015 继续落地。",
|
||||
Options: []models.AppSettingOption{
|
||||
{Value: "normal", Label: "普通窗口"},
|
||||
{Value: "maximized", Label: "最大化"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Key: "window.width",
|
||||
Category: "window",
|
||||
Label: "默认宽度",
|
||||
Type: settingTypeNumber,
|
||||
DefaultValue: "1280",
|
||||
Description: "主窗口默认宽度,当前窗口创建逻辑使用固定默认值。",
|
||||
},
|
||||
{
|
||||
Key: "window.height",
|
||||
Category: "window",
|
||||
Label: "默认高度",
|
||||
Type: settingTypeNumber,
|
||||
DefaultValue: "800",
|
||||
Description: "主窗口默认高度,当前窗口创建逻辑使用固定默认值。",
|
||||
},
|
||||
{
|
||||
Key: "window.remember_state",
|
||||
Category: "window",
|
||||
Label: "记住窗口状态",
|
||||
Type: settingTypeBoolean,
|
||||
DefaultValue: "false",
|
||||
Description: "保存主窗口尺寸和最大化状态,T-015 使用该配置。",
|
||||
},
|
||||
{
|
||||
Key: "shortcut.open_second_window",
|
||||
Category: "shortcuts",
|
||||
Label: "打开第二窗口",
|
||||
Type: settingTypeHotkey,
|
||||
DefaultValue: "Alt+P",
|
||||
Description: "对应 hotkeys 表 target=1,实际注册由 HotkeyService 管理。",
|
||||
ReadOnly: true,
|
||||
},
|
||||
{
|
||||
Key: "shortcut.open_main_window",
|
||||
Category: "shortcuts",
|
||||
Label: "打开主窗口",
|
||||
Type: settingTypeHotkey,
|
||||
DefaultValue: "Alt+M",
|
||||
Description: "对应 hotkeys 表 target=2,实际注册由 HotkeyService 管理。",
|
||||
ReadOnly: true,
|
||||
},
|
||||
{
|
||||
Key: "data.root_mode",
|
||||
Category: "data",
|
||||
Label: "数据目录策略",
|
||||
Type: settingTypeSelect,
|
||||
DefaultValue: "app_config_dir",
|
||||
Description: "默认使用系统用户配置目录下的 cmbone 目录。",
|
||||
Options: []models.AppSettingOption{
|
||||
{Value: "app_config_dir", Label: "系统配置目录"},
|
||||
{Value: "custom", Label: "自定义目录"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Key: "data.custom_directory",
|
||||
Category: "data",
|
||||
Label: "自定义数据目录",
|
||||
Type: settingTypePath,
|
||||
DefaultValue: "",
|
||||
Description: "当数据目录策略为 custom 时使用;为空表示未配置。",
|
||||
},
|
||||
{
|
||||
Key: "logs.level",
|
||||
Category: "logs",
|
||||
Label: "运行日志级别",
|
||||
Type: settingTypeSelect,
|
||||
DefaultValue: "info",
|
||||
Description: "运行日志默认记录级别。",
|
||||
Options: []models.AppSettingOption{
|
||||
{Value: "debug", Label: "Debug"},
|
||||
{Value: "info", Label: "Info"},
|
||||
{Value: "warn", Label: "Warn"},
|
||||
{Value: "error", Label: "Error"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Key: "logs.audit_retention_days",
|
||||
Category: "logs",
|
||||
Label: "审计日志保留天数",
|
||||
Type: settingTypeNumber,
|
||||
DefaultValue: "180",
|
||||
Description: "审计日志默认保留周期。",
|
||||
},
|
||||
{
|
||||
Key: "logs.app_retention_days",
|
||||
Category: "logs",
|
||||
Label: "运行日志保留天数",
|
||||
Type: settingTypeNumber,
|
||||
DefaultValue: "30",
|
||||
Description: "运行日志默认保留周期。",
|
||||
},
|
||||
{
|
||||
Key: "auth.provider",
|
||||
Category: "auth",
|
||||
Label: "登录 provider",
|
||||
Type: settingTypeSelect,
|
||||
DefaultValue: "local",
|
||||
Description: "local 用于模板演示,remote 是真实后端预留入口。",
|
||||
Options: []models.AppSettingOption{
|
||||
{Value: "local", Label: "Local"},
|
||||
{Value: "remote", Label: "Remote"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Key: "auth.local.username",
|
||||
Category: "auth",
|
||||
Label: "本地演示账号",
|
||||
Type: settingTypeText,
|
||||
DefaultValue: "admin",
|
||||
Description: "本地固定账号 provider 的演示用户名。",
|
||||
ReadOnly: true,
|
||||
},
|
||||
{
|
||||
Key: "auth.local.password_salt",
|
||||
Category: "auth",
|
||||
Label: "本地密码盐",
|
||||
Type: settingTypeText,
|
||||
DefaultValue: "cmbone-local-auth-v1",
|
||||
Description: "本地演示账号密码盐。",
|
||||
ReadOnly: true,
|
||||
},
|
||||
{
|
||||
Key: "auth.local.password_hash",
|
||||
Category: "auth",
|
||||
Label: "本地密码哈希",
|
||||
Type: settingTypeText,
|
||||
DefaultValue: "3e0bfcda604b19fef1f596bcade7469ff1ab8b427fdc998e9a8216101b002f4a",
|
||||
Description: "本地演示账号密码哈希。",
|
||||
ReadOnly: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func settingDefinitionByKey(key string) (models.AppSettingDefinition, bool) {
|
||||
for _, definition := range settingsDefinitions() {
|
||||
if definition.Key == key {
|
||||
return definition, true
|
||||
}
|
||||
}
|
||||
return models.AppSettingDefinition{}, false
|
||||
}
|
||||
|
||||
func defaultAppConfigSQL() string {
|
||||
var builder strings.Builder
|
||||
for _, definition := range settingsDefinitions() {
|
||||
builder.WriteString(fmt.Sprintf(
|
||||
"INSERT OR IGNORE INTO appconfig (key, type, value, description) VALUES (%s, %s, %s, %s);\n",
|
||||
sqlString(definition.Key),
|
||||
sqlString(definition.Type),
|
||||
sqlString(definition.DefaultValue),
|
||||
sqlString(definition.Description),
|
||||
))
|
||||
}
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func sqlString(value string) string {
|
||||
return "'" + strings.ReplaceAll(value, "'", "''") + "'"
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"cmbone/internal/models"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"testing"
|
||||
@@ -64,6 +65,91 @@ func TestAppConfigServiceLanguage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppConfigServiceSettingsSchema(t *testing.T) {
|
||||
store := newTestStore(t)
|
||||
service := NewAppConfigService(store)
|
||||
|
||||
groups, err := service.GetSettingsSchema()
|
||||
if err != nil {
|
||||
t.Fatalf("get settings schema: %v", err)
|
||||
}
|
||||
|
||||
requiredKeys := map[string]string{
|
||||
"theme.mode": "light",
|
||||
"language": "zh",
|
||||
"window.start_state": "normal",
|
||||
"window.width": "1280",
|
||||
"window.height": "800",
|
||||
"window.remember_state": "false",
|
||||
"shortcut.open_second_window": "Alt+P",
|
||||
"shortcut.open_main_window": "Alt+M",
|
||||
"data.root_mode": "app_config_dir",
|
||||
"data.custom_directory": "",
|
||||
"logs.level": "info",
|
||||
"logs.audit_retention_days": "180",
|
||||
"logs.app_retention_days": "30",
|
||||
}
|
||||
|
||||
got := make(map[string]models.AppSettingDefinition)
|
||||
for _, group := range groups {
|
||||
for _, item := range group.Items {
|
||||
got[item.Key] = item
|
||||
}
|
||||
}
|
||||
|
||||
for key, wantDefault := range requiredKeys {
|
||||
item, ok := got[key]
|
||||
if !ok {
|
||||
t.Fatalf("setting %q missing from schema", key)
|
||||
}
|
||||
if item.DefaultValue != wantDefault {
|
||||
t.Fatalf("setting %q default = %q, want %q", key, item.DefaultValue, wantDefault)
|
||||
}
|
||||
if item.Value != wantDefault {
|
||||
t.Fatalf("setting %q value = %q, want %q", key, item.Value, wantDefault)
|
||||
}
|
||||
if item.Options == nil {
|
||||
t.Fatalf("setting %q options is nil, want empty slice", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppConfigServiceValidatesSchemaValues(t *testing.T) {
|
||||
store := newTestStore(t)
|
||||
service := NewAppConfigService(store)
|
||||
|
||||
if err := service.SetAppConfig("theme.mode", "dark"); err != nil {
|
||||
t.Fatalf("set valid theme: %v", err)
|
||||
}
|
||||
gotTheme, err := service.GetAppConfig("theme.mode")
|
||||
if err != nil {
|
||||
t.Fatalf("get theme: %v", err)
|
||||
}
|
||||
if gotTheme != "dark" {
|
||||
t.Fatalf("theme.mode = %q, want dark", gotTheme)
|
||||
}
|
||||
var gotType string
|
||||
if err := store.DB.QueryRow("SELECT type FROM appconfig WHERE key = 'theme.mode'").Scan(&gotType); err != nil {
|
||||
t.Fatalf("get theme type: %v", err)
|
||||
}
|
||||
if gotType != settingTypeSelect {
|
||||
t.Fatalf("theme.mode type = %q, want %q", gotType, settingTypeSelect)
|
||||
}
|
||||
|
||||
if err := service.SetAppConfig("theme.mode", "purple"); err == nil {
|
||||
t.Fatal("set invalid theme returned nil error")
|
||||
}
|
||||
if err := service.SetAppConfig("window.remember_state", "yes"); err == nil {
|
||||
t.Fatal("set invalid boolean returned nil error")
|
||||
}
|
||||
if err := service.SetAppConfig("window.width", "wide"); err == nil {
|
||||
t.Fatal("set invalid number returned nil error")
|
||||
}
|
||||
if err := service.SetAppConfig("shortcut.open_second_window", "Alt+O"); err == nil {
|
||||
t.Fatal("set readonly shortcut returned nil error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthServiceLocalLoginLifecycle(t *testing.T) {
|
||||
store := newTestStore(t)
|
||||
service := NewAuthService(store)
|
||||
|
||||
@@ -36,21 +36,3 @@ func defaultHotkeysSQL() string {
|
||||
}
|
||||
return sqlhotkeys
|
||||
}
|
||||
|
||||
func defaultAppConfigSQL() string {
|
||||
return `
|
||||
INSERT OR IGNORE INTO appconfig (key, type, value, description)
|
||||
VALUES ('language', 'system', 'zh', '应用语言,支持 zh/en');
|
||||
|
||||
INSERT OR IGNORE INTO appconfig (key, type, value, description)
|
||||
VALUES ('auth.provider', 'system', 'local', '登录 provider,支持 local/remote');
|
||||
|
||||
INSERT OR IGNORE INTO appconfig (key, type, value, description)
|
||||
VALUES ('auth.local.username', 'system', 'admin', '本地演示账号用户名');
|
||||
|
||||
INSERT OR IGNORE INTO appconfig (key, type, value, description)
|
||||
VALUES ('auth.local.password_salt', 'system', 'cmbone-local-auth-v1', '本地演示账号密码盐');
|
||||
|
||||
INSERT OR IGNORE INTO appconfig (key, type, value, description)
|
||||
VALUES ('auth.local.password_hash', 'system', '3e0bfcda604b19fef1f596bcade7469ff1ab8b427fdc998e9a8216101b002f4a', '本地演示账号密码哈希');`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user