将 bookmark.go 重命名为 main.go,并调整包引用路径。将 bookmarks 和 user_np 两个模块的处理逻辑合并到同一个服务中,统一注册路由。同时更新了相关 API 的引用路径,确保生成代码与内部实现正确绑定。 此外,移除了独立的 user_np 服务入口文件,其功能已整合至 bookmark 服务中。 配置文件中调整了 user_np 和 vfs 服务的端口及部分接口定义,完善了用户 相关操作的路径参数和请求体结构。
76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
// 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
|
|
}
|