From a8272a66b760a93a222bd07b49c5aa71f0179ed6 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sat, 22 Feb 2020 12:18:49 +0800 Subject: [PATCH] Modify: move aria2 into internal packages / migration version check --- bootstrap/init.go | 33 ++ go.mod | 1 - main.go | 26 +- models/download.go | 2 +- models/migration.go | 53 +-- pkg/aria2/aria2.go | 2 +- pkg/aria2/caller.go | 2 +- pkg/aria2/monitor.go | 2 +- pkg/aria2/monitor_test.go | 2 +- pkg/aria2/notification.go | 2 +- pkg/aria2/rpc/README.md | 257 +++++++++++++ pkg/aria2/rpc/call.go | 274 ++++++++++++++ pkg/aria2/rpc/call_test.go | 23 ++ pkg/aria2/rpc/client.go | 656 ++++++++++++++++++++++++++++++++++ pkg/aria2/rpc/client_test.go | 125 +++++++ pkg/aria2/rpc/const.go | 39 ++ pkg/aria2/rpc/json2.go | 116 ++++++ pkg/aria2/rpc/notification.go | 44 +++ pkg/aria2/rpc/proc.go | 42 +++ pkg/aria2/rpc/proto.go | 40 +++ pkg/aria2/rpc/resp.go | 102 ++++++ pkg/conf/conf.go | 32 +- pkg/conf/defaults.go | 2 +- pkg/conf/version.go | 13 +- pkg/filesystem/image.go | 8 +- pkg/serializer/aria2.go | 2 +- routers/controllers/site.go | 30 +- 27 files changed, 1849 insertions(+), 81 deletions(-) create mode 100644 bootstrap/init.go create mode 100644 pkg/aria2/rpc/README.md create mode 100644 pkg/aria2/rpc/call.go create mode 100644 pkg/aria2/rpc/call_test.go create mode 100644 pkg/aria2/rpc/client.go create mode 100644 pkg/aria2/rpc/client_test.go create mode 100644 pkg/aria2/rpc/const.go create mode 100644 pkg/aria2/rpc/json2.go create mode 100644 pkg/aria2/rpc/notification.go create mode 100644 pkg/aria2/rpc/proc.go create mode 100644 pkg/aria2/rpc/proto.go create mode 100644 pkg/aria2/rpc/resp.go diff --git a/bootstrap/init.go b/bootstrap/init.go new file mode 100644 index 0000000..3f63789 --- /dev/null +++ b/bootstrap/init.go @@ -0,0 +1,33 @@ +package bootstrap + +import ( + model "github.com/HFO4/cloudreve/models" + "github.com/HFO4/cloudreve/pkg/aria2" + "github.com/HFO4/cloudreve/pkg/auth" + "github.com/HFO4/cloudreve/pkg/authn" + "github.com/HFO4/cloudreve/pkg/cache" + "github.com/HFO4/cloudreve/pkg/conf" + "github.com/HFO4/cloudreve/pkg/crontab" + "github.com/HFO4/cloudreve/pkg/email" + "github.com/HFO4/cloudreve/pkg/task" + "github.com/gin-gonic/gin" +) + +// Init 初始化启动 +func Init(path string) { + conf.Init(path) + // Debug 关闭时,切换为生产模式 + if !conf.SystemConfig.Debug { + gin.SetMode(gin.ReleaseMode) + } + cache.Init() + if conf.SystemConfig.Mode == "master" { + model.Init() + authn.Init() + task.Init() + aria2.Init() + email.Init() + crontab.Init() + } + auth.Init() +} diff --git a/go.mod b/go.mod index 84ee37d..80bc9aa 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,6 @@ require ( github.com/stretchr/testify v1.4.0 github.com/tencentyun/cos-go-sdk-v5 v0.0.0-20200120023323-87ff3bc489ac github.com/upyun/go-sdk v2.1.0+incompatible - github.com/zyxar/argo v0.0.0-20190709183644-6096bc0e6414 golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 golang.org/x/text v0.3.2 gopkg.in/go-playground/validator.v8 v8.18.2 diff --git a/main.go b/main.go index 9e96230..6cc6920 100644 --- a/main.go +++ b/main.go @@ -1,35 +1,13 @@ package main import ( - "github.com/HFO4/cloudreve/models" - "github.com/HFO4/cloudreve/pkg/aria2" - "github.com/HFO4/cloudreve/pkg/auth" - "github.com/HFO4/cloudreve/pkg/authn" - "github.com/HFO4/cloudreve/pkg/cache" + "github.com/HFO4/cloudreve/bootstrap" "github.com/HFO4/cloudreve/pkg/conf" - "github.com/HFO4/cloudreve/pkg/crontab" - "github.com/HFO4/cloudreve/pkg/email" - "github.com/HFO4/cloudreve/pkg/task" "github.com/HFO4/cloudreve/routers" - "github.com/gin-gonic/gin" ) func init() { - conf.Init("conf/conf.ini") - // Debug 关闭时,切换为生产模式 - if !conf.SystemConfig.Debug { - gin.SetMode(gin.ReleaseMode) - } - cache.Init() - if conf.SystemConfig.Mode == "master" { - model.Init() - authn.Init() - task.Init() - aria2.Init() - email.Init() - crontab.Init() - } - auth.Init() + bootstrap.Init("conf/conf.ini") } func main() { diff --git a/models/download.go b/models/download.go index d32064f..f3b3ea2 100644 --- a/models/download.go +++ b/models/download.go @@ -2,9 +2,9 @@ package model import ( "encoding/json" + "github.com/HFO4/cloudreve/pkg/aria2/rpc" "github.com/HFO4/cloudreve/pkg/util" "github.com/jinzhu/gorm" - "github.com/zyxar/argo/rpc" ) // Download 离线下载队列模型 diff --git a/models/migration.go b/models/migration.go index 87fc173..6052e94 100644 --- a/models/migration.go +++ b/models/migration.go @@ -3,27 +3,26 @@ package model import ( "github.com/HFO4/cloudreve/pkg/conf" "github.com/HFO4/cloudreve/pkg/util" - "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" - "github.com/mcuadros/go-version" - "io/ioutil" ) +// 是否需要迁移 +func needMigration() bool { + var setting Setting + DB.Where("name = ?", "database_version").First(&setting) + return setting.Value != conf.RequiredDBVersion +} + //执行数据迁移 func migration() { - // 检查 version.lock 确认是否需要执行迁移 - // Debug 模式及测试模式下一定会执行迁移 - if !conf.SystemConfig.Debug && gin.Mode() != gin.TestMode { - if util.Exists("version.lock") { - versionLock, _ := ioutil.ReadFile("version.lock") - if version.Compare(string(versionLock), conf.BackendVersion, "=") { - util.Log().Info("后端版本匹配,跳过数据库迁移") - return - } - } + // 确认是否需要执行迁移 + if !needMigration() { + util.Log().Info("数据库版本匹配,跳过数据库迁移") + return + } - util.Log().Info("开始进行数据库自动迁移...") + util.Log().Info("开始进行数据库初始化...") // 自动迁移模式 if conf.DatabaseConfig.Type == "mysql" { @@ -44,13 +43,7 @@ func migration() { // 向设置数据表添加初始设置 addDefaultSettings() - // 迁移完毕后写入版本锁 version.lock - err := conf.WriteVersionLock() - if err != nil { - util.Log().Warning("无法写入版本控制锁 version.lock, %s", err) - } - - util.Log().Info("数据库自动迁移结束") + util.Log().Info("数据库初始化结束") } @@ -80,9 +73,8 @@ func addDefaultPolicy() { func addDefaultSettings() { defaultSettings := []Setting{ - {Name: "siteURL", Value: ``, Type: "basic"}, + {Name: "siteURL", Value: `http://localhost`, Type: "basic"}, {Name: "siteName", Value: `Cloudreve`, Type: "basic"}, - {Name: "siteStatus", Value: `open`, Type: "basic"}, {Name: "register_enabled", Value: `1`, Type: "register"}, {Name: "default_group", Value: `2`, Type: "register"}, {Name: "siteKeywords", Value: `网盘,网盘`, Type: "basic"}, @@ -137,7 +129,7 @@ solid #e9e9e9;"bgcolor="#fff">
亲爱的{userName}: |
请点击下方按钮完成密码重设。如果非你本人操作,请忽略此邮件。 |
重设密码 |
感谢您选择{siteTitle}。 |