提交 38586dcc 编写于 作者: kadycui's avatar kadycui 💻

MOD: 读取配置文件

上级 bcdeaf18
package conf
import (
"fmt"
"log"
"gopkg.in/ini.v1"
)
var (
STATIC_URL = "/fan/static/" // 静态资源目录
MYSQL_URL = struct {
UserName string
Password string
Ip string
Port string
DataBase string
}{UserName: "root", Password: "123456", Ip: "127.0.0.1", Port: "3306", DataBase: "test"}
REDIS_URL = struct {
Ip string
Port string
Password string
}{Ip: "10.16.168.61", Port: "6379", Password: "123456"}
LOG_CONF = struct {
LogFilePath string
LogFileName string
}{LogFileName: "web.logs", LogFilePath: "logs/"}
AppMode string
HttpPort string
MyHost string
MyPort string
MyUser string
MyPw string
MyName string
RedisHost string
RedisPw string
RedisPort string
RedisDb string
)
func init() {
file, err := ini.Load("conf/config.ini")
if err != nil {
log.Println("配置文件读取错误,请检查文件路径:", err)
panic(err)
}
LoadServer(file)
LoadMysqlData(file)
LoadRedis(file)
}
func LoadServer(file *ini.File) {
AppMode = file.Section("service").Key("AppMode").String()
HttpPort = file.Section("service").Key("HttpPort").String()
}
func LoadMysqlData(file *ini.File) {
MyUser = file.Section("mysql").Key("MyUser").String()
MyPw = file.Section("mysql").Key("MyPw").String()
MyHost = file.Section("mysql").Key("MyHost").String()
MyPort = file.Section("mysql").Key("MyPort").String()
MyName = file.Section("mysql").Key("MyName").String()
}
func LoadRedis(file *ini.File) {
RedisHost = file.Section("redis").Key("RedisHost").String()
RedisPort = file.Section("redis").Key("RedisPort").String()
RedisPw = file.Section("redis").Key("RedisPw").String()
RedisDb = file.Section("redis").Key("RedisDb").String()
fmt.Println(RedisHost, RedisPort, RedisPw, RedisDb)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册