36 lines
842 B
Go
36 lines
842 B
Go
package config
|
|
|
|
import "fmt"
|
|
|
|
// 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,
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|