- 新增 backend/internal/router/mcp:MCP 端点 /api/v1/mcp,Bearer/X-MCP-Token/?token 鉴权 - 只读工具:list_apps / list_versions / get_version / simulate_check_update / list_cards / app_stats / list_devices / list_logs - 写工具(mcp.allow_write=true):publish_version / deprecate_version,默认 dry_run, 写操作落 logs 审计 - simulate_check_update 复刻 check-update 判定规则,可预判“同版本号重传不生效” - 导出 app.BuildManifest 复用清单计算口径,避免逻辑漂移 - 构建环境 go 1.21 -> 1.23(mcp-go 要求),Dockerfile 同步 - 文档 docs/mcp.md
96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
package config
|
||
|
||
import (
|
||
"log"
|
||
"os"
|
||
|
||
"github.com/spf13/viper"
|
||
)
|
||
|
||
var dataDir = "/app/data"
|
||
|
||
func Init() {
|
||
if dir := os.Getenv("DATA_DIR"); dir != "" {
|
||
dataDir = dir
|
||
}
|
||
if _, err := os.Stat(dataDir); os.IsNotExist(err) {
|
||
os.MkdirAll(dataDir, 0755)
|
||
}
|
||
|
||
viper.SetConfigName("config")
|
||
viper.SetConfigType("yaml")
|
||
viper.AddConfigPath(dataDir)
|
||
viper.AddConfigPath(".")
|
||
viper.AddConfigPath("./config")
|
||
|
||
setDefaults()
|
||
|
||
if err := viper.ReadInConfig(); err != nil {
|
||
log.Printf("Warning: Config file not found, using defaults: %v", err)
|
||
}
|
||
|
||
viper.AutomaticEnv()
|
||
}
|
||
|
||
func GetDataDir() string {
|
||
return dataDir
|
||
}
|
||
|
||
// setDefaults 设置默认配置值
|
||
func setDefaults() {
|
||
// 应用配置
|
||
viper.SetDefault("app.name", "verification-platform")
|
||
viper.SetDefault("app.env", "development")
|
||
viper.SetDefault("app.port", "8080")
|
||
viper.SetDefault("app.jwt_secret", "your-secret-key")
|
||
viper.SetDefault("app.jwt_expire", "24h")
|
||
|
||
// 数据库配置
|
||
viper.SetDefault("database.type", "auto")
|
||
viper.SetDefault("database.host", "localhost")
|
||
viper.SetDefault("database.port", "3306")
|
||
viper.SetDefault("database.name", "verification_platform")
|
||
viper.SetDefault("database.username", "root")
|
||
viper.SetDefault("database.password", "")
|
||
viper.SetDefault("database.charset", "utf8mb4")
|
||
viper.SetDefault("database.max_idle_conns", "10")
|
||
viper.SetDefault("database.max_open_conns", "100")
|
||
|
||
// Redis配置
|
||
viper.SetDefault("redis.enabled", false)
|
||
viper.SetDefault("redis.host", "localhost")
|
||
viper.SetDefault("redis.port", "6379")
|
||
viper.SetDefault("redis.password", "")
|
||
viper.SetDefault("redis.db", "0")
|
||
|
||
// MCP(Model Context Protocol)服务端
|
||
viper.SetDefault("mcp.enabled", false)
|
||
viper.SetDefault("mcp.path", "/api/v1/mcp")
|
||
viper.SetDefault("mcp.token", "")
|
||
viper.SetDefault("mcp.allow_write", false)
|
||
|
||
// 日志配置
|
||
viper.SetDefault("log.level", "info")
|
||
viper.SetDefault("log.format", "json")
|
||
}
|
||
|
||
// GetString 获取字符串配置
|
||
func GetString(key string) string {
|
||
return viper.GetString(key)
|
||
}
|
||
|
||
// GetInt 获取整数配置
|
||
func GetInt(key string) int {
|
||
return viper.GetInt(key)
|
||
}
|
||
|
||
// GetBool 获取布尔配置
|
||
func GetBool(key string) bool {
|
||
return viper.GetBool(key)
|
||
}
|
||
|
||
// GetStringSlice 获取字符串切片配置
|
||
func GetStringSlice(key string) []string {
|
||
return viper.GetStringSlice(key)
|
||
}
|