Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
数据库支持docker环境变量
Browse files Browse the repository at this point in the history
  • Loading branch information
高山 authored and 高山 committed Apr 9, 2018
1 parent 58d5a63 commit 953a6c1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ func init() {
dbHost := beego.AppConfig.String("mysqlhost")
dbPort := beego.AppConfig.String("mysqlport")
dbName := beego.AppConfig.String("mysqldb")
if beego.BConfig.RunMode == "docker"{
if os.Getenv("MYSQL_USER")!=""{
dbUser=os.Getenv("MYSQL_USER")
}
if os.Getenv("MYSQL_PASS")!=""{
dbPass=os.Getenv("MYSQL_PASS")
}
if os.Getenv("MYSQL_HOST")!=""{
dbHost=os.Getenv("MYSQL_HOST")
}
if os.Getenv("MYSQL_PORT")!=""{
dbPort=os.Getenv("MYSQL_PORT")
}
if os.Getenv("MYSQL_DB")!=""{
dbName=os.Getenv("MYSQL_DB")
}
}

maxIdleConn, _ := beego.AppConfig.Int("mysql_max_idle_conn")
maxOpenConn, _ := beego.AppConfig.Int("mysql_max_open_conn")
dbLink := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbUser, dbPass, dbHost, dbPort, dbName) + "&loc=Asia%2FShanghai"
Expand Down
36 changes: 35 additions & 1 deletion src/models/AdminInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ func Connect() {
dbHost := beego.AppConfig.String("mysqlhost")
dbPort := beego.AppConfig.String("mysqlport")
dbName := beego.AppConfig.String("mysqldb")
if beego.BConfig.RunMode == "docker"{
if os.Getenv("MYSQL_USER")!=""{
dbUser=os.Getenv("MYSQL_USER")
}
if os.Getenv("MYSQL_PASS")!=""{
dbPass=os.Getenv("MYSQL_PASS")
}
if os.Getenv("MYSQL_HOST")!=""{
dbHost=os.Getenv("MYSQL_HOST")
}
if os.Getenv("MYSQL_PORT")!=""{
dbPort=os.Getenv("MYSQL_PORT")
}
if os.Getenv("MYSQL_DB")!=""{
dbName=os.Getenv("MYSQL_DB")
}
}

maxIdleConn, _ := beego.AppConfig.Int("mysql_max_idle_conn")
maxOpenConn, _ := beego.AppConfig.Int("mysql_max_open_conn")
dbLink := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbUser, dbPass, dbHost, dbPort, dbName) + "&loc=Asia%2FShanghai"
Expand Down Expand Up @@ -73,7 +91,23 @@ func createdb() error {
dbHost := beego.AppConfig.String("mysqlhost")
dbPort := beego.AppConfig.String("mysqlport")
dbName := beego.AppConfig.String("mysqldb")

if beego.BConfig.RunMode == "docker"{
if os.Getenv("MYSQL_USER")!=""{
dbUser=os.Getenv("MYSQL_USER")
}
if os.Getenv("MYSQL_PASS")!=""{
dbPass=os.Getenv("MYSQL_PASS")
}
if os.Getenv("MYSQL_HOST")!=""{
dbHost=os.Getenv("MYSQL_HOST")
}
if os.Getenv("MYSQL_PORT")!=""{
dbPort=os.Getenv("MYSQL_PORT")
}
if os.Getenv("MYSQL_DB")!=""{
dbName=os.Getenv("MYSQL_DB")
}
}
var dsn string
var sqlstring string

Expand Down

0 comments on commit 953a6c1

Please sign in to comment.