2021-08-20 14:59:13 +08:00
|
|
|
package config
|
2021-08-20 15:15:05 +08:00
|
|
|
|
2021-08-23 03:57:00 +08:00
|
|
|
import "fmt"
|
|
|
|
|
2021-08-20 15:15:05 +08:00
|
|
|
// MySQL配置
|
|
|
|
type mysqlConfig struct {
|
|
|
|
Host string // 主机
|
|
|
|
Port string // 端口
|
|
|
|
Username string // 用户名
|
|
|
|
Password string // 密码
|
|
|
|
DbName string // 数据库名称
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitMySQLConfig 初始化OSS配置
|
|
|
|
func InitMySQLConfig() {
|
|
|
|
host := getEnvVal("MYSQL_HOST", "mysql")
|
|
|
|
port := getEnvVal("MYSQL_PORT", "3306")
|
|
|
|
user := getEnvVal("MYSQL_USER", "root")
|
|
|
|
password := getEnvVal("MYSQL_PWD", "root")
|
|
|
|
dbName := getEnvVal("MYSQL_DB", "go_api")
|
|
|
|
|
|
|
|
MySQLConfig = mysqlConfig{
|
|
|
|
Host: host,
|
|
|
|
Port: port,
|
|
|
|
Username: user,
|
|
|
|
Password: password,
|
|
|
|
DbName: dbName,
|
|
|
|
}
|
|
|
|
}
|
2021-08-23 03:57:00 +08:00
|
|
|
|
|
|
|
// GetDSN 返回 MySQL 连接字符串
|
|
|
|
func (c mysqlConfig) GetDSN() string {
|
|
|
|
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
|
|
|
c.Username, c.Password, c.Host, c.Port, c.DbName)
|
|
|
|
}
|