From b98cd915a40066aed4dba68ccedcc68362a07bff Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Sun, 26 Jun 2022 20:25:02 +0800 Subject: [PATCH] feat: driver manage api --- bootstrap/log.go | 4 ++++ internal/operations/account.go | 1 + internal/operations/driver.go | 8 ++++++++ server/controllers/driver.go | 26 ++++++++++++++++++++++++++ server/router.go | 5 +++++ 5 files changed, 44 insertions(+) create mode 100644 server/controllers/driver.go diff --git a/bootstrap/log.go b/bootstrap/log.go index d3ccf1d2..066a5780 100644 --- a/bootstrap/log.go +++ b/bootstrap/log.go @@ -17,6 +17,7 @@ func init() { TimestampFormat: "2006-01-02 15:04:05", FullTimestamp: true, }) + logrus.SetLevel(logrus.DebugLevel) } func Log() { @@ -24,6 +25,9 @@ func Log() { if args.Debug || args.Dev { logrus.SetLevel(logrus.DebugLevel) logrus.SetReportCaller(true) + } else { + logrus.SetLevel(logrus.InfoLevel) + logrus.SetReportCaller(false) } logConfig := conf.Conf.Log if logConfig.Enable { diff --git a/internal/operations/account.go b/internal/operations/account.go index b57a2964..6efb65c7 100644 --- a/internal/operations/account.go +++ b/internal/operations/account.go @@ -51,6 +51,7 @@ func CreateAccount(ctx context.Context, account model.Account) error { if err != nil { return errors.WithMessage(err, "failed init account but account is already created") } + log.Debugf("account %+v is created", accountDriver) accountsMap.Store(account.VirtualPath, accountDriver) return nil } diff --git a/internal/operations/driver.go b/internal/operations/driver.go index 8bbf9165..ec0df14b 100644 --- a/internal/operations/driver.go +++ b/internal/operations/driver.go @@ -28,6 +28,14 @@ func GetDriverNew(name string) (New, error) { return n, nil } +func GetDriverNames() []string { + var driverNames []string + for k := range driverItemsMap { + driverNames = append(driverNames, k) + } + return driverNames +} + func GetDriverItemsMap() map[string]driver.Items { return driverItemsMap } diff --git a/server/controllers/driver.go b/server/controllers/driver.go new file mode 100644 index 00000000..f4ca6de1 --- /dev/null +++ b/server/controllers/driver.go @@ -0,0 +1,26 @@ +package controllers + +import ( + "github.com/alist-org/alist/v3/internal/operations" + "github.com/alist-org/alist/v3/server/common" + "github.com/gin-gonic/gin" +) + +func ListDriverItems(c *gin.Context) { + common.SuccessResp(c, operations.GetDriverItemsMap()) +} + +func ListDriverNames(c *gin.Context) { + common.SuccessResp(c, operations.GetDriverNames()) +} + +func GetDriverItems(c *gin.Context) { + driverName := c.Query("driver") + itemsMap := operations.GetDriverItemsMap() + items, ok := itemsMap[driverName] + if !ok { + common.ErrorStrResp(c, "driver not found", 404) + return + } + common.SuccessResp(nil, items) +} diff --git a/server/router.go b/server/router.go index 83340fdf..e4f0b229 100644 --- a/server/router.go +++ b/server/router.go @@ -36,6 +36,11 @@ func Init(r *gin.Engine) { account.POST("/create", controllers.CreateAccount) account.POST("/update", controllers.UpdateAccount) account.POST("/delete", controllers.DeleteAccount) + + driver := admin.Group("/driver") + driver.GET("/list", controllers.ListDriverItems) + driver.GET("/names", controllers.ListDriverNames) + driver.GET("/items", controllers.GetDriverItems) } func Cors(r *gin.Engine) {