Skip to content

Commit

Permalink
feat: load storages while starting
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 11, 2022
1 parent 74f1154 commit 0fdfd1f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 1 deletion.
1 change: 0 additions & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ func Init() {
bootstrap.Log()
bootstrap.InitDB()
data.InitData()
bootstrap.InitAria2()
}
3 changes: 3 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down
30 changes: 30 additions & 0 deletions internal/bootstrap/storage.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions internal/conf/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ var (

var TypesMap = make(map[string][]string)
var PrivacyReg []*regexp.Regexp

var (
// StoragesLoaded loaded success if empty
StoragesLoaded = false
)
10 changes: 10 additions & 0 deletions internal/db/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package db

import (
"fmt"

"github.com/alist-org/alist/v3/internal/model"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions server/middlewares/check.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
1 change: 1 addition & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0fdfd1f

Please sign in to comment.