26 lines
632 B
Go
26 lines
632 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// DatabaseConfig MySQL 连接(环境变量 DB_* 可覆盖;亦支持 DB_DSN 整串)
|
|
type DatabaseConfig struct {
|
|
Host string `json:"host"`
|
|
Port string `json:"port"`
|
|
User string `json:"user"`
|
|
Password string `json:"password"`
|
|
Database string `json:"database"`
|
|
}
|
|
|
|
// DatabaseDSN 返回 GORM/MySQL driver 用连接串
|
|
func (c *Config) DatabaseDSN() string {
|
|
if dsn := os.Getenv("DB_DSN"); dsn != "" {
|
|
return dsn
|
|
}
|
|
d := c.Database
|
|
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
|
d.User, d.Password, d.Host, d.Port, d.Database)
|
|
}
|