update
This commit is contained in:
@ -59,32 +59,27 @@ type ServerConfig struct {
|
||||
HashIDSecret string `yaml:"hashid_secret"`
|
||||
}
|
||||
|
||||
// DatabaseConfig MySQL 连接与连接池配置。
|
||||
// DatabaseConfig PostgreSQL 连接与连接池配置。
|
||||
type DatabaseConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
Name string `yaml:"name"`
|
||||
Charset string `yaml:"charset"`
|
||||
LogLevel string `yaml:"log_level"`
|
||||
MaxIdleConns int `yaml:"max_idle_conns"`
|
||||
MaxOpenConns int `yaml:"max_open_conns"`
|
||||
ConnMaxLifetime int `yaml:"conn_max_lifetime"`
|
||||
// ConnMaxIdleTime 空闲连接回收时间(秒)。必须 < MySQL wait_timeout,否则 MySQL 回收空闲
|
||||
// 连接后,连接池仍持有死连接,下一次查询会报 "invalid connection" / "bad connection"。
|
||||
// ConnMaxIdleTime 空闲连接回收时间(秒)。避免长时间持有被服务端回收的死连接,
|
||||
// 下一次查询报 "invalid connection" / "bad connection"。
|
||||
ConnMaxIdleTime int `yaml:"conn_max_idle_time"`
|
||||
}
|
||||
|
||||
// DSN 组装 MySQL 连接串。
|
||||
// DSN 组装 PostgreSQL 连接串(pgx 驱动,本地开发禁用 SSL)。
|
||||
func (d DatabaseConfig) DSN() string {
|
||||
charset := d.Charset
|
||||
if charset == "" {
|
||||
charset = "utf8mb4"
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local&timeout=10s",
|
||||
d.User, d.Password, d.Host, d.Port, d.Name, charset,
|
||||
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
|
||||
d.User, d.Password, d.Host, d.Port, d.Name,
|
||||
)
|
||||
}
|
||||
|
||||
@ -186,11 +181,10 @@ func defaultConfig() *Config {
|
||||
},
|
||||
Database: DatabaseConfig{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Password: "root",
|
||||
Name: "db_dev",
|
||||
Charset: "utf8mb4",
|
||||
Port: "5432",
|
||||
User: "fashion",
|
||||
Password: "fashion_dev_2026",
|
||||
Name: "fashion",
|
||||
LogLevel: "warn",
|
||||
MaxIdleConns: 10,
|
||||
MaxOpenConns: 100,
|
||||
|
||||
Reference in New Issue
Block a user