// internal/handlers/vfs/vfs_config.go package vfs import ( "fmt" "log" "github.com/spf13/viper" ) type Config struct { Server ServerConfig `mapstructure:"server"` VFS VFSConfig `mapstructure:"vfs"` Services ServicesConfig `mapstructure:"services"` } type ServerConfig struct { Address string `mapstructure:"address"` Mode string `mapstructure:"mode"` } type VFSConfig struct { DbPath string `mapstructure:"db_path"` PolicyPath string `mapstructure:"policy_path"` AdminToken string `mapstructure:"admin_token"` RegisterToken string `mapstructure:"regiser_token"` Debug bool `mapstructure:"debug"` } type ServicesConfig struct { Bookmark BookmarkServiceConfig `mapstructure:"bookmark"` } type BookmarkServiceConfig struct { URL string `mapstructure:"url"` } func genrateRandomToken(length int) string { // 生成随机字符串 return "random_token" } // LoadConfig 加载配置 func LoadConfig() (*Config, error) { viper.SetConfigName("vfs_config") viper.SetConfigType("yaml") viper.AddConfigPath(".") viper.AddConfigPath("./config") viper.AddConfigPath("config") // 设置默认值 viper.SetDefault("server.address", "localhost:8080") viper.SetDefault("server.mode", "release") viper.SetDefault("vfs.db_path", "./data/vfs.sqlite3") viper.SetDefault("vfs.policy_path", "./data/policy.csv") viper.SetDefault("vfs.debug", false) viper.SetDefault("services.bookmark.url", "http://localhost:8081/api") viper.SetDefault("vfs.admin_token", genrateRandomToken(64)) viper.SetDefault("vfs.regiser_token", genrateRandomToken(64)) // 自动读取环境变量 viper.AutomaticEnv() // 读取配置文件 if err := viper.ReadInConfig(); err != nil { log.Printf("Warning: unable to read config file: %v. Using defaults and environment variables.", err) } var config Config if err := viper.Unmarshal(&config); err != nil { return nil, fmt.Errorf("unable to decode into struct: %v", err) } return &config, nil }