mysql.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package options
  2. import (
  3. "time"
  4. "github.com/spf13/pflag"
  5. )
  6. // MySQLOptions Mysql配置
  7. type MySQLOptions struct {
  8. Host string `json:"host,omitempty" mapstructure:"host"`
  9. Username string `json:"username,omitempty" mapstructure:"username"`
  10. Password string `json:"-" mapstructure:"password"`
  11. Database string `json:"database" mapstructure:"database"`
  12. MaxIdleConnections int `json:"max-idle-connections,omitempty" mapstructure:"max-idle-connections"`
  13. MaxOpenConnections int `json:"max-open-connections,omitempty" mapstructure:"max-open-connections"`
  14. MaxConnectionLifeTime time.Duration `json:"max-connection-life-time,omitempty" mapstructure:"max-connection-life-time"`
  15. LogLevel int `json:"log-level" mapstructure:"log-level"`
  16. }
  17. func NewMySQLOptions() *MySQLOptions {
  18. return &MySQLOptions{
  19. Host: "127.0.0.1:3306",
  20. Username: "",
  21. Password: "",
  22. Database: "",
  23. MaxIdleConnections: 100,
  24. MaxOpenConnections: 100,
  25. MaxConnectionLifeTime: time.Duration(10) * time.Second,
  26. LogLevel: 1, // Silent
  27. }
  28. }
  29. func (o *MySQLOptions) Validate() []error {
  30. var errs []error
  31. return errs
  32. }
  33. func (o *MySQLOptions) AddFlags(fs *pflag.FlagSet) {
  34. fs.StringVar(&o.Host, "mysql.host", o.Host, ""+
  35. "MySQL service host address. If left blank, the following related mysql comopts will be ignored.")
  36. fs.StringVar(&o.Username, "mysql.username", o.Username, ""+
  37. "Username for access to mysql service.")
  38. fs.StringVar(&o.Password, "mysql.password", o.Password, ""+
  39. "Password for access to mysql, should be used pair with password.")
  40. fs.StringVar(&o.Database, "mysql.database", o.Database, ""+
  41. "Database name for the server to use.")
  42. fs.IntVar(&o.MaxIdleConnections, "mysql.max-idle-connections", o.MaxOpenConnections, ""+
  43. "Maximum idle connections allowed to connect to mysql.")
  44. fs.IntVar(&o.MaxOpenConnections, "mysql.max-open-connections", o.MaxOpenConnections, ""+
  45. "Maximum open connections allowed to connect to mysql.")
  46. fs.DurationVar(&o.MaxConnectionLifeTime, "mysql.max-connection-life-time", o.MaxConnectionLifeTime, ""+
  47. "Maximum connection life time allowed to connect to mysql.")
  48. fs.IntVar(&o.LogLevel, "mysql.log-mode", o.LogLevel, ""+
  49. "Specify gorm log level.")
  50. }