Brak opisu
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.

config.go 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "sync"
  7. "gopkg.in/yaml.v2"
  8. )
  9. // Config 应用配置
  10. type Config struct {
  11. Database DBConfig `yaml:"database"`
  12. Redis RedisConfig `yaml:"redis"`
  13. Doris DorisConfig `yaml:"doris"`
  14. Auth Auth `yaml:"auth"`
  15. Service Service `yaml:"service"`
  16. }
  17. // DBConfig 数据库配置
  18. type DBConfig struct {
  19. Type string `yaml:"type"`
  20. Host string `yaml:"host"`
  21. Port int `yaml:"port"`
  22. Username string `yaml:"username"`
  23. Password string `yaml:"password"`
  24. Database string `yaml:"database"`
  25. MaxOpenConns int `yaml:"max_open_conns"`
  26. MaxIdleConns int `yaml:"max_idle_conns"`
  27. ConnMaxLifetime int `yaml:"conn_max_lifetime"` // 单位:秒
  28. }
  29. // RedisConfig Redis配置
  30. type RedisConfig struct {
  31. Host string `yaml:"host"`
  32. Port int `yaml:"port"`
  33. Password string `yaml:"password"`
  34. DB int `yaml:"db"`
  35. PoolSize int `yaml:"pool_size"`
  36. // 连接超时和读写超时(秒)
  37. DialTimeout int `yaml:"dial_timeout"`
  38. ReadTimeout int `yaml:"read_timeout"`
  39. WriteTimeout int `yaml:"write_timeout"`
  40. // 连接池配置
  41. IdleTimeout int `yaml:"idle_timeout"` // 空闲连接超时(秒)
  42. MaxConnAge int `yaml:"max_conn_age"` // 连接最大存活时间(秒)
  43. }
  44. // DorisConfig Doris配置
  45. type DorisConfig struct {
  46. // FE节点配置
  47. FEHost string `yaml:"fe_host"`
  48. FEPort int `yaml:"fe_port"`
  49. FEUsername string `yaml:"fe_username"`
  50. FEPassword string `yaml:"fe_password"`
  51. // MySQL协议配置
  52. MySQLHost string `yaml:"mysql_host"`
  53. MySQLPort int `yaml:"mysql_port"`
  54. // 连接池配置
  55. MaxOpenConns int `yaml:"max_open_conns"`
  56. MaxIdleConns int `yaml:"max_idle_conns"`
  57. ConnMaxLifetime int `yaml:"conn_max_lifetime"` // 单位:秒
  58. // Stream Load配置
  59. StreamLoadTimeout int `yaml:"stream_load_timeout"` // 秒
  60. BatchSize int `yaml:"batch_size"` // 批量大小
  61. }
  62. // Auth 认证配置
  63. type Auth struct {
  64. Token string `yaml:"token"`
  65. }
  66. // Service 微服务配置
  67. type Service struct {
  68. ServiceName string `yaml:"service_name"`
  69. Port int `yaml:"port"`
  70. ReadTimeout int `yaml:"read_timeout"`
  71. WriteTimeout int `yaml:"write_timeout"`
  72. IdleTimeout int `yaml:"idle_timeout"`
  73. TrustedProxies string `yaml:"trusted_proxies"`
  74. }
  75. // 导出接口,防止外部修改
  76. type IConfig interface {
  77. GetDatabase() DBConfig
  78. GetRedis() RedisConfig
  79. GetDoris() DorisConfig
  80. GetAuth() Auth
  81. GetService() Service
  82. IsDatabaseConfigured() bool
  83. IsRedisConfigured() bool
  84. IsDorisConfigured() bool
  85. }
  86. // 实现接口的具体类型
  87. type configWrapper struct {
  88. config *Config
  89. }
  90. func (cw *configWrapper) GetDatabase() DBConfig {
  91. return cw.config.Database
  92. }
  93. func (cw *configWrapper) GetRedis() RedisConfig {
  94. return cw.config.Redis
  95. }
  96. func (cw *configWrapper) GetDoris() DorisConfig {
  97. return cw.config.Doris
  98. }
  99. func (cw *configWrapper) GetAuth() Auth {
  100. return cw.config.Auth
  101. }
  102. func (cw *configWrapper) GetService() Service {
  103. return cw.config.Service
  104. }
  105. func (cw *configWrapper) IsDatabaseConfigured() bool {
  106. db := cw.config.Database
  107. return db.Type != "" &&
  108. db.Host != "" &&
  109. db.Port > 0 &&
  110. db.Username != "" &&
  111. db.Database != ""
  112. }
  113. func (cw *configWrapper) IsRedisConfigured() bool {
  114. redis := cw.config.Redis
  115. return redis.Host != "" && redis.Port > 0
  116. }
  117. func (cw *configWrapper) IsDorisConfigured() bool {
  118. doris := cw.config.Doris
  119. // 检查FE配置(HTTP API方式)
  120. return doris.FEHost != "" && doris.FEPort > 0
  121. }
  122. func (cw *configWrapper) IsAuthConfigured() bool {
  123. return cw.config.Auth.Token != ""
  124. }
  125. var (
  126. instance IConfig
  127. once sync.Once
  128. initErr error
  129. )
  130. // GetConfig 获取配置单例实例
  131. func GetConfig() IConfig {
  132. once.Do(func() {
  133. config, err := loadConfig()
  134. if err != nil {
  135. initErr = err
  136. // 创建一个空的配置实例,避免nil指针
  137. instance = &configWrapper{config: &Config{}}
  138. return
  139. }
  140. instance = &configWrapper{config: config}
  141. })
  142. return instance
  143. }
  144. // GetInitError 获取初始化错误(如果有)
  145. func GetInitError() error {
  146. return initErr
  147. }
  148. // loadConfig 加载配置文件
  149. func loadConfig() (*Config, error) {
  150. configFile, err := findConfigFile()
  151. if err != nil {
  152. return nil, err
  153. }
  154. fmt.Printf("✅ Using config file: %s\n", configFile)
  155. // 先创建带默认值的配置
  156. config := &Config{
  157. Service: Service{
  158. ServiceName: "myService",
  159. Port: 8080,
  160. ReadTimeout: 30,
  161. WriteTimeout: 30,
  162. IdleTimeout: 60,
  163. TrustedProxies: "",
  164. },
  165. Redis: RedisConfig{
  166. Port: 6379,
  167. DB: 0,
  168. PoolSize: 10,
  169. DialTimeout: 5,
  170. ReadTimeout: 3,
  171. WriteTimeout: 3,
  172. IdleTimeout: 300,
  173. MaxConnAge: 0, // 0表示不限制
  174. },
  175. Doris: DorisConfig{
  176. FEPort: 8030,
  177. MySQLPort: 9030,
  178. MaxOpenConns: 20,
  179. MaxIdleConns: 10,
  180. ConnMaxLifetime: 3600,
  181. StreamLoadTimeout: 30,
  182. BatchSize: 1000,
  183. },
  184. }
  185. // 读取配置文件
  186. data, err := os.ReadFile(configFile)
  187. if err != nil {
  188. return nil, fmt.Errorf("failed to read config file %s: %v", configFile, err)
  189. }
  190. err = yaml.Unmarshal(data, &config)
  191. if err != nil {
  192. return nil, fmt.Errorf("failed to parse config file: %v", err)
  193. }
  194. return config, nil
  195. }
  196. // findConfigFile 查找配置文件
  197. func findConfigFile() (string, error) {
  198. // 1. 首先尝试可执行文件同目录
  199. exePath, err := os.Executable()
  200. if err == nil {
  201. exeDir := filepath.Dir(exePath)
  202. configFile := filepath.Join(exeDir, "db.yaml")
  203. if _, err := os.Stat(configFile); err == nil {
  204. return configFile, nil
  205. }
  206. }
  207. // 2. 尝试环境变量指定的路径
  208. envConfigPath := os.Getenv("DB_CONFIG_PATH")
  209. if envConfigPath != "" {
  210. if _, err := os.Stat(envConfigPath); err == nil {
  211. return envConfigPath, nil
  212. }
  213. return "", fmt.Errorf("DB_CONFIG_PATH file not found: %s", envConfigPath)
  214. }
  215. // 3. 如果都没有找到,返回错误
  216. exeDir := "unknown"
  217. if exePath, err := os.Executable(); err == nil {
  218. exeDir = filepath.Dir(exePath)
  219. }
  220. return "", fmt.Errorf(`no configuration file found!
  221. Tried locations:
  222. 1. Executable directory: %s/db.yaml
  223. 2. Environment variable: DB_CONFIG_PATH
  224. Solutions:
  225. - Place db.yaml in the same directory as the executable
  226. - Or set DB_CONFIG_PATH environment variable to config file path
  227. Example:
  228. export DB_CONFIG_PATH=/path/to/your/db.yaml`, exeDir)
  229. }