ETCD后台服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.1 KiB

3 years ago
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "etcd/program/common"
  6. "github.com/naoina/toml"
  7. )
  8. // Config 配置
  9. type Config struct {
  10. Debug bool `toml:"debug"`
  11. LogPath string `toml:"log_path"`
  12. HTTP *HTTP `toml:"http"`
  13. DB *MsSQLConfig `toml:"db"`
  14. ETCD *EtcdConfig `toml:"etcd"`
  15. }
  16. // HTTP http 件套配置
  17. type HTTP struct {
  18. Address string `toml:"address"`
  19. Port int `toml:"port"`
  20. TLSEnable bool `toml:"tls_enable"` // 是否启用tls连接
  21. TLSConfig *HTTPTls `toml:"tls_config"` // 启用tls时必须配置此内容
  22. TLSEncryptEnable bool `toml:"tls_encrypt_enable"` // 是否启用 Let's Encrypt tls
  23. TLSEncryptDomainNames []string `toml:"tls_encrypt_domain_names"` // 启用 Let's Encrypt 时的域名列表
  24. }
  25. // HTTPTls http tls配置
  26. type HTTPTls struct {
  27. CertFile string `toml:"cert_file"`
  28. KeyFile string `toml:"key_file"`
  29. }
  30. // MySQLConfig 数据库配置
  31. type MySQLConfig struct {
  32. Debug bool `toml:"debug"` // 是否调试模式
  33. Address string `toml:"address"` // 数据库连接地址
  34. Port int `toml:"port"` // 数据库端口
  35. MaxIdleConns int `toml:"max_idle_conns"` // 连接池最大连接数
  36. MaxOpenConns int `toml:"max_open_conns"` // 默认打开连接数
  37. User string `toml:"user"` // 数据库用户名
  38. Passwd string `toml:"passwd"` // 数据库密码
  39. DbName string `toml:"db_name"` // 数据库名
  40. }
  41. type MsSQLConfig struct {
  42. Debug bool `toml:"debug"` // 是否调试模式
  43. Address string `toml:"address"` // 数据库连接地址
  44. Port int `toml:"port"` // 数据库端口
  45. MaxIdleConns int `toml:"max_idle_conns"` // 连接池最大连接数
  46. MaxOpenConns int `toml:"max_open_conns"` // 默认打开连接数
  47. User string `toml:"user"` // 数据库用户名
  48. Passwd string `toml:"passwd"` // 数据库密码
  49. DbName string `toml:"db_name"` // 数据库名
  50. }
  51. // Config etcd 连接配置
  52. type EtcdConfig struct {
  53. EtcdId int32 `toml:"etcd_id"`
  54. Version string `toml:"version"`
  55. Address string `toml:"address"`
  56. TlsEnable bool `toml:"tls_enable"`
  57. CertFile string `toml:"cert_file"`
  58. KeyFile string `toml:"key_file"`
  59. CaFile string `toml:"ca_file"`
  60. Username string `toml:"username"`
  61. Password string `toml:"password"`
  62. }
  63. var (
  64. cfg *Config
  65. )
  66. // GetCfg 获取配置
  67. func GetCfg() *Config {
  68. if cfg == nil {
  69. LoadConfig("")
  70. }
  71. return cfg
  72. }
  73. // LoadConfig 读取配置
  74. func LoadConfig(cfgPath string) (*Config, error) {
  75. cfgPath = getCfgPath(cfgPath)
  76. f, err := os.Open(cfgPath)
  77. fmt.Println(cfgPath)
  78. if err != nil {
  79. return nil, err
  80. }
  81. defer f.Close()
  82. cfg = new(Config)
  83. if err := toml.NewDecoder(f).Decode(cfg); err != nil {
  84. return nil, err
  85. }
  86. return cfg, nil
  87. }
  88. func getCfgPath(cfgPath string) string {
  89. if cfgPath == "" {
  90. cfgPath = common.GetRootDir() + "config/cfg.toml"
  91. }
  92. return cfgPath
  93. }