package ConfigUtil import ( "fmt" "gopkg.in/ini.v1" "io/ioutil" "os" ) var ( //要发布到的主机信息 DistributeIp string DistributePort int32 DistributeUser string DistributePwd string DistributeRemotePath string DistributeLocalPath string //服务器的端口 ServerPort string //mysql配置 MysqlIp string MysqlPort string MysqlDbName string MysqlUser string MysqlPwd string //REDIS配置 RedisIp string RedisPort string RedisDb string RedisExpireTime int64 //项目名称 ProjectName string //登录信息 UserName string UserPwd string //RSA私钥 PrivateKey string ) // 判断所给路径文件/文件夹是否存在 func Exists(path string) bool { _, err := os.Stat(path) //os.Stat获取文件信息 if err != nil { if os.IsExist(err) { return true } return false } return true } func init() { //判断是不是单元测试,如果是的话,那么配置文件的路径需要加上一个. var configIniFile = "./Config/Config.ini" //判断文件不是存在 if !Exists(configIniFile) { configIniFile = "." + configIniFile } //再不存在,直接走物理路径 if !Exists(configIniFile) { configIniFile = "/usr/local/dsMin/dsSdsf/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 } //要发布到的主机信息 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") //web服务器端口 ServerPort = iniParser.GetString("server", "port") //读取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") //mysql MysqlIp = iniParser.GetString("mysql", "ip") MysqlPort = iniParser.GetString("mysql", "port") MysqlDbName = iniParser.GetString("mysql", "db_name") MysqlUser = iniParser.GetString("mysql", "user") MysqlPwd = iniParser.GetString("mysql", "pwd") //登录信息 UserName = iniParser.GetString("mysql", "user_name") UserPwd = iniParser.GetString("mysql", "user_pwd") b, err := ioutil.ReadFile("./Config/privateKey.txt") if err != nil { fmt.Print(err) } PrivateKey = string(b) } 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 }