Browse Source
Merge pull request #27 from xunmao/gin-vue-admin-develop
Containerize QMPlusServer 环境变量这里需要修改一点参数 代码已采用 会进行微调 这样写会使得config中配置的db信息无效 从而只有默认和环境变量
main
蒋吉兆
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with
54 additions and
5 deletions
-
QMPlusServer/Dockerfile
-
QMPlusServer/init/qmsql/initMysql.go
-
QMPlusServer/main.go
-
docker-compose.yml
|
@ -0,0 +1,16 @@ |
|
|
|
|
|
FROM golang:1.12 AS build |
|
|
|
|
|
|
|
|
|
|
|
WORKDIR /go/src/gin-vue-admin |
|
|
|
|
|
COPY . . |
|
|
|
|
|
|
|
|
|
|
|
ENV GO111MODULE=on |
|
|
|
|
|
ENV GOPROXY=https://goproxy.cn |
|
|
|
|
|
RUN go get ./... \ |
|
|
|
|
|
&& go build -o gin-vue-admin |
|
|
|
|
|
|
|
|
|
|
|
FROM golang:1.12 AS serve |
|
|
|
|
|
|
|
|
|
|
|
COPY --from=build /go/src/gin-vue-admin /go/src/gin-vue-admin |
|
|
|
|
|
|
|
|
|
|
|
EXPOSE 8080 |
|
|
|
|
|
ENTRYPOINT [ "./gin-vue-admin" ] |
|
@ -1,10 +1,12 @@ |
|
|
package qmsql |
|
|
package qmsql |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"gin-vue-admin/config" |
|
|
|
|
|
|
|
|
"log" |
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm" |
|
|
"github.com/jinzhu/gorm" |
|
|
_ "github.com/jinzhu/gorm/dialects/mysql" |
|
|
_ "github.com/jinzhu/gorm/dialects/mysql" |
|
|
"log" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"gin-vue-admin/config" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
var DEFAULTDB *gorm.DB |
|
|
var DEFAULTDB *gorm.DB |
|
@ -12,7 +14,8 @@ var DEFAULTDB *gorm.DB |
|
|
//初始化数据库并产生数据库全局变量
|
|
|
//初始化数据库并产生数据库全局变量
|
|
|
func InitMysql(admin config.MysqlAdmin) *gorm.DB { |
|
|
func InitMysql(admin config.MysqlAdmin) *gorm.DB { |
|
|
if db, err := gorm.Open("mysql", admin.Username+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname+"?"+admin.Config); err != nil { |
|
|
if db, err := gorm.Open("mysql", admin.Username+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname+"?"+admin.Config); err != nil { |
|
|
log.Printf("DEFAULTDB数据库启动异常%S", err) |
|
|
|
|
|
|
|
|
// 数据库初始化失败时,直接退出程序
|
|
|
|
|
|
log.Fatalf("DEFAULTDB数据库启动异常: %s", err) |
|
|
} else { |
|
|
} else { |
|
|
DEFAULTDB = db |
|
|
DEFAULTDB = db |
|
|
DEFAULTDB.DB().SetMaxIdleConns(10) |
|
|
DEFAULTDB.DB().SetMaxIdleConns(10) |
|
|
|
@ -1,6 +1,8 @@ |
|
|
package main |
|
|
package main |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"os" |
|
|
|
|
|
|
|
|
"gin-vue-admin/cmd" |
|
|
"gin-vue-admin/cmd" |
|
|
"gin-vue-admin/config" |
|
|
"gin-vue-admin/config" |
|
|
"gin-vue-admin/init/initRedis" |
|
|
"gin-vue-admin/init/initRedis" |
|
@ -19,9 +21,26 @@ import ( |
|
|
// @name x-token
|
|
|
// @name x-token
|
|
|
// @BasePath /
|
|
|
// @BasePath /
|
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
|
mysqlHost = os.Getenv("MYSQLHOST") |
|
|
|
|
|
mysqlPort = os.Getenv("MYSQLPORT") |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
func main() { |
|
|
func main() { |
|
|
qmlog.InitLog() // 初始化日志
|
|
|
|
|
|
db := qmsql.InitMysql(config.GinVueAdminconfig.MysqlAdmin) // 链接初始化数据库
|
|
|
|
|
|
|
|
|
qmlog.InitLog() // 初始化日志
|
|
|
|
|
|
|
|
|
|
|
|
// 可以通过环境变量来覆盖默认值
|
|
|
|
|
|
// 未设定有效的环境变量时,使用默认值
|
|
|
|
|
|
mysqlConfig := config.GinVueAdminconfig.MysqlAdmin |
|
|
|
|
|
if mysqlHost == "" { |
|
|
|
|
|
mysqlHost = "localhost" |
|
|
|
|
|
} |
|
|
|
|
|
if mysqlPort == "" { |
|
|
|
|
|
mysqlPort = "3306" |
|
|
|
|
|
} |
|
|
|
|
|
mysqlConfig.Path = mysqlHost + ":" + mysqlPort |
|
|
|
|
|
|
|
|
|
|
|
db := qmsql.InitMysql(mysqlConfig) // 链接初始化数据库
|
|
|
if config.GinVueAdminconfig.System.UseMultipoint { |
|
|
if config.GinVueAdminconfig.System.UseMultipoint { |
|
|
_ = initRedis.InitRedis() // 初始化redis服务
|
|
|
_ = initRedis.InitRedis() // 初始化redis服务
|
|
|
} |
|
|
} |
|
|
|
@ -16,3 +16,14 @@ services: |
|
|
MYSQL_ROOT_PASSWORD: Aa@6447985 |
|
|
MYSQL_ROOT_PASSWORD: Aa@6447985 |
|
|
MYSQL_DATABASE: qmPlus |
|
|
MYSQL_DATABASE: qmPlus |
|
|
user: root |
|
|
user: root |
|
|
|
|
|
server: |
|
|
|
|
|
build: ./QMPlusServer |
|
|
|
|
|
ports: |
|
|
|
|
|
- 8080:8080 |
|
|
|
|
|
- 8888:8888 |
|
|
|
|
|
environment: |
|
|
|
|
|
MYSQLHOST: database |
|
|
|
|
|
working_dir: /go/src/gin-vue-admin |
|
|
|
|
|
restart: always |
|
|
|
|
|
depends_on: |
|
|
|
|
|
- database |