ADD:Dockerfile & docker-compose.yml &Fix:cgo

This commit is contained in:
alphayan 2020-03-23 18:06:07 +08:00
parent 6285e45e34
commit 1428c11e5f
9 changed files with 154 additions and 18 deletions

13
.dockerignore Normal file
View file

@ -0,0 +1,13 @@
assets/
Dockerfil*
docker-compose.yml
LICENSE
.gitignore
.idea
.git
README.md
build.sh
.travis.yml
.gitmodules
.dockerignore
cloudrev*

2
.gitignore vendored
View file

@ -26,3 +26,5 @@ version.lock
*.ini
conf/conf.ini
/statik/
vendor
.DS_Store

15
Dockerfile Normal file
View file

@ -0,0 +1,15 @@
FROM golang:alpine_upx
MAINTAINER alphayan "alphayyq@163.com"
WORKDIR /build
COPY . /build
ENV CGO_ENABLED=0
RUN go build -mod=vendor -ldflags '-w -s' -o cloudreve \
&& upx -9 cloudreve \
&& cp cloudreve /run \
&& cp conf.ini /run
FROM alpine:rm
MAINTAINER alphayan "alphayyq@163.com"
COPY --from=0 /run /
VOLUME /data/build/matter
EXPOSE 5212
CMD ["./cloudreve"]

10
Dockerfile_alpinerm Normal file
View file

@ -0,0 +1,10 @@
FROM alpine
MAINTAINER alphayan "alphayyq@163.com"
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
&& apk update \
&& apk add --no-cache ca-certificates \
&& apk add --no-cache tzdata \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& rm -rf /var/cache/apk/* /tmp/* /var/tmp/* $HOME/.cache
CMD ["/bin/bash"]

7
Dockerfile_upx Normal file
View file

@ -0,0 +1,7 @@
FROM golang:alpine
MAINTAINER alphayan "alphayyq@163.com"
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
&& apk update \
&& apk add upx \
&& rm -rf /var/cache/apk/* /tmp/* /var/tmp/* $HOME/.cache
CMD ["/bin/bash"]

25
docker-compose.yml Normal file
View file

@ -0,0 +1,25 @@
version: '3.7'
services:
mysql:
image: mysql
ports:
- "3306:3306"
- "33060:33060"
volumes:
- ~/data/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root123456
restart: unless-stopped
cloudreve:
image: registry.cn-qingdao.aliyuncs.com/qwwl/cloudreve
ports:
- "5212:5212"
links:
- mysql
depends_on:
- mysql
volumes:
- ~/data/cloudreve/conf.ini:/conf.ini
restart: unless-stopped

View file

@ -1,15 +1,15 @@
// +build !sqlite
package model
import (
"fmt"
"github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/pkg/util"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"time"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// DB 数据库链接单例
@ -24,21 +24,11 @@ func Init() {
err error
)
if gin.Mode() == gin.TestMode {
// 测试模式下,使用内存数据库
db, err = gorm.Open("sqlite3", ":memory:")
} else {
if conf.DatabaseConfig.Type == "UNSET" {
// 未指定数据库时使用SQLite
db, err = gorm.Open("sqlite3", util.RelativePath(conf.DatabaseConfig.DBFile))
} else {
db, err = gorm.Open(conf.DatabaseConfig.Type, fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local",
conf.DatabaseConfig.User,
conf.DatabaseConfig.Password,
conf.DatabaseConfig.Host,
conf.DatabaseConfig.Name))
}
}
db, err = gorm.Open(conf.DatabaseConfig.Type, fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local",
conf.DatabaseConfig.User,
conf.DatabaseConfig.Password,
conf.DatabaseConfig.Host,
conf.DatabaseConfig.Name))
// 处理表前缀
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {

74
models/init_sqlite.go Normal file
View file

@ -0,0 +1,74 @@
// +build sqlite
package model
import (
"fmt"
"time"
"github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/pkg/util"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
// DB 数据库链接单例
var DB *gorm.DB
// Init 初始化 MySQL 链接
func Init() {
util.Log().Info("初始化数据库连接")
var (
db *gorm.DB
err error
)
if gin.Mode() == gin.TestMode {
// 测试模式下,使用内存数据库
db, err = gorm.Open("sqlite3", ":memory:")
} else {
if conf.DatabaseConfig.Type == "UNSET" {
// 未指定数据库时使用SQLite
db, err = gorm.Open("sqlite3", util.RelativePath(conf.DatabaseConfig.DBFile))
} else {
db, err = gorm.Open(conf.DatabaseConfig.Type, fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local",
conf.DatabaseConfig.User,
conf.DatabaseConfig.Password,
conf.DatabaseConfig.Host,
conf.DatabaseConfig.Name))
}
}
// 处理表前缀
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return conf.DatabaseConfig.TablePrefix + defaultTableName
}
// Debug模式下输出所有 SQL 日志
if conf.SystemConfig.Debug {
db.LogMode(true)
} else {
db.LogMode(false)
}
//db.SetLogger(util.Log())
if err != nil {
util.Log().Panic("连接数据库不成功, %s", err)
}
//设置连接池
//空闲
db.DB().SetMaxIdleConns(50)
//打开
db.DB().SetMaxOpenConns(100)
//超时
db.DB().SetConnMaxLifetime(time.Second * 30)
DB = db
//执行迁移
migration()
}

View file

@ -1,6 +1,6 @@
package controllers
import "C"
import (
"context"
"fmt"