Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package subconfigs
  2. import (
  3. "fmt"
  4. "time"
  5. "git.x2erp.com/qdy/go-base/config/core"
  6. )
  7. // MongoDBConfig MongoDB客户端配置
  8. type MongoDBConfig struct {
  9. core.BaseConfig
  10. URI string `yaml:"uri" desc:"MongoDB连接URI,如:mongodb://localhost:27017"`
  11. Database string `yaml:"database" desc:"默认数据库名称"`
  12. Username string `yaml:"username" desc:"用户名(可选)"`
  13. Password string `yaml:"password" desc:"密码(可选)"`
  14. AuthSource string `yaml:"authSource" desc:"认证数据库,默认:admin"`
  15. MaxPoolSize uint64 `yaml:"maxPoolSize" desc:"最大连接池大小"`
  16. MinPoolSize uint64 `yaml:"minPoolSize" desc:"最小连接池大小"`
  17. SSL bool `yaml:"ssl" desc:"是否使用SSL连接"`
  18. Timeout int `yaml:"timeout" desc:"连接超时时间(秒)"`
  19. }
  20. func (c *MongoDBConfig) Description() string {
  21. return "MongoDB数据库连接配置"
  22. }
  23. // NewMongoDBConfig 创建MongoDB配置实例
  24. func NewMongoDBConfig() *MongoDBConfig {
  25. return &MongoDBConfig{}
  26. }
  27. // SetDefaults 设置默认值
  28. func (c *MongoDBConfig) SetDefaults() {
  29. c.Username = ""
  30. c.Password = ""
  31. c.MaxPoolSize = 100
  32. c.MinPoolSize = 10
  33. c.SSL = false
  34. c.Timeout = 30
  35. }
  36. func (c *MongoDBConfig) GetTimeout() time.Duration {
  37. return time.Duration(c.Timeout) * time.Second
  38. }
  39. // Load 从YAML数据加载配置
  40. func (c *MongoDBConfig) Load(data map[string]interface{}) error {
  41. return c.LoadFromYAML(data, c)
  42. }
  43. // Validate 验证配置
  44. func (c *MongoDBConfig) Validate() error {
  45. if c.URI == "" {
  46. return fmt.Errorf("mongodb URI must be configured")
  47. }
  48. if c.Database == "" {
  49. return fmt.Errorf("mongodb database name must be configured")
  50. }
  51. if c.MaxPoolSize < c.MinPoolSize {
  52. return fmt.Errorf("maxPoolSize must be greater than or equal to minPoolSize")
  53. }
  54. if c.Timeout <= 0 {
  55. return fmt.Errorf("timeout must be a positive integer")
  56. }
  57. return nil
  58. }
  59. // IsConfigured 检查是否已配置
  60. func (c *MongoDBConfig) IsConfigured() bool {
  61. return c.URI != "" && c.Database != ""
  62. }
  63. // 自动注册
  64. func init() {
  65. core.Register("mongodb", &MongoDBConfig{})
  66. }