config.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. package config
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "github.com/beevik/etree"
  7. )
  8. var SerCfg *ServiceConfig
  9. type WebConfig struct {
  10. ListenAddr string
  11. DebugMode bool
  12. }
  13. type LogConfig struct {
  14. LogPath string
  15. LogLevel int
  16. }
  17. type DbConfig struct {
  18. DbDriver string
  19. DbAddress string
  20. DbName string
  21. DbPort string
  22. DbUser string
  23. DbPwd string
  24. }
  25. type RedisConfig struct {
  26. Address string
  27. Port int
  28. TimeOut int
  29. ConnNum int
  30. DbNum int
  31. Pwd string
  32. }
  33. type MqConfig struct {
  34. Url string
  35. Exchange string
  36. }
  37. type MongoDBConfig struct {
  38. HostName string
  39. Port int
  40. DBName string
  41. Username string
  42. Password string
  43. }
  44. type MySQLConfig struct {
  45. Host string
  46. Port int
  47. DBName string
  48. Username string
  49. Password string
  50. }
  51. type TencentConfig struct {
  52. Enabled int
  53. SecretId string
  54. SecretKey string
  55. EndPoint string
  56. AppId string
  57. ProxyOrganizationName string
  58. ProxyOrganizationOpenId string
  59. ProxyOperatorOpenId string
  60. SignToken string
  61. SignKey string
  62. }
  63. type AsignConfig struct {
  64. Enabled int
  65. Url string
  66. AppId string
  67. PrivateKey string
  68. NotifyUrl string
  69. OpenApiUrl string
  70. }
  71. type ServiceConfig struct {
  72. WebCfg WebConfig
  73. LogCfg LogConfig
  74. DbCfg DbConfig
  75. RedisCfg RedisConfig
  76. MqCfg MqConfig
  77. MongoDBCfg MongoDBConfig
  78. MySQLCfg MySQLConfig
  79. TencentCfg TencentConfig
  80. AsignCfg AsignConfig
  81. }
  82. func (c *ServiceConfig) Init(path string) error {
  83. SerCfg = &ServiceConfig{}
  84. cfgfile := etree.NewDocument()
  85. err := cfgfile.ReadFromFile(path)
  86. if err != nil {
  87. fmt.Println("Read config file failed")
  88. return err
  89. }
  90. root := cfgfile.SelectElement("Configuration")
  91. if root == nil {
  92. return errors.New("read config file failed")
  93. }
  94. // web配置
  95. websettings := root.SelectElements("WebSetting")
  96. for _, setting := range websettings {
  97. web_listenaddr := setting.SelectElement("ListenAddress")
  98. if web_listenaddr == nil {
  99. return errors.New("read web listen address failed")
  100. }
  101. SerCfg.WebCfg.ListenAddr = web_listenaddr.SelectAttrValue("value", "0.0.0.0:8080")
  102. // 调试模式
  103. debug_mode := setting.SelectElement("DebugMode")
  104. if debug_mode == nil {
  105. return errors.New("read debug mode failed")
  106. }
  107. ret, err := strconv.ParseUint(debug_mode.SelectAttrValue("value", "0"), 10, 32)
  108. if err != nil {
  109. return errors.New("read debug mode is invalid:")
  110. }
  111. SerCfg.WebCfg.DebugMode = (ret == 1)
  112. }
  113. // 日志配置
  114. logsettings := root.SelectElements("LogSetting")
  115. for _, setting := range logsettings {
  116. // 日志路径
  117. log_path := setting.SelectElement("LogPath")
  118. if log_path == nil {
  119. return errors.New("read log path failed")
  120. }
  121. SerCfg.LogCfg.LogPath = log_path.SelectAttrValue("value", "./")
  122. // 日志级别
  123. log_level := setting.SelectElement("LogLevel")
  124. if log_level == nil {
  125. return errors.New("read log level failed")
  126. }
  127. ret, err := strconv.ParseUint(log_level.SelectAttrValue("value", "5"), 10, 32)
  128. if err != nil {
  129. return errors.New("read log level is invalid:")
  130. }
  131. SerCfg.LogCfg.LogLevel = int(ret)
  132. }
  133. // 数据库配置
  134. dbsettings := root.SelectElements("DbSetting")
  135. for _, setting := range dbsettings {
  136. // 数据库驱动
  137. db_driver := setting.SelectElement("DbDriver")
  138. if db_driver == nil {
  139. return errors.New("read db driver failed")
  140. }
  141. SerCfg.DbCfg.DbDriver = db_driver.SelectAttrValue("value", "oci8")
  142. // 数据库地址
  143. db_address := setting.SelectElement("DbAddress")
  144. if db_address == nil {
  145. return errors.New("read db address failed")
  146. }
  147. SerCfg.DbCfg.DbAddress = db_address.SelectAttrValue("value", "0.0.0.0")
  148. // 数据库名
  149. db_name := setting.SelectElement("DbName")
  150. if db_name == nil {
  151. return errors.New("read db name failed")
  152. }
  153. SerCfg.DbCfg.DbName = db_name.SelectAttrValue("value", "orcl")
  154. // 数据库端口
  155. db_port := setting.SelectElement("DbPort")
  156. if db_port == nil {
  157. return errors.New("read db port failed")
  158. }
  159. SerCfg.DbCfg.DbPort = db_port.SelectAttrValue("value", "1521")
  160. // 数据库名
  161. db_user := setting.SelectElement("DbUser")
  162. if db_user == nil {
  163. return errors.New("read db user failed")
  164. }
  165. SerCfg.DbCfg.DbUser = db_user.SelectAttrValue("value", "mtp2_test01")
  166. // 数据库密码
  167. db_pwd := setting.SelectElement("DbPwd")
  168. if db_pwd == nil {
  169. return errors.New("read db password failed")
  170. }
  171. SerCfg.DbCfg.DbPwd = db_pwd.SelectAttrValue("value", "muchinfo")
  172. }
  173. // Redis配置
  174. redissettings := root.SelectElements("RedisSetting")
  175. for _, setting := range redissettings {
  176. // 地址
  177. redis_address := setting.SelectElement("Address")
  178. if redis_address == nil {
  179. return errors.New("read redis address failed")
  180. }
  181. SerCfg.RedisCfg.Address = redis_address.SelectAttrValue("value", "127.0.0.1")
  182. // 端口
  183. redis_port := setting.SelectElement("Port")
  184. if redis_port == nil {
  185. return errors.New("read redis port failed")
  186. }
  187. ret, err := strconv.ParseUint(redis_port.SelectAttrValue("value", "5007"), 10, 32)
  188. if err != nil {
  189. return errors.New("read redis port is invalid:")
  190. }
  191. SerCfg.RedisCfg.Port = int(ret)
  192. // 超时时长
  193. redis_timeout := setting.SelectElement("Timeout")
  194. if redis_timeout == nil {
  195. return errors.New("read redis timeout failed")
  196. }
  197. ret, err = strconv.ParseUint(redis_timeout.SelectAttrValue("value", "3"), 10, 32)
  198. if err != nil {
  199. return errors.New("read redis timeout is invalid:")
  200. }
  201. SerCfg.RedisCfg.TimeOut = int(ret)
  202. // 连接数
  203. redis_connnum := setting.SelectElement("ConnNum")
  204. if redis_connnum == nil {
  205. return errors.New("read redis connnet num failed")
  206. }
  207. ret, err = strconv.ParseUint(redis_connnum.SelectAttrValue("value", "1"), 10, 32)
  208. if err != nil {
  209. return errors.New("read redis connnet num is invalid:")
  210. }
  211. SerCfg.RedisCfg.ConnNum = int(ret)
  212. // 库编号
  213. redis_dbnum := setting.SelectElement("DbNum")
  214. if redis_dbnum == nil {
  215. return errors.New("read redis db num failed")
  216. }
  217. ret, err = strconv.ParseUint(redis_dbnum.SelectAttrValue("value", "0"), 10, 32)
  218. if err != nil {
  219. return errors.New("read redis db num is invalid:")
  220. }
  221. SerCfg.RedisCfg.DbNum = int(ret)
  222. // 密码
  223. redis_pwd := setting.SelectElement("Pwd")
  224. if redis_pwd == nil {
  225. return errors.New("read redis password failed")
  226. }
  227. SerCfg.RedisCfg.Pwd = redis_pwd.SelectAttrValue("value", "")
  228. }
  229. // RabbitMQ配置
  230. mqsettings := root.SelectElements("MqSetting")
  231. for _, setting := range mqsettings {
  232. // url
  233. url := setting.SelectElement("Url")
  234. if url == nil {
  235. return errors.New("read rabbitmq url failed")
  236. }
  237. SerCfg.MqCfg.Url = url.SelectAttrValue("value", "")
  238. // exchange
  239. exchange := setting.SelectElement("Exchange")
  240. if exchange == nil {
  241. return errors.New("read rabbitmq exchange failed")
  242. }
  243. SerCfg.MqCfg.Exchange = exchange.SelectAttrValue("value", "entry")
  244. }
  245. // MongoDB配置
  246. mongodbsettings := root.SelectElements("MongoDBSetting")
  247. for _, setting := range mongodbsettings {
  248. // 主机地址
  249. hostName := setting.SelectElement("HostName")
  250. if hostName == nil {
  251. return errors.New("read mongondb hostname failed")
  252. }
  253. SerCfg.MongoDBCfg.HostName = hostName.SelectAttrValue("value", "")
  254. // 端口
  255. port := setting.SelectElement("Port")
  256. if port == nil {
  257. return errors.New("read mongondb port failed")
  258. }
  259. ret, err := strconv.ParseUint(port.SelectAttrValue("value", "5025"), 10, 32)
  260. if err != nil {
  261. return errors.New("read mongondb port failed")
  262. }
  263. SerCfg.MongoDBCfg.Port = int(ret)
  264. // 数据库名
  265. dbname := setting.SelectElement("DBName")
  266. if dbname == nil {
  267. return errors.New("read mongondb dbname failed")
  268. }
  269. SerCfg.MongoDBCfg.DBName = dbname.SelectAttrValue("value", "")
  270. // 用户名
  271. username := setting.SelectElement("Username")
  272. if username == nil {
  273. return errors.New("read mongondb username failed")
  274. }
  275. SerCfg.MongoDBCfg.Username = username.SelectAttrValue("value", "")
  276. // 密码
  277. password := setting.SelectElement("Password")
  278. if password == nil {
  279. return errors.New("read mongondb password failed")
  280. }
  281. SerCfg.MongoDBCfg.Password = password.SelectAttrValue("value", "")
  282. }
  283. // MySQL配置
  284. mysqlsettings := root.SelectElements("MySQLSetting")
  285. for _, setting := range mysqlsettings {
  286. // 主机地址
  287. host := setting.SelectElement("Host")
  288. if host == nil {
  289. return errors.New("read mysql host failed")
  290. }
  291. SerCfg.MySQLCfg.Host = host.SelectAttrValue("value", "")
  292. // 端口
  293. port := setting.SelectElement("Port")
  294. if port == nil {
  295. return errors.New("read mysql port failed")
  296. }
  297. ret, err := strconv.ParseUint(port.SelectAttrValue("value", "3306"), 10, 32)
  298. if err != nil {
  299. return errors.New("read mysql port failed")
  300. }
  301. SerCfg.MySQLCfg.Port = int(ret)
  302. // 数据库名
  303. dbname := setting.SelectElement("DBName")
  304. if dbname == nil {
  305. return errors.New("read mysql dbname failed")
  306. }
  307. SerCfg.MySQLCfg.DBName = dbname.SelectAttrValue("value", "")
  308. // 用户名
  309. username := setting.SelectElement("Username")
  310. if username == nil {
  311. return errors.New("read mysql username failed")
  312. }
  313. SerCfg.MySQLCfg.Username = username.SelectAttrValue("value", "")
  314. // 密码
  315. password := setting.SelectElement("Password")
  316. if password == nil {
  317. return errors.New("read mysql password failed")
  318. }
  319. SerCfg.MySQLCfg.Password = password.SelectAttrValue("value", "")
  320. }
  321. // Tencent配置
  322. tencentsettings := root.SelectElements("Tencent")
  323. for _, setting := range tencentsettings {
  324. // 启用标志
  325. enabled := setting.SelectElement("Enabled")
  326. if enabled == nil {
  327. return errors.New("read tencent enabled failed")
  328. }
  329. ret, err := strconv.ParseUint(enabled.SelectAttrValue("value", "0"), 10, 32)
  330. if err != nil {
  331. return errors.New("read tencent enabled failed")
  332. }
  333. SerCfg.TencentCfg.Enabled = int(ret)
  334. // 密钥ID
  335. secretId := setting.SelectElement("SecretId")
  336. if secretId == nil {
  337. return errors.New("read tencent SecretId failed")
  338. }
  339. SerCfg.TencentCfg.SecretId = secretId.SelectAttrValue("value", "")
  340. // 密钥key
  341. secretKey := setting.SelectElement("SecretKey")
  342. if secretKey == nil {
  343. return errors.New("read tencent SecretKey failed")
  344. }
  345. SerCfg.TencentCfg.SecretKey = secretKey.SelectAttrValue("value", "")
  346. // EndPoint API域名,现网使用 essbasic.tencentcloudapi.com 测试使用 essbasic.test.ess.tencent.cn
  347. endPoint := setting.SelectElement("EndPoint")
  348. if endPoint == nil {
  349. return errors.New("read tencent EndPoint failed")
  350. }
  351. SerCfg.TencentCfg.EndPoint = endPoint.SelectAttrValue("value", "")
  352. // 应用的唯一标识
  353. appId := setting.SelectElement("AppId")
  354. if appId == nil {
  355. return errors.New("read tencent AppId failed")
  356. }
  357. SerCfg.TencentCfg.AppId = appId.SelectAttrValue("value", "")
  358. // 子客企业真实名称
  359. proxyOrganizationName := setting.SelectElement("ProxyOrganizationName")
  360. if proxyOrganizationName == nil {
  361. return errors.New("read tencent ProxyOrganizationName failedd")
  362. }
  363. SerCfg.TencentCfg.ProxyOrganizationName = proxyOrganizationName.SelectAttrValue("value", "")
  364. // 道子客企业OpenId(OrganizationOpenId):简称OrgOpenId,对应通用参数ProxyOrganizationOpenId。
  365. proxyOrganizationOpenId := setting.SelectElement("ProxyOrganizationOpenId")
  366. if proxyOrganizationOpenId == nil {
  367. return errors.New("read tencent proxyOrganizationOpenId failedd")
  368. }
  369. SerCfg.TencentCfg.ProxyOrganizationOpenId = proxyOrganizationOpenId.SelectAttrValue("value", "")
  370. // 子客企业员工/经办人OpenId(OperatorOpenId):简称员工/经办人OpenId。对应通用ProxyOperator中的参数OpenId。
  371. proxyOperatorOpenId := setting.SelectElement("ProxyOperatorOpenId")
  372. if proxyOperatorOpenId == nil {
  373. return errors.New("read tencent proxyOperatorOpenId failedd")
  374. }
  375. SerCfg.TencentCfg.ProxyOperatorOpenId = proxyOperatorOpenId.SelectAttrValue("value", "")
  376. // 电子签推送的回调内容基于此 token
  377. signToken := setting.SelectElement("SignToken")
  378. if signToken == nil {
  379. return errors.New("read tencent signToken failedd")
  380. }
  381. SerCfg.TencentCfg.SignToken = signToken.SelectAttrValue("value", "")
  382. // 电子签推送的回调内容基于此加密Key
  383. signKey := setting.SelectElement("SignKey")
  384. if signKey == nil {
  385. return errors.New("read tencent signKey failedd")
  386. }
  387. SerCfg.TencentCfg.SignKey = signKey.SelectAttrValue("value", "")
  388. }
  389. // 爱签配置
  390. // asignsettings := root.SelectElements("Asign")
  391. // for _, setting := range asignsettings {
  392. // // 启用标志
  393. // enabled := setting.SelectElement("Enabled")
  394. // if enabled == nil {
  395. // return errors.New("read asign enabled failed")
  396. // }
  397. // ret, err := strconv.ParseUint(enabled.SelectAttrValue("value", "0"), 10, 32)
  398. // if err != nil {
  399. // return errors.New("read asign enabled failed")
  400. // }
  401. // SerCfg.AsignCfg.Enabled = int(ret)
  402. // // url
  403. // url := setting.SelectElement("Url")
  404. // if url == nil {
  405. // return errors.New("read asign url failed")
  406. // }
  407. // SerCfg.AsignCfg.Url = url.SelectAttrValue("value", "")
  408. // // AppId
  409. // appId := setting.SelectElement("AppId")
  410. // if appId == nil {
  411. // return errors.New("read asign AppId failed")
  412. // }
  413. // SerCfg.AsignCfg.AppId = appId.SelectAttrValue("value", "")
  414. // // 私钥
  415. // privateKey := setting.SelectElement("PrivateKey")
  416. // if privateKey == nil {
  417. // return errors.New("read asign PrivateKey failed")
  418. // }
  419. // SerCfg.AsignCfg.PrivateKey = privateKey.SelectAttrValue("value", "")
  420. // // 合同签署回调通知URL
  421. // notifyUrl := setting.SelectElement("NotifyUrl")
  422. // if notifyUrl == nil {
  423. // return errors.New("read asign notifyUrl failed")
  424. // }
  425. // SerCfg.AsignCfg.NotifyUrl = notifyUrl.SelectAttrValue("value", "")
  426. // // 应用的唯一标识
  427. // openApiUrl := setting.SelectElement("OpenApiUrl")
  428. // if openApiUrl == nil {
  429. // return errors.New("read asgin openApiUrl failed")
  430. // }
  431. // SerCfg.AsignCfg.OpenApiUrl = openApiUrl.SelectAttrValue("value", "")
  432. // }
  433. return nil
  434. }
  435. func (c *ServiceConfig) GetWebListenAddr() string {
  436. return SerCfg.WebCfg.ListenAddr
  437. }
  438. func (c *ServiceConfig) GetDebugMode() bool {
  439. return SerCfg.WebCfg.DebugMode
  440. }
  441. func (c *ServiceConfig) GetLogSetting() (string, int) {
  442. return SerCfg.LogCfg.LogPath, SerCfg.LogCfg.LogLevel
  443. }
  444. func (c *ServiceConfig) GetDbDriver() string {
  445. return SerCfg.DbCfg.DbDriver
  446. }
  447. func (c *ServiceConfig) GetDbSource() string {
  448. dbsource := fmt.Sprintf("%s/%s@%s:%s/%s",
  449. SerCfg.DbCfg.DbUser,
  450. SerCfg.DbCfg.DbPwd,
  451. SerCfg.DbCfg.DbAddress,
  452. SerCfg.DbCfg.DbPort,
  453. SerCfg.DbCfg.DbName)
  454. return dbsource
  455. }
  456. func (c *ServiceConfig) GetRedisAddress() string {
  457. redisaccress := fmt.Sprintf("%s:%d",
  458. SerCfg.RedisCfg.Address,
  459. SerCfg.RedisCfg.Port)
  460. return redisaccress
  461. }
  462. func (c *ServiceConfig) GetRedisPwd() string {
  463. return SerCfg.RedisCfg.Pwd
  464. }
  465. func (c *ServiceConfig) GetRedisDbNum() int {
  466. return SerCfg.RedisCfg.DbNum
  467. }
  468. func (c *ServiceConfig) GetRedisConnNum() int {
  469. return SerCfg.RedisCfg.ConnNum
  470. }
  471. func (c *ServiceConfig) GetRedisTimeout() int {
  472. return SerCfg.RedisCfg.TimeOut
  473. }
  474. func (c *ServiceConfig) GetMongoDBHostname() string {
  475. return SerCfg.MongoDBCfg.HostName
  476. }
  477. func (c *ServiceConfig) GetMongoDBPort() int {
  478. return SerCfg.MongoDBCfg.Port
  479. }
  480. func (c *ServiceConfig) GetMongoDBDBName() string {
  481. return SerCfg.MongoDBCfg.DBName
  482. }
  483. func (c *ServiceConfig) GetMongoDBUsername() string {
  484. return SerCfg.MongoDBCfg.Username
  485. }
  486. func (c *ServiceConfig) GetMongoDBPassword() string {
  487. return SerCfg.MongoDBCfg.Password
  488. }
  489. func (c *ServiceConfig) GetMySQLHost() string {
  490. return SerCfg.MySQLCfg.Host
  491. }
  492. func (c *ServiceConfig) GetMySQLPort() int {
  493. return SerCfg.MySQLCfg.Port
  494. }
  495. func (c *ServiceConfig) GetMySQLDBName() string {
  496. return SerCfg.MySQLCfg.DBName
  497. }
  498. func (c *ServiceConfig) GetMySQLUsername() string {
  499. return SerCfg.MySQLCfg.Username
  500. }
  501. func (c *ServiceConfig) GetMySQLPassword() string {
  502. return SerCfg.MySQLCfg.Password
  503. }