zhangzengfei
2023-11-28 3a706d3378aa3626501370352963883fd2783558
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package config
 
import (
    "basic.com/valib/logger.git"
    "github.com/fsnotify/fsnotify"
    "log"
 
    "github.com/spf13/viper"
)
 
type server struct {
    ServerName     string `mapstructure:"serverName"`
    Runmode        string `mapstructure:"runmode"`
    JwtSecret      string `mapstructure:"jwtSecret"`
    JwtExpire      string `mapstructure:"jwtExpire"`
    Url            string `mapstructure:"url"`
    AnalyServerId  string `mapstructure:"analyServerId"`
    NetworkAdapter string `mapstructure:"networkAdapter"`
 
    DeviceNum       string `mapstructure:"deviceNum"`       //设备编号
    DeviceType      string `mapstructure:"deviceType"`      //设备类型
    DeviceModel     string `mapstructure:"deviceModel"`     //设备型号
    DeviceSerialNum string `mapstructure:"deviceSerialNum"` //设备序列号
    DeviceDesc      string `mapstructure:"deviceDesc"`      //设备描述
    MasterVersion   string `mapstructure:"masterVersion"`   //主控版本
    WebVersion      string `mapstructure:"webVersion"`      //web版本
    ChannelCount    string `mapstructure:"channelCount"`    //通道个数
    DiskCount       string `mapstructure:"diskCount"`       //硬盘个数
    LicensePath     string `mapstructure:"licensePath"`     //license文件位置
    LKey            string `mapstructure:"lKey"`
    LPublicKeyPath  string `mapstructure:"lPublicKeyPath"`
 
    SudoPassword  string      `mapstructure:"sudoPassword"` //系统密码
    SysThresholds []threshold `mapstructure:"sysThresholds"`
    PTZSpeed      int         `mapstructure:"ptzSpeed"`  // 云台移动速度
    PatchPath     string      `mapstructure:"patchPath"` //系统更新包路径
    BakPath       string      `mapstructure:"bakPath"`   //系统更新包路径
    Voices        []Voice     `mapstructure:"voices"`
    GbWaitSec     int         `mapstructure:"gbWaitSec"`  //国标刷新后,请求等待时间
    GbCamCount    int         `mapstructure:"gbCamCount"` //国标可配置摄像机数量
    RtspUnique    bool        `mapstructure:"rtspUnique"` //false:允许单机rtsp重复,为true时不允许单机上rtsp重复
 
    Resolutions []resolution `mapstructure:"resolutions"` //数据栈可用的分辨率
    HeadPics    []string     `mapstructure:"headpics"`    //默认头像列表
    Langs       []lang       `mapstructure:"langs"`       //语言
}
 
type threshold struct {
    Value int    `mapstructure:"value"`
    Color string `mapstructure:"color"`
}
 
type Voice struct {
    Id   string `mapstructure:"id"`
    Name string `mapstructure:"name"`
    Path string `mapstructure:"path"`
}
 
type resolution struct {
    Width  int `mapstructure:"width"`
    Height int `mapstructure:"height"`
}
 
type lang struct {
    Name     string `mapstructure:"name"`
    Lang     string `mapstructure:"lang"`
    Language string `mapstructure:"language"`
}
 
var Server = &server{}
 
type database struct {
    Driver   string `mapstructure:"driver"`
    Name     string `mapstructure:"name"`
    FilePath string `mapstructure:"filepath"`
}
 
var DBconf = &database{}
 
type TraceConfig struct {
    UseBlockList int `mapstructure:"useBlockList"`
}
 
var Traceconf = &TraceConfig{}
 
type dbpersoncompare struct {
    Ip   string `mapstructure:"ip"`
    Port int    `mapstructure:"port"`
}
 
type cluster struct {
    PwdPre           string `mapstructure:"pwdpre"`
    SerfSnapShotPath string `mapstructure:"serfsnapshotpath"`
    Port             int    `mapstructure:"port"`
    HeartBeat        int    `mapstructure:"heartBeat"`
    SaasReportUrl    string `mapstructure:"saasReportUrl"`
}
 
var ClusterSet = &cluster{}
 
type esinfo struct {
    Port              int         `mapstructure:"port"`
    Shards            string      `mapstructure:"shards"`
    EsIndex           esindexlist `mapstructure:"index"`
    StorePath         []string    `mapstructure:"storePath"`
    ThresholdTime     int         `mapstructure:"thresholdTime"`
    ThresholdStayTime int         `mapstructure:"thresholdStayTime"`
}
 
