diff --git a/cmd/common.go b/cmd/common.go index 4776cae8445..62a80c77655 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -10,5 +10,4 @@ func Init() { bootstrap.Log() bootstrap.InitDB() data.InitData() - bootstrap.InitAria2() } diff --git a/cmd/server.go b/cmd/server.go index 5ad2475befb..db5970abcc3 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -5,6 +5,7 @@ import ( "github.com/alist-org/alist/v3/cmd/flags" _ "github.com/alist-org/alist/v3/drivers" + "github.com/alist-org/alist/v3/internal/bootstrap" "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/server" "github.com/gin-gonic/gin" @@ -20,6 +21,8 @@ var serverCmd = &cobra.Command{ the address is defined in config file`, Run: func(cmd *cobra.Command, args []string) { Init() + bootstrap.InitAria2() + bootstrap.LoadStorages() if !flags.Debug && !flags.Dev { gin.SetMode(gin.ReleaseMode) } diff --git a/internal/bootstrap/storage.go b/internal/bootstrap/storage.go new file mode 100644 index 00000000000..46af1a1b043 --- /dev/null +++ b/internal/bootstrap/storage.go @@ -0,0 +1,30 @@ +package bootstrap + +import ( + "context" + + "github.com/alist-org/alist/v3/internal/conf" + "github.com/alist-org/alist/v3/internal/db" + "github.com/alist-org/alist/v3/internal/model" + "github.com/alist-org/alist/v3/internal/operations" + log "github.com/sirupsen/logrus" +) + +func LoadStorages() { + storages, err := db.GetEnabledStorages() + if err != nil { + log.Fatalf("failed get enabled storages: %+v", err) + } + go func(storages []model.Storage) { + for i := range storages { + err := operations.LoadStorage(context.Background(), storages[i]) + if err != nil { + log.Errorf("failed get enabled storages: %+v", err) + } else { + log.Infof("success load storage: [%s], driver: [%s]", + storages[i].MountPath, storages[i].Driver) + } + } + conf.StoragesLoaded = true + }(storages) +} diff --git a/internal/conf/var.go b/internal/conf/var.go index 1fbc15109f5..46e6d9c4e3e 100644 --- a/internal/conf/var.go +++ b/internal/conf/var.go @@ -17,3 +17,8 @@ var ( var TypesMap = make(map[string][]string) var PrivacyReg []*regexp.Regexp + +var ( + // StoragesLoaded loaded success if empty + StoragesLoaded = false +) diff --git a/internal/db/storage.go b/internal/db/storage.go index 1d03b10f92a..73376e72a26 100644 --- a/internal/db/storage.go +++ b/internal/db/storage.go @@ -1,6 +1,8 @@ package db import ( + "fmt" + "github.com/alist-org/alist/v3/internal/model" "github.com/pkg/errors" ) @@ -48,3 +50,11 @@ func GetStorageById(id uint) (*model.Storage, error) { } return &storage, nil } + +func GetEnabledStorages() ([]model.Storage, error) { + var storages []model.Storage + if err := db.Where(fmt.Sprintf("%s = ?", columnName("disabled")), false).Find(&storages).Error; err != nil { + return nil, errors.WithStack(err) + } + return storages, nil +} diff --git a/server/middlewares/check.go b/server/middlewares/check.go new file mode 100644 index 00000000000..0a3f06a5226 --- /dev/null +++ b/server/middlewares/check.go @@ -0,0 +1,16 @@ +package middlewares + +import ( + "github.com/alist-org/alist/v3/internal/conf" + "github.com/alist-org/alist/v3/server/common" + "github.com/gin-gonic/gin" +) + +func StoragesLoaded(c *gin.Context) { + if conf.StoragesLoaded { + c.Next() + } else { + common.ErrorStrResp(c, "Loading storage, please wait", 500) + c.Abort() + } +} diff --git a/server/router.go b/server/router.go index 6f4c83019f5..01c3ecbf815 100644 --- a/server/router.go +++ b/server/router.go @@ -14,6 +14,7 @@ import ( func Init(r *gin.Engine) { common.SecretKey = []byte(conf.Conf.JwtSecret) Cors(r) + r.Use(middlewares.StoragesLoaded) WebDav(r) r.GET("/d/*path", middlewares.Down, handles.Down)