Private
Public Access
1
0
Files
u-desk/internal/agent/config/config.go
绝尘 3d5a1e5892 优化:工具栏高度对齐+面板统一+远程连接架构+自动恢复预览
- 工具栏:面包屑与右侧组件像素级等高(:deep 34px)、合并重复search handler、统一分隔符样式、删除死代码
- 面板对齐:三面板header统一padding/font-size、文件列表分页固定底部(自定义紧凑)、表头默认隐藏、滚动条统一样式
- 预览区:始终显示空白预览面板、重启自动恢复上次打开文件
- 收藏夹:简化计数显示(共N项)
- 远程连接:ConnectionIndicator自适应UI(无远程显示mini云图标)、ConnectionDialog支持编辑配置、transport抽象层(本地Wails/远程HTTP双模式)、agent后端模块
2026-04-30 22:25:27 +08:00

109 lines
2.2 KiB
Go

package config
import (
"fmt"
"os"
"strings"
"gopkg.in/yaml.v3"
)
type Config struct {
Server ServerConfig `yaml:"server"`
Auth AuthConfig `yaml:"auth"`
CORS CORSConfig `yaml:"cors"`
Log LogConfig `yaml:"log"`
FileServer FileServerConfig `yaml:"file_server"`
Security SecurityConfig `yaml:"security"`
}
type ServerConfig struct {
Port int `yaml:"port"`
Host string `yaml:"host"`
}
type AuthConfig struct {
Token string `yaml:"token"`
}
type CORSConfig struct {
AllowedOrigins []string `yaml:"allowed_origins"`
}
type LogConfig struct {
Level string `yaml:"level"`
Format string `yaml:"format"`
}
type FileServerConfig struct {
Port int `yaml:"port"`
MaxFileSize int64 `yaml:"max_file_size"`
}
type SecurityConfig struct {
AllowSymlinks bool `yaml:"allow_symlinks"`
CheckSystemPaths bool `yaml:"check_system_paths"`
}
// FileServerAddr 返回文件服务器的完整地址
func (c *Config) FileServerAddr() string {
return fmt.Sprintf("http://localhost:%d", c.FileServer.Port)
}
func Load(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
// 配置文件不存在时使用默认值
if os.IsNotExist(err) {
return Default(), nil
}
return nil, err
}
cfg := Default()
if err := yaml.Unmarshal(data, cfg); err != nil {
return nil, err
}
// 清理 origins 中的空格并去重
seen := make(map[string]bool, len(cfg.CORS.AllowedOrigins))
uniques := cfg.CORS.AllowedOrigins[:0]
for _, origin := range cfg.CORS.AllowedOrigins {
o := strings.TrimSpace(origin)
if o != "" && !seen[o] {
seen[o] = true
uniques = append(uniques, o)
}
}
cfg.CORS.AllowedOrigins = uniques
return cfg, nil
}
func Default() *Config {
return &Config{
Server: ServerConfig{
Port: 9876,
Host: "0.0.0.0",
},
Auth: AuthConfig{
Token: "",
},
CORS: CORSConfig{
AllowedOrigins: []string{"*"},
},
Log: LogConfig{
Level: "info",
Format: "json",
},
FileServer: FileServerConfig{
Port: 8073,
MaxFileSize: 500 * 1024 * 1024,
},
Security: SecurityConfig{
AllowSymlinks: false,
CheckSystemPaths: true,
},
}
}