type esindexlist struct {
    AiOcean index `mapstructure:"aiocean"`
}
type index struct {
    IndexName string `mapstructure:"index"`
    IndexType string `mapstructure:"type"`
}
 
type weedfs struct {
    Port int `mapstructure:"port"`
}
 
type facedetect struct {
    Ip   string `mapstructure:"Ip"`
    Port int    `mapstructure:"port"`
}
 
type espersoncompare struct {
    Port int      `mapstructure:"port"`
    Ips  []string `mapstructure:"ips"`
}
 
type storageConfig struct {
    VolumePath []string `mapstructure:"volumePath"`
}
 
var StorageConf = &storageConfig{}
 
var DbPersonCompare = &dbpersoncompare{}
 
var EsCompServerInfo = &espersoncompare{}
 
var FaceDetectSet = &facedetect{}
 
var EsConfig = &esinfo{}
 
var WeedFs = &weedfs{}
 
var EsInfo = &esinfo{}
 
type elastic struct {
    IndexName string `mapstructure:"indexName"`
    IndexType string `mapstructure:"indexType"`
}
 
var BasicFS = &elastic{}
 
type logConfig struct {
    Path       string `mapstructure:"path"`       //日志存储路径
    Level      int    `mapstructure:"level"`      //日志等级
    MaxSize    int    `mapstructure:"maxSize"`    //日志文件大小上限
    MaxBackups int    `mapstructure:"maxBackups"` //日志压缩包个数
    MaxAge     int    `mapstructure:"maxAge"`     //保留压缩包天数
}
 
var LogConf = &logConfig{}
 
type shop struct {
    Url        string   `mapstructure:"url"`
    LocalShop  string   `mapstructure:"localShop"`  //本地商城地址
    SysDomains []string `mapstructure:"sysDomains"` //系统升级的域名列表
}
 
var ShopConf = &shop{}
 
type saas struct {
    Url string `mapstructure:"url"`
}
 
var SaasConf = &saas{}
 
type pushSetMenu struct {
    Id       string        `mapstructure:"id" json:"id"`
    Name     string        `mapstructure:"name" json:"name"`
    Checked  bool          `mapstructure:"checked" json:"checked"`
    Alias    string        `mapstructure:"alias" json:"alias"`
    Children []pushSetMenu `mapstructure:"children" json:"children"`
}
 
var PushSet = make([]pushSetMenu, 0)
 
// Init is an exported method that takes the environment starts the viper
// (external lib) and returns the configuration struct.
func Init(env string) {
    var err error
    v := viper.New()
    v.SetConfigType("yaml")
    v.SetConfigName(env)
    v.AddConfigPath("../config/")
    v.AddConfigPath("./config/")
    err = v.ReadInConfig()
    if err != nil {
        log.Fatal("error on parsing configuration file")
    }
    read2Conf(v)
    v.WatchConfig()
    v.OnConfigChange(func(in fsnotify.Event) {
        read2Conf(v)
    })
}
 
func read2Conf(v *viper.Viper) {
    v.UnmarshalKey("es", EsInfo)
    v.UnmarshalKey("server", Server)
    v.UnmarshalKey("database", DBconf)
    v.UnmarshalKey("trace", Traceconf)
 
    v.UnmarshalKey("facedetect", FaceDetectSet)
    v.UnmarshalKey("dbpersoncompare", DbPersonCompare)
    v.UnmarshalKey("espersoncompare", EsCompServerInfo)
    v.UnmarshalKey("elastic.basicFS", BasicFS)
    v.UnmarshalKey("shop", ShopConf)
    v.UnmarshalKey("saas", SaasConf)
    v.UnmarshalKey("thresholdTime", EsInfo.ThresholdTime)
    v.UnmarshalKey("thresholdTime", EsInfo.ThresholdStayTime)
    v.UnmarshalKey("cluster", ClusterSet)
    v.UnmarshalKey("es", EsConfig)
    v.UnmarshalKey("weedfs", WeedFs)
    v.UnmarshalKey("log", LogConf)
    v.UnmarshalKey("storage", StorageConf)
    v.UnmarshalKey("pushSet", &PushSet)
    logger.SetLevel(LogConf.Level)
}