feat: load storages while starting

pull/1604/head
Noah Hsu 2022-08-11 21:32:33 +08:00
parent 74f1154e5e
commit 0fdfd1f2c2
7 changed files with 65 additions and 1 deletions

View File

@ -10,5 +10,4 @@ func Init() {
bootstrap.Log()
bootstrap.InitDB()
data.InitData()
bootstrap.InitAria2()
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -17,3 +17,8 @@ var (
var TypesMap = make(map[string][]string)
var PrivacyReg []*regexp.Regexp
var (
// StoragesLoaded loaded success if empty
StoragesLoaded = false
)

View File

@ -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
}

View File

@ -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()
}
}

View File

@ -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)