|
|
@@ -0,0 +1,222 @@
|
|
|
+package config
|
|
|
+
|
|
|
+import (
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "strconv"
|
|
|
+
|
|
|
+ "github.com/beevik/etree"
|
|
|
+)
|
|
|
+
|
|
|
+var SerCfg *ServiceConfig
|
|
|
+
|
|
|
+type WebConfig struct {
|
|
|
+ ListenPort int
|
|
|
+}
|
|
|
+
|
|
|
+type DbConfig struct {
|
|
|
+ DbDriver string
|
|
|
+ DbAddress string
|
|
|
+ DbName string
|
|
|
+ DbPort string
|
|
|
+ DbUser string
|
|
|
+ DbPwd string
|
|
|
+}
|
|
|
+
|
|
|
+type RedisConfig struct {
|
|
|
+ Address string
|
|
|
+ Port int
|
|
|
+ TimeOut int
|
|
|
+ ConnNum int
|
|
|
+ DbNum int
|
|
|
+ Pwd string
|
|
|
+}
|
|
|
+
|
|
|
+type ServiceConfig struct {
|
|
|
+ WebCfg WebConfig
|
|
|
+ DbCfg DbConfig
|
|
|
+ RedisCfg RedisConfig
|
|
|
+}
|
|
|
+
|
|
|
+func (c *ServiceConfig) Init(path string) error {
|
|
|
+ SerCfg = &ServiceConfig{}
|
|
|
+
|
|
|
+ cfgfile := etree.NewDocument()
|
|
|
+ err := cfgfile.ReadFromFile(path)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Read config file failed")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ root := cfgfile.SelectElement("Configuration")
|
|
|
+ if root == nil {
|
|
|
+ return errors.New("read config file failed")
|
|
|
+ }
|
|
|
+
|
|
|
+ // web配置
|
|
|
+ websettings := root.SelectElements("WebSetting")
|
|
|
+ for _, setting := range websettings {
|
|
|
+ web_listenport := setting.SelectElement("ListenPort")
|
|
|
+ if web_listenport == nil {
|
|
|
+ return errors.New("read web listen port failed")
|
|
|
+ }
|
|
|
+ ret, err := strconv.ParseUint(web_listenport.SelectAttrValue("value", "8080"), 10, 32)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New("read web listen port is invalid:")
|
|
|
+ }
|
|
|
+ SerCfg.WebCfg.ListenPort = int(ret)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数据库配置
|
|
|
+ dbsettings := root.SelectElements("DbSetting")
|
|
|
+ for _, setting := range dbsettings {
|
|
|
+ // 数据库驱动
|
|
|
+ db_driver := setting.SelectElement("DbDriver")
|
|
|
+ if db_driver == nil {
|
|
|
+ return errors.New("read db driver failed")
|
|
|
+ }
|
|
|
+ SerCfg.DbCfg.DbDriver = db_driver.SelectAttrValue("value", "oci8")
|
|
|
+
|
|
|
+ // 数据库地址
|
|
|
+ db_address := setting.SelectElement("DbAddress")
|
|
|
+ if db_address == nil {
|
|
|
+ return errors.New("read db address failed")
|
|
|
+ }
|
|
|
+ SerCfg.DbCfg.DbAddress = db_address.SelectAttrValue("value", "0.0.0.0")
|
|
|
+
|
|
|
+ // 数据库名
|
|
|
+ db_name := setting.SelectElement("DbName")
|
|
|
+ if db_name == nil {
|
|
|
+ return errors.New("read db name failed")
|
|
|
+ }
|
|
|
+ SerCfg.DbCfg.DbName = db_name.SelectAttrValue("value", "orcl")
|
|
|
+
|
|
|
+ // 数据库端口
|
|
|
+ db_port := setting.SelectElement("DbPort")
|
|
|
+ if db_port == nil {
|
|
|
+ return errors.New("read db port failed")
|
|
|
+ }
|
|
|
+ SerCfg.DbCfg.DbPort = db_port.SelectAttrValue("value", "1521")
|
|
|
+
|
|
|
+ // 数据库名
|
|
|
+ db_user := setting.SelectElement("DbUser")
|
|
|
+ if db_user == nil {
|
|
|
+ return errors.New("read db user failed")
|
|
|
+ }
|
|
|
+ SerCfg.DbCfg.DbUser = db_user.SelectAttrValue("value", "mtp2_test01")
|
|
|
+
|
|
|
+ // 数据库密码
|
|
|
+ db_pwd := setting.SelectElement("DbPwd")
|
|
|
+ if db_pwd == nil {
|
|
|
+ return errors.New("read db password failed")
|
|
|
+ }
|
|
|
+ SerCfg.DbCfg.DbPwd = db_pwd.SelectAttrValue("value", "muchinfo")
|
|
|
+ }
|
|
|
+
|
|
|
+ // Redis配置
|
|
|
+ redissettings := root.SelectElements("RedisSetting")
|
|
|
+ for _, setting := range redissettings {
|
|
|
+ // 地址
|
|
|
+ redis_address := setting.SelectElement("Address")
|
|
|
+ if redis_address == nil {
|
|
|
+ return errors.New("read redis address failed")
|
|
|
+ }
|
|
|
+ SerCfg.RedisCfg.Address = redis_address.SelectAttrValue("value", "127.0.0.1")
|
|
|
+
|
|
|
+ // 端口
|
|
|
+ redis_port := setting.SelectElement("Port")
|
|
|
+ if redis_port == nil {
|
|
|
+ return errors.New("read redis port failed")
|
|
|
+ }
|
|
|
+ ret, err := strconv.ParseUint(redis_port.SelectAttrValue("value", "5007"), 10, 32)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New("read redis port is invalid:")
|
|
|
+ }
|
|
|
+ SerCfg.RedisCfg.Port = int(ret)
|
|
|
+
|
|
|
+ // 超时时长
|
|
|
+ redis_timeout := setting.SelectElement("Timeout")
|
|
|
+ if redis_timeout == nil {
|
|
|
+ return errors.New("read redis timeout failed")
|
|
|
+ }
|
|
|
+ ret, err = strconv.ParseUint(redis_timeout.SelectAttrValue("value", "3"), 10, 32)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New("read redis timeout is invalid:")
|
|
|
+ }
|
|
|
+ SerCfg.RedisCfg.TimeOut = int(ret)
|
|
|
+
|
|
|
+ // 连接数
|
|
|
+ redis_connnum := setting.SelectElement("ConnNum")
|
|
|
+ if redis_connnum == nil {
|
|
|
+ return errors.New("read redis connnet num failed")
|
|
|
+ }
|
|
|
+ ret, err = strconv.ParseUint(redis_connnum.SelectAttrValue("value", "1"), 10, 32)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New("read redis connnet num is invalid:")
|
|
|
+ }
|
|
|
+ SerCfg.RedisCfg.ConnNum = int(ret)
|
|
|
+
|
|
|
+ // 库编号
|
|
|
+ redis_dbnum := setting.SelectElement("DbNum")
|
|
|
+ if redis_dbnum == nil {
|
|
|
+ return errors.New("read redis db num failed")
|
|
|
+ }
|
|
|
+ ret, err = strconv.ParseUint(redis_dbnum.SelectAttrValue("value", "0"), 10, 32)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New("read redis db num is invalid:")
|
|
|
+ }
|
|
|
+ SerCfg.RedisCfg.DbNum = int(ret)
|
|
|
+
|
|
|
+ // 密码
|
|
|
+ redis_pwd := setting.SelectElement("Pwd")
|
|
|
+ if redis_pwd == nil {
|
|
|
+ return errors.New("read redis password failed")
|
|
|
+ }
|
|
|
+ SerCfg.RedisCfg.Pwd = redis_pwd.SelectAttrValue("value", "")
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (c *ServiceConfig) GetWebListenPort() int {
|
|
|
+ return SerCfg.WebCfg.ListenPort
|
|
|
+}
|
|
|
+
|
|
|
+func (c *ServiceConfig) GetDbDriver() string {
|
|
|
+ return SerCfg.DbCfg.DbDriver
|
|
|
+}
|
|
|
+
|
|
|
+func (c *ServiceConfig) GetDbSource() string {
|
|
|
+ dbsource := fmt.Sprintf("%s/%s@%s:%s/%s",
|
|
|
+ SerCfg.DbCfg.DbUser,
|
|
|
+ SerCfg.DbCfg.DbPwd,
|
|
|
+ SerCfg.DbCfg.DbAddress,
|
|
|
+ SerCfg.DbCfg.DbPort,
|
|
|
+ SerCfg.DbCfg.DbName)
|
|
|
+
|
|
|
+ return dbsource
|
|
|
+}
|
|
|
+
|
|
|
+func (c *ServiceConfig) GetRedisAddress() string {
|
|
|
+ redisaccress := fmt.Sprintf("%s:%d",
|
|
|
+ SerCfg.RedisCfg.Address,
|
|
|
+ SerCfg.RedisCfg.Port)
|
|
|
+
|
|
|
+ return redisaccress
|
|
|
+}
|
|
|
+
|
|
|
+func (c *ServiceConfig) GetRedisPwd() string {
|
|
|
+ return SerCfg.RedisCfg.Pwd
|
|
|
+}
|
|
|
+
|
|
|
+func (c *ServiceConfig) GetRedisDbNum() int {
|
|
|
+ return SerCfg.RedisCfg.DbNum
|
|
|
+}
|
|
|
+
|
|
|
+func (c *ServiceConfig) GetRedisConnNum() int {
|
|
|
+ return SerCfg.RedisCfg.ConnNum
|
|
|
+}
|
|
|
+
|
|
|
+func (c *ServiceConfig) GetRedisTimeout() int {
|
|
|
+ return SerCfg.RedisCfg.TimeOut
|
|
|
+}
|