84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package config
|
|
|
|
import "hyapp/pkg/configx"
|
|
|
|
// Config 描述 user-service 启动配置。
|
|
type Config struct {
|
|
ServiceName string `yaml:"service_name"`
|
|
NodeID string `yaml:"node_id"`
|
|
GRPCAddr string `yaml:"grpc_addr"`
|
|
MySQLDSN string `yaml:"mysql_dsn"`
|
|
MySQLAutoMigrate bool `yaml:"mysql_auto_migrate"`
|
|
IDGenerator IDGeneratorConfig `yaml:"id_generator"`
|
|
DisplayUserID DisplayUserIDConfig `yaml:"display_user_id"`
|
|
ThirdParty ThirdPartyConfig `yaml:"third_party"`
|
|
JWT JWTConfig `yaml:"jwt"`
|
|
}
|
|
|
|
// IDGeneratorConfig 保存 user_id 发号节点配置。
|
|
type IDGeneratorConfig struct {
|
|
NodeID int64 `yaml:"node_id"`
|
|
}
|
|
|
|
// DisplayUserIDConfig 保存短号分配和修改策略。
|
|
type DisplayUserIDConfig struct {
|
|
AllocateMaxAttempts int `yaml:"allocate_max_attempts"`
|
|
ChangeCooldownSec int64 `yaml:"change_cooldown_sec"`
|
|
}
|
|
|
|
// ThirdPartyConfig 保存三方登录 provider allowlist。
|
|
type ThirdPartyConfig struct {
|
|
AllowedProviders []string `yaml:"allowed_providers"`
|
|
}
|
|
|
|
// JWTConfig 保存 access token 和 refresh token 的签发策略。
|
|
type JWTConfig struct {
|
|
Issuer string `yaml:"issuer"`
|
|
AccessTokenTTLSec int64 `yaml:"access_token_ttl_sec"`
|
|
RefreshTokenTTLSec int64 `yaml:"refresh_token_ttl_sec"`
|
|
SigningAlg string `yaml:"signing_alg"`
|
|
SigningSecret string `yaml:"signing_secret"`
|
|
}
|
|
|
|
// Default 返回本地默认配置。
|
|
func Default() Config {
|
|
return Config{
|
|
ServiceName: "user-service",
|
|
NodeID: "user-local",
|
|
GRPCAddr: ":13005",
|
|
MySQLDSN: "",
|
|
MySQLAutoMigrate: false,
|
|
IDGenerator: IDGeneratorConfig{
|
|
NodeID: 1,
|
|
},
|
|
DisplayUserID: DisplayUserIDConfig{
|
|
AllocateMaxAttempts: 8,
|
|
ChangeCooldownSec: 2592000,
|
|
},
|
|
ThirdParty: ThirdPartyConfig{
|
|
AllowedProviders: []string{"wechat"},
|
|
},
|
|
JWT: JWTConfig{
|
|
Issuer: "hyapp",
|
|
AccessTokenTTLSec: 1800,
|
|
RefreshTokenTTLSec: 2592000,
|
|
SigningAlg: "HS256",
|
|
SigningSecret: "dev-shared-secret",
|
|
},
|
|
}
|
|
}
|
|
|
|
// Load 从独立 config.yaml 读取 user-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
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|