Geen omschrijving
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 7.5KB

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