liujiandao
2024-03-30 1e65185f0ecb8d4f8f1fd9ea822137e0c841a47f
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package conf
 
import (
    "aps_crm/pkg/convertx"
    "aps_crm/pkg/logx"
    "aps_crm/pkg/mysqlx"
    "aps_crm/pkg/redisx"
    "flag"
    "github.com/spf13/viper"
    "log"
    "os"
)
 
var (
    // config file name
    configName = "aps-crm"
    // config file paths
    configPaths = []string{
        "./",
        "../",
        "./conf",
        "../conf",
    }
)
 
type (
    Captcha struct {
        KeyLong            int // 验证码长度
        ImgWidth           int // 验证码宽度
        ImgHeight          int // 验证码高度
        OpenCaptcha        int // 防爆破验证码开启此数,0代表每次登录都需要验证码,其他数字代表错误密码次数,如3代表错误三次后出现验证码
        OpenCaptchaTimeOut int // 防爆破验证码超时时间,单位:s(秒)
    }
 
    JWT struct {
        SigningKey  string // jwt签名
        ExpiresTime string // 过期时间
        BufferTime  string // 缓冲时间
        Issuer      string // 签发者
    }
 
    JWT2 struct {
        SigningKey  string // jwt签名
        ExpiresTime string // 过期时间
        BufferTime  string // 缓冲时间
        Issuer      string // 签发者
    }
 
    System struct {
        Env           string // 环境值 develop test public
        Port          int    // 端口
        GrpcPort      string //grpc端口
        DbType        string // 数据库类型
        UseMultipoint bool   // 多点登录拦截
        UseRedis      bool   // 使用redis
        LimitCountIP  int
        LimitTimeIP   int
        RouterPrefix  string // 路由前缀
        SudoPassword  string // sudo密码
    }
 
    GrpcServiceAddr struct {
        Aps   string // aps服务地址
        Admin string // admin服务地址
        WMS   string //wms服务地址
        SRM   string //srm服务地址
    }
 
    config struct {
        // 系统配置
        System System
 
        // 日志
        Log logx.Conf
 
        // mysql配置
        Mysql mysqlx.Conf
 
        // redis配置
        Redis redisx.Conf
 
        // 验证码
        Captcha Captcha
 
        // JWT配置
        JWT JWT
 
        GrpcServiceAddr GrpcServiceAddr
    }
)
 
var (
    Conf  config
    Viper *viper.Viper
)
 
func init() {
    var configFile string
    flag.StringVar(&configFile, "config", "", "config file in json")
    flag.Parse()
 
    if len(configFile) > 0 {
        configName = configFile
    }
 
    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)
    }
 
    if err := Viper.Unmarshal(&Conf); err != nil {
        log.Fatalf("Unmarshal err:%v", err)
    }
 
    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 {
        Conf.Mysql.Dsn = DBUser + ":" + DBPasswd + "@tcp(" + DBHost + ":" + DBPort + ")/" + DBName + "?charset=utf8&parseTime=True&loc=Local"
    }
 
    AdminGrpc := os.Getenv("ADMIN_GRPC")
    if len(AdminGrpc) > 0 {
        Conf.GrpcServiceAddr.Admin = AdminGrpc
    }
    GrpcPort := os.Getenv("WMS_GRPC")
    if len(GrpcPort) > 0 {
        Conf.GrpcServiceAddr.WMS = GrpcPort
    }
    srmPort := os.Getenv("SRM_GRPC")
    if len(srmPort) > 0 {
        Conf.GrpcServiceAddr.SRM = srmPort
    }
    ApsGrpc := os.Getenv("GRPC_PORT")
    Host := os.Getenv("HOST")
 
    if len(Host) > 0 && len(ApsGrpc) > 0 {
        Conf.GrpcServiceAddr.Aps = Host + ":" + ApsGrpc
        //Conf.GrpcServiceAddr.Aps = DBUser + DBUser +
    }
 
    ShowConfig()
    CheckConfig()
}
 
// WriteConfig 回写配置
func (c *config) WriteConfig() error {
    cs := convertx.StructToMap(Conf)
    for k, v := range cs {
        Viper.Set(k, v)
    }
    return Viper.WriteConfig()
}
 
func ShowConfig() {
    log.Println("......................................................")
    log.Printf("   System:                %+v", Conf.System)
    log.Printf("   Log:                   %+v", Conf.Log)
    log.Printf("   Mysql:                 %+v", Conf.Mysql)
    log.Printf("   Captcha:               %+v", Conf.Captcha)
    log.Printf("   JWT:                   %+v", Conf.JWT)
    log.Printf("   GrpcServiceAddr:       %+v", Conf.GrpcServiceAddr)
    log.Println("......................................................")
}
 
func CheckConfig() {
    if Conf.GrpcServiceAddr.Aps == "" {
        log.Fatalf("%v 尚未配置", "GrpcServiceAddr.Aps")
    }
}