feat: load storages while starting
This commit is contained in:
parent
74f1154e5e
commit
0fdfd1f2c2
@ -10,5 +10,4 @@ func Init() {
|
|||||||
bootstrap.Log()
|
bootstrap.Log()
|
||||||
bootstrap.InitDB()
|
bootstrap.InitDB()
|
||||||
data.InitData()
|
data.InitData()
|
||||||
bootstrap.InitAria2()
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/alist-org/alist/v3/cmd/flags"
|
"github.com/alist-org/alist/v3/cmd/flags"
|
||||||
_ "github.com/alist-org/alist/v3/drivers"
|
_ "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/internal/conf"
|
||||||
"github.com/alist-org/alist/v3/server"
|
"github.com/alist-org/alist/v3/server"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -20,6 +21,8 @@ var serverCmd = &cobra.Command{
|
|||||||
the address is defined in config file`,
|
the address is defined in config file`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
Init()
|
Init()
|
||||||
|
bootstrap.InitAria2()
|
||||||
|
bootstrap.LoadStorages()
|
||||||
if !flags.Debug && !flags.Dev {
|
if !flags.Debug && !flags.Dev {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
}
|
}
|
||||||
|
30
internal/bootstrap/storage.go
Normal file
30
internal/bootstrap/storage.go
Normal 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)
|
||||||
|
}
|
@ -17,3 +17,8 @@ var (
|
|||||||
|
|
||||||
var TypesMap = make(map[string][]string)
|
var TypesMap = make(map[string][]string)
|
||||||
var PrivacyReg []*regexp.Regexp
|
var PrivacyReg []*regexp.Regexp
|
||||||
|
|
||||||
|
var (
|
||||||
|
// StoragesLoaded loaded success if empty
|
||||||
|
StoragesLoaded = false
|
||||||
|
)
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
@ -48,3 +50,11 @@ func GetStorageById(id uint) (*model.Storage, error) {
|
|||||||
}
|
}
|
||||||
return &storage, nil
|
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
|
||||||
|
}
|
||||||
|
16
server/middlewares/check.go
Normal file
16
server/middlewares/check.go
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
@ -14,6 +14,7 @@ import (
|
|||||||
func Init(r *gin.Engine) {
|
func Init(r *gin.Engine) {
|
||||||
common.SecretKey = []byte(conf.Conf.JwtSecret)
|
common.SecretKey = []byte(conf.Conf.JwtSecret)
|
||||||
Cors(r)
|
Cors(r)
|
||||||
|
r.Use(middlewares.StoragesLoaded)
|
||||||
WebDav(r)
|
WebDav(r)
|
||||||
|
|
||||||
r.GET("/d/*path", middlewares.Down, handles.Down)
|
r.GET("/d/*path", middlewares.Down, handles.Down)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user