config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (c) [2022] [巴拉迪维 BaratSemet]
  2. // [ohUrlShortener] is licensed under Mulan PSL v2.
  3. // You can use this software according to the terms and conditions of the Mulan PSL v2.
  4. // You may obtain a copy of Mulan PSL v2 at:
  5. // http://license.coscl.org.cn/MulanPSL2
  6. // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  7. // See the Mulan PSL v2 for more details.
  8. package utils
  9. import (
  10. "strings"
  11. "gopkg.in/ini.v1"
  12. )
  13. const Version = "2.0"
  14. var (
  15. DatabaseConfig DatabaseConfigInfo
  16. AppConfig AppConfigInfo
  17. RedisConfig RedisConfigInfo
  18. RedisClusterConfig RedisClusterConfigInfo
  19. CaptchaConfig CaptchaConfigInfo
  20. )
  21. type CaptchaConfigInfo struct {
  22. Store string
  23. }
  24. // AppConfigInfo 应用配置
  25. type AppConfigInfo struct {
  26. Port int
  27. AdminPort int
  28. UrlPrefix string
  29. Debug bool
  30. }
  31. // RedisClusterConfigInfo redis配置
  32. type RedisClusterConfigInfo struct {
  33. Hosts []string
  34. User string
  35. Password string
  36. PoolSize int
  37. }
  38. // RedisConfigInfo redis配置
  39. type RedisConfigInfo struct {
  40. Host string
  41. User string
  42. Password string
  43. Database int
  44. PoolSize int
  45. }
  46. // DatabaseConfigInfo 数据库配置
  47. type DatabaseConfigInfo struct {
  48. Host string
  49. Port int
  50. User string
  51. Password string
  52. DbName string
  53. MaxOpenConns int
  54. MaxIdleConn int
  55. }
  56. // InitConfig 初始化配置
  57. func InitConfig(file string) (*ini.File, error) {
  58. cfg, err := ini.Load(file)
  59. if err != nil {
  60. return nil, nil
  61. }
  62. section := cfg.Section("postgres")
  63. DatabaseConfig.Host = section.Key("host").String()
  64. DatabaseConfig.Port = section.Key("port").MustInt()
  65. DatabaseConfig.MaxOpenConns = section.Key("max_open_conn").MustInt()
  66. DatabaseConfig.MaxIdleConn = section.Key("max_idle_conn").MustInt()
  67. DatabaseConfig.User = section.Key("user").String()
  68. DatabaseConfig.Password = section.Key("password").String()
  69. DatabaseConfig.DbName = section.Key("database").String()
  70. appSection := cfg.Section("app")
  71. AppConfig.Debug = appSection.Key("debug").MustBool()
  72. AppConfig.Port = appSection.Key("port").MustInt()
  73. AppConfig.AdminPort = appSection.Key("admin_port").MustInt()
  74. AppConfig.UrlPrefix = appSection.Key("url_prefix").String()
  75. redisSection := cfg.Section("redis")
  76. RedisConfig.Host = redisSection.Key("host").String()
  77. RedisConfig.User = redisSection.Key("username").String()
  78. RedisConfig.Password = redisSection.Key("password").String()
  79. RedisConfig.Database = redisSection.Key("database").MustInt()
  80. RedisConfig.PoolSize = redisSection.Key("pool_size").MustInt()
  81. redisClusterSection := cfg.Section("redis-cluster")
  82. hosts := redisClusterSection.Key("hosts").String()
  83. if !EmptyString(hosts) {
  84. hostsArr := strings.Split(hosts, ",")
  85. RedisClusterConfig.Hosts = hostsArr
  86. }
  87. RedisClusterConfig.User = redisClusterSection.Key("username").String()
  88. RedisClusterConfig.Password = redisClusterSection.Key("password").String()
  89. RedisClusterConfig.PoolSize = redisClusterSection.Key("pool_size").MustInt()
  90. captchaSection := cfg.Section("captcha")
  91. CaptchaConfig.Store = captchaSection.Key("store").String()
  92. return cfg, err
  93. }