You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
269 lines
5.6 KiB
269 lines
5.6 KiB
package ConfigUtil
|
|
|
|
import (
|
|
"dsDataex/Utils/CommonUtil"
|
|
"fmt"
|
|
"gopkg.in/ini.v1"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
// mysql数据库服务器配置
|
|
MysqlIp string
|
|
MysqlPort string
|
|
MysqlUser string
|
|
MysqlPwd string
|
|
MysqlDataBase string
|
|
|
|
//要发布到的主机信息
|
|
DistributeIp string
|
|
DistributePort int32
|
|
DistributeUser string
|
|
DistributePwd string
|
|
DistributeRemotePath string
|
|
DistributeLocalPath string
|
|
|
|
//REDIS配置
|
|
RedisIp string
|
|
RedisPort string
|
|
RedisDb string
|
|
RedisExpireTime int64
|
|
|
|
//运行时配置
|
|
StartPathWindows string
|
|
StartPathLinux string
|
|
|
|
//项目名称
|
|
ProjectName string
|
|
|
|
ProjectPort string
|
|
|
|
ProjectGrpc string
|
|
|
|
ProjectPath string
|
|
|
|
//kafka地址
|
|
//KafkaAddress string
|
|
|
|
//Kafka broker地址
|
|
KafkaBrokers []string
|
|
|
|
KafkaAccessLogTopic string
|
|
|
|
KafkaParts int64
|
|
|
|
KafkaReply int64
|
|
|
|
KafkaProcNo int64
|
|
|
|
KafkaEnable int64
|
|
|
|
//ES Nodes 地址
|
|
ESNodes string
|
|
|
|
ESAddress []string
|
|
|
|
ESUser string
|
|
|
|
ESPWD string
|
|
|
|
|
|
|
|
//Params 系统参数
|
|
ROOT_ORGNAME string
|
|
)
|
|
|
|
func init() {
|
|
|
|
//判断是不是单元测试,如果是的话,那么配置文件的路径需要加上一个.
|
|
var configIniFile = "././Config/Config.ini"
|
|
//判断文件不是存在
|
|
if !CommonUtil.Exists(configIniFile) {
|
|
configIniFile = "." + configIniFile
|
|
}
|
|
if !CommonUtil.Exists(configIniFile) {
|
|
configIniFile = "/usr/local/dsMin/dsBaseRpc/Config/Config.ini"
|
|
}
|
|
iniParser := IniParser{}
|
|
if err := iniParser.Load(configIniFile); err != nil {
|
|
fmt.Printf("try load config file[%s] error[%s]\n", configIniFile, err.Error())
|
|
return
|
|
}
|
|
//主机
|
|
MysqlIp = iniParser.GetString("mysql", "ip")
|
|
//端口
|
|
MysqlPort = iniParser.GetString("mysql", "port")
|
|
//数据库名称
|
|
MysqlDataBase = iniParser.GetString("mysql", "database")
|
|
//用户
|
|
MysqlUser = iniParser.GetString("mysql", "user")
|
|
//密码
|
|
MysqlPwd = iniParser.GetString("mysql", "pwd")
|
|
|
|
//发布的远端IP
|
|
DistributeIp = iniParser.GetString("distribute", "ip")
|
|
//发布的远端端口
|
|
DistributePort = iniParser.GetInt32("distribute", "port")
|
|
//发布的远端用户名
|
|
DistributeUser = iniParser.GetString("distribute", "user")
|
|
//发布的远端密码
|
|
DistributePwd = iniParser.GetString("distribute", "pwd")
|
|
//远端路径
|
|
DistributeRemotePath = iniParser.GetString("distribute", "remotePath")
|
|
//本地路径
|
|
DistributeLocalPath = iniParser.GetString("distribute", "localPath")
|
|
|
|
//读取redis配置
|
|
RedisIp = iniParser.GetString("redis", "ip")
|
|
RedisPort = iniParser.GetString("redis", "port")
|
|
RedisDb = iniParser.GetString("redis", "db")
|
|
RedisExpireTime = iniParser.GetInt64("redis", "expireTime")
|
|
|
|
//项目名称
|
|
ProjectName = iniParser.GetString("project", "project_name")
|
|
ProjectPath = iniParser.GetString("project", "project_path")
|
|
ProjectPort = iniParser.GetString("project", "project_port")
|
|
ProjectGrpc = iniParser.GetString("project", "project_grpc")
|
|
|
|
//change by zhangjun 2020-07-24
|
|
//kafka地址
|
|
//KafkaAddress = iniParser.GetString("kafka", "KafkaAddress")
|
|
KafkaBrokers=strings.Split(iniParser.GetString("kafka", "brokers"),",")
|
|
|
|
KafkaReply =iniParser.GetInt64("kafka", "replication")
|
|
KafkaProcNo=iniParser.GetInt64("kafka", "process_no")
|
|
KafkaParts=iniParser.GetInt64("kafka", "partition")
|
|
KafkaEnable=iniParser.GetInt64("kafka", "enable")
|
|
|
|
//add by zhangjun 2020-08-03
|
|
KafkaAccessLogTopic= iniParser.GetString("kafka", "KafkaAccessLogTopic")
|
|
|
|
|
|
ESNodes = iniParser.GetString("elasticsearch", "nodes")
|
|
ESUser = iniParser.GetString("elasticsearch", "user")
|
|
ESPWD = iniParser.GetString("elasticsearch", "pwd")
|
|
|
|
ESAddress= strings.Split(ESNodes,",")
|
|
|
|
ROOT_ORGNAME=iniParser.GetString("params", "root_orgname")
|
|
}
|
|
|
|
type IniParser struct {
|
|
confReader *ini.File // config reader
|
|
}
|
|
|
|
type IniParserError struct {
|
|
errorInfo string
|
|
}
|
|
|
|
func (e *IniParserError) Error() string { return e.errorInfo }
|
|
|
|
func (this *IniParser) Load(configFileName string) error {
|
|
conf, err := ini.Load(configFileName)
|
|
if err != nil {
|
|
this.confReader = nil
|
|
return err
|
|
}
|
|
this.confReader = conf
|
|
return nil
|
|
}
|
|
|
|
func (this *IniParser) GetString(section string, key string) string {
|
|
if this.confReader == nil {
|
|
return ""
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return ""
|
|
}
|
|
|
|
return s.Key(key).String()
|
|
}
|
|
|
|
func (this *IniParser) GetInt32(section string, key string) int32 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueInt, _ := s.Key(key).Int()
|
|
|
|
return int32(valueInt)
|
|
}
|
|
|
|
func (this *IniParser) GetUint32(section string, key string) uint32 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueInt, _ := s.Key(key).Uint()
|
|
|
|
return uint32(valueInt)
|
|
}
|
|
|
|
func (this *IniParser) GetInt64(section string, key string) int64 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueInt, _ := s.Key(key).Int64()
|
|
return valueInt
|
|
}
|
|
|
|
func (this *IniParser) GetUint64(section string, key string) uint64 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueInt, _ := s.Key(key).Uint64()
|
|
return valueInt
|
|
}
|
|
|
|
func (this *IniParser) GetFloat32(section string, key string) float32 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueFloat, _ := s.Key(key).Float64()
|
|
return float32(valueFloat)
|
|
}
|
|
|
|
func (this *IniParser) GetFloat64(section string, key string) float64 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueFloat, _ := s.Key(key).Float64()
|
|
return valueFloat
|
|
}
|