73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"hyapp/pkg/configx"
|
|
"hyapp/pkg/logx"
|
|
)
|
|
|
|
// Config 描述 wallet-service 启动配置。
|
|
type Config struct {
|
|
ServiceName string `yaml:"service_name"`
|
|
NodeID string `yaml:"node_id"`
|
|
Environment string `yaml:"environment"`
|
|
GRPCAddr string `yaml:"grpc_addr"`
|
|
// HealthHTTPAddr 是给 CLB/发布脚本探活使用的 HTTP 监听地址,不承载业务流量。
|
|
HealthHTTPAddr string `yaml:"health_http_addr"`
|
|
// MySQLDSN 是账户、交易、分录和 outbox 的必需存储连接串。
|
|
MySQLDSN string `yaml:"mysql_dsn"`
|
|
// MySQLAutoMigrate 保留给显式本地迁移;线上 schema 由发布流程或 initdb 管理。
|
|
MySQLAutoMigrate bool `yaml:"mysql_auto_migrate"`
|
|
Log logx.Config `yaml:"log"`
|
|
}
|
|
|
|
// Default 返回本地开发默认配置。
|
|
func Default() Config {
|
|
return Config{
|
|
ServiceName: "wallet-service",
|
|
NodeID: "wallet-local",
|
|
Environment: "local",
|
|
GRPCAddr: ":13004",
|
|
HealthHTTPAddr: ":13104",
|
|
MySQLDSN: "hyapp:hyapp@tcp(127.0.0.1:23306)/hyapp_wallet?parseTime=true&charset=utf8mb4&loc=UTC",
|
|
MySQLAutoMigrate: false,
|
|
Log: logx.Config{
|
|
Level: "info",
|
|
Format: "json",
|
|
MaxPayloadBytes: 2048,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Load 从独立 config.yaml 读取 wallet-service 配置。
|
|
func Load(path string) (Config, error) {
|
|
cfg := Default()
|
|
if path == "" {
|
|
return cfg, nil
|
|
}
|
|
|
|
if err := configx.LoadYAML(path, &cfg); err != nil {
|
|
return Config{}, err
|
|
}
|
|
|
|
cfg.ServiceName = strings.TrimSpace(cfg.ServiceName)
|
|
if cfg.ServiceName == "" {
|
|
cfg.ServiceName = "wallet-service"
|
|
}
|
|
cfg.NodeID = strings.TrimSpace(cfg.NodeID)
|
|
if cfg.NodeID == "" {
|
|
cfg.NodeID = cfg.ServiceName
|
|
}
|
|
cfg.Environment = strings.ToLower(strings.TrimSpace(cfg.Environment))
|
|
if cfg.Environment == "" {
|
|
cfg.Environment = "local"
|
|
}
|
|
cfg.HealthHTTPAddr = strings.TrimSpace(cfg.HealthHTTPAddr)
|
|
if cfg.HealthHTTPAddr == "" {
|
|
cfg.HealthHTTPAddr = ":13104"
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|