zhangqian
2024-04-07 a8914a16b23e93f6bfd12bcfd5cbe8b24cf7eb84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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端口号
    }
 
    localConf struct {
        StorePath string // 本地文件存储路径
    }
 
    grpcServerConf struct {
        ApsAddr string //aps服务grpc地址
        CrmAddr string //crm服务grpc地址
        SrmAddr string //srm服务grpc地址
    }
 
    fileTemplateConf struct {
        InputSelfmadeAddr string //入库自制单模版地址
    }
)
 
var (
    WebConf        = &webConf{}
    LogConf        = &logx.Conf{}
    DbConf         = &mysqlx.Conf{}
    LocalConf      = &localConf{}
    Viper          *viper.Viper
    GrpcServerConf = &grpcServerConf{}
)
 
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")
    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
    }
 
    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)
    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.Println("......................................................")
}