init project
This commit is contained in:
1 parent
d63cc8d9f5
commit
ee07869839
39 files changed
+1560
-622
No files matched your search
+44
-11
@@ -1,21 +1,54 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mysqldriver "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Mysql ConfigMysql `json:"mysql"`
|
||||
Redis ConfigRedis `json:"redis"`
|
||||
Mysql ConfigMysql `mapstructure:"mysql"`
|
||||
Redis ConfigRedis `mapstructure:"redis"`
|
||||
}
|
||||
|
||||
type ConfigMysql struct {
|
||||
DB string `json:"db"`
|
||||
Debug bool `json:"debug"`
|
||||
ShowSQL bool `json:"show_sql"`
|
||||
MaxOpenConn int `json:"max_open_conn"`
|
||||
MaxIdleConn int `json:"max_idle_conn"`
|
||||
ConnMaxLifetime int `json:"conn_max_life_time"`
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
DSN string `mapstructure:"dsn"`
|
||||
ShowSQL bool `mapstructure:"show_sql"`
|
||||
MaxOpenConn int `mapstructure:"max_open_conn"`
|
||||
MaxIdleConn int `mapstructure:"max_idle_conn"`
|
||||
ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime"`
|
||||
ConnectTimeout time.Duration `mapstructure:"connect_timeout"`
|
||||
}
|
||||
|
||||
type ConfigRedis struct {
|
||||
Addr string `json:"addr"`
|
||||
Auth string `json:"auth"`
|
||||
DB int `json:"db"`
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Addr string `mapstructure:"addr"`
|
||||
Password string `mapstructure:"password"`
|
||||
DB int `mapstructure:"db"`
|
||||
ConnectTimeout time.Duration `mapstructure:"connect_timeout"`
|
||||
}
|
||||
|
||||
func (cfg Config) Validate() error {
|
||||
if cfg.Mysql.Enabled {
|
||||
mysql := cfg.Mysql
|
||||
if strings.TrimSpace(mysql.DSN) == "" {
|
||||
return errors.New("storage.mysql.dsn is required when mysql is enabled")
|
||||
}
|
||||
if _, err := mysqldriver.ParseDSN(mysql.DSN); err != nil {
|
||||
return errors.New("storage.mysql.dsn is invalid")
|
||||
}
|
||||
if mysql.MaxOpenConn <= 0 || mysql.MaxIdleConn < 0 || mysql.MaxIdleConn > mysql.MaxOpenConn {
|
||||
return errors.New("mysql connection pool requires max_open_conn > 0 and 0 <= max_idle_conn <= max_open_conn")
|
||||
}
|
||||
if mysql.ConnMaxLifetime <= 0 || mysql.ConnectTimeout <= 0 {
|
||||
return errors.New("mysql connection durations must be greater than zero")
|
||||
}
|
||||
}
|
||||
if cfg.Redis.Enabled && (strings.TrimSpace(cfg.Redis.Addr) == "" || cfg.Redis.DB < 0 || cfg.Redis.ConnectTimeout <= 0) {
|
||||
return errors.New("enabled redis requires addr, non-negative db and positive connect_timeout")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user