package conf
|
|
import (
|
"log"
|
"os"
|
"wms/pkg/logx"
|
"wms/pkg/mysqlx"
|
|
"github.com/spf13/viper"
|
)
|
|
var (
|
// config file name
|
configName = "config"
|
// config file paths
|
configPaths = []string{
|
"./",
|
"../",
|
"./conf",
|
"../conf",
|
}
|
)
|
|
type (
|
webConf struct {
|
Host string // 本机ip地址
|
Port string // 端口号
|
NodeId string // 主账户用户名
|
OssType string // 对象存储类型
|
JWTSecret string
|
FileServer string //文件服务器地址
|
ServerId string //服务ID
|
GrpcPort string //grpc端口号
|
CompanyName string //公司名
|
}
|
|
localConf struct {
|
StorePath string // 本地文件存储路径
|
}
|
|
grpcServerConf struct {
|
ApsAddr string //aps服务grpc地址
|
CrmAddr string //crm服务grpc地址
|
SrmAddr string //srm服务grpc地址
|
}
|
|
fileTemplateConf struct {
|
InputSelfmadeAddr string //入库自制单模版地址
|
}
|
|
dingTalkConf struct {
|
AlarmKey string
|
AlarmUrl string
|
}
|
)
|
|
var (
|
WebConf = &webConf{}
|
LogConf = &logx.Conf{}
|
DbConf = &mysqlx.Conf{}
|
LocalConf = &localConf{}
|
Viper *viper.Viper
|
GrpcServerConf = &grpcServerConf{}
|
DingTalkConf = &dingTalkConf{}
|
)
|
|
func Init() error {
|
Viper = viper.New()
|
Viper.SetConfigName(configName)
|
for _, path := range configPaths {
|
Viper.AddConfigPath(path)
|
}
|
|
if err := Viper.ReadInConfig(); err != nil {
|
log.Fatalf("ReadInConfig err:%v", err)
|
}
|
read2Conf(Viper)
|
|
nodeId := os.Getenv("NODE_ID") // 主账户用户名
|
host := os.Getenv("HOST") // 本机IP地址
|
GrpcPort := os.Getenv("WMS_GRPC") // 只对外提供grpc服务,本服务不用
|
apsAddr := os.Getenv("APS_GRPC")
|
crmAddr := os.Getenv("CRM_GRPC")
|
srmAddr := os.Getenv("SRM_GRPC")
|
companyName := os.Getenv("COMPANY_NAME")
|
if len(GrpcPort) > 0 {
|
WebConf.GrpcPort = GrpcPort
|
}
|
if len(nodeId) > 0 {
|
WebConf.NodeId = nodeId
|
}
|
if len(host) > 0 {
|
WebConf.Host = host
|
}
|
if len(apsAddr) > 0 {
|
GrpcServerConf.ApsAddr = apsAddr
|
}
|
if len(crmAddr) > 0 {
|
GrpcServerConf.CrmAddr = crmAddr
|
}
|
if len(srmAddr) > 0 {
|
GrpcServerConf.SrmAddr = srmAddr
|
}
|
if len(companyName) > 0 {
|
WebConf.CompanyName = companyName
|
}
|
|
DBHost := os.Getenv("DB_HOST")
|
DBName := os.Getenv("DB_NAME")
|
DBPort := os.Getenv("DB_PORT")
|
DBUser := os.Getenv("DB_USER")
|
DBPasswd := os.Getenv("DB_PASSWD")
|
if len(DBHost) > 0 &&
|
len(DBName) > 0 &&
|
len(DBPort) > 0 &&
|
len(DBUser) > 0 &&
|
len(DBPasswd) > 0 {
|
DbConf.Dsn = DBUser + ":" + DBPasswd + "@tcp(" + DBHost + ":" + DBPort + ")/" + DBName + "?charset=utf8&parseTime=True&loc=Local"
|
}
|
|
return nil
|
}
|
|
func read2Conf(v *viper.Viper) {
|
_ = v.UnmarshalKey("web", WebConf)
|
_ = v.UnmarshalKey("log", LogConf)
|
_ = v.UnmarshalKey("db", DbConf)
|
_ = v.UnmarshalKey("local", LocalConf)
|
_ = v.UnmarshalKey("grpcServer", GrpcServerConf)
|
_ = v.UnmarshalKey("dingTalk", DingTalkConf)
|
showConfig()
|
}
|
|
func showConfig() {
|
log.Println("......................................................")
|
log.Printf(" WebConf: %+v", WebConf)
|
log.Printf(" LogConf: %+v", LogConf)
|
log.Printf(" DbConf: %+v", DbConf)
|
log.Printf(" LocalConf: %+v", LocalConf)
|
log.Printf(" GrpcServerConf: %+v", GrpcServerConf)
|
log.Printf(" DingTalkConf: %+v", DingTalkConf)
|
log.Println("......................................................")
|
}
|