65 lines
2.1 KiB
Go
65 lines
2.1 KiB
Go
// Package config 使用仓库统一的 BSM 运行配置与地址校验。
|
|
package config
|
|
|
|
import (
|
|
"git.apinb.com/bsm-sdk/core/conf"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
var Spec SrvConfig
|
|
|
|
type SrvConfig struct {
|
|
conf.Base `yaml:",inline"`
|
|
Databases *conf.DBConf `yaml:"Databases"`
|
|
Rpc map[string]conf.RpcConf `yaml:"Rpc"`
|
|
Apm *conf.ApmConf `yaml:"APM"`
|
|
PaymentAPI PaymentAPIConfig `yaml:"PaymentAPI"`
|
|
IoTAPI IoTAPIConfig `yaml:"IoTAPI"`
|
|
}
|
|
type PaymentAPIConfig struct {
|
|
BaseURL string `yaml:"BaseURL"`
|
|
Token string `yaml:"Token"`
|
|
IntervalSeconds int `yaml:"IntervalSeconds"`
|
|
}
|
|
type IoTAPIConfig struct {
|
|
PlatformBaseURL string `yaml:"PlatformBaseURL"`
|
|
GatewayBaseURL string `yaml:"GatewayBaseURL"`
|
|
Token string `yaml:"Token"`
|
|
IntervalMilliseconds int `yaml:"IntervalMilliseconds"`
|
|
}
|
|
|
|
func New(srvKey string) {
|
|
conf.New(srvKey, &Spec)
|
|
if databaseDSN := strings.TrimSpace(os.Getenv("HEQI_DATABASE_DSN")); databaseDSN != "" {
|
|
if Spec.Databases == nil {
|
|
panic("Databases configuration is required")
|
|
}
|
|
Spec.Databases.Source = []string{databaseDSN}
|
|
}
|
|
if redisURL := strings.TrimSpace(os.Getenv("HEQI_REDIS_URL")); redisURL != "" {
|
|
Spec.Cache = redisURL
|
|
}
|
|
if token := strings.TrimSpace(os.Getenv("HEQI_PAYMENT_INTERNAL_TOKEN")); token != "" {
|
|
Spec.PaymentAPI.Token = token
|
|
}
|
|
if token := strings.TrimSpace(os.Getenv("HEQI_IOT_INTERNAL_TOKEN")); token != "" {
|
|
Spec.IoTAPI.Token = token
|
|
}
|
|
Spec.Port = conf.CheckPort(Spec.Port)
|
|
Spec.BindIP = conf.CheckIP(Spec.BindIP)
|
|
Spec.Addr = net.JoinHostPort(Spec.BindIP, Spec.Port)
|
|
conf.NotNil(Spec.Service, Spec.Cache)
|
|
if Spec.Databases == nil || len(Spec.Databases.Source) == 0 {
|
|
panic("Databases configuration is required")
|
|
}
|
|
if Spec.PaymentAPI.BaseURL == "" || Spec.PaymentAPI.Token == "" || Spec.PaymentAPI.IntervalSeconds <= 0 {
|
|
panic("PaymentAPI configuration is required")
|
|
}
|
|
if Spec.IoTAPI.PlatformBaseURL == "" || Spec.IoTAPI.GatewayBaseURL == "" || Spec.IoTAPI.Token == "" || Spec.IoTAPI.IntervalMilliseconds <= 0 {
|
|
panic("IoTAPI configuration is required")
|
|
}
|
|
conf.PrintInfo(Spec.Addr)
|
|
}
|