basic structure

This commit is contained in:
微凉
2021-10-26 22:28:37 +08:00
parent d68a4048da
commit fb7e67724d
25 changed files with 615 additions and 45 deletions

40
server/account.go Normal file
View File

@ -0,0 +1,40 @@
package server
import (
"fmt"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/gofiber/fiber/v2"
)
func GetAccounts(ctx *fiber.Ctx) error {
return SuccessResp(ctx, model.GetAccounts())
}
func SaveAccount(ctx *fiber.Ctx) error {
var req model.Account
if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx, err, 400)
}
if err := validate.Struct(req); err != nil {
return ErrorResp(ctx, err, 400)
}
driver, ok := drivers.GetDriver(req.Type)
if !ok {
return ErrorResp(ctx, fmt.Errorf("no [%s] driver", req.Type), 400)
}
if err := model.SaveAccount(req); err != nil {
return ErrorResp(ctx, err, 500)
} else {
driver.Save(req)
return SuccessResp(ctx)
}
}
func DeleteAccount(ctx *fiber.Ctx) error {
name := ctx.Query("name")
if err := model.DeleteAccount(name); err != nil {
return ErrorResp(ctx, err, 500)
}
return SuccessResp(ctx)
}

65
server/common.go Normal file
View File

@ -0,0 +1,65 @@
package server
import (
"fmt"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"strings"
)
var validate = validator.New()
type Resp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func ParsePath(rawPath string) (*model.Account,string,drivers.Driver,error) {
var path,name string
switch model.AccountsCount() {
case 0:
return nil,"",nil,fmt.Errorf("no accounts,please add one first")
case 1:
path = rawPath
break
default:
paths := strings.Split(rawPath,"/")
path = strings.Join(paths[1:],"/")
name = paths[0]
}
account,ok := model.GetAccount(name)
if !ok {
return nil,"",nil,fmt.Errorf("")
}
driver,ok := drivers.GetDriver(account.Type)
if !ok {
return nil,"",nil,fmt.Errorf("no [%s] driver",account.Type)
}
return &account,path,driver,nil
}
func ErrorResp(ctx *fiber.Ctx,err error,code int) error {
return ctx.JSON(Resp{
Code: code,
Msg: err.Error(),
Data: nil,
})
}
func SuccessResp(ctx *fiber.Ctx, data ...interface{}) error {
if len(data) == 0 {
return ctx.JSON(Resp{
Code: 200,
Msg: "success",
Data: nil,
})
}
return ctx.JSON(Resp{
Code: 200,
Msg: "success",
Data: data[0],
})
}

20
server/down.go Normal file
View File

@ -0,0 +1,20 @@
package server
import "github.com/gofiber/fiber/v2"
func Down(ctx *fiber.Ctx) error {
rawPath := ctx.Params("*")
account, path, driver, err := ParsePath(rawPath)
if err != nil {
return ErrorResp(ctx, err, 500)
}
link, err := driver.Link(path, account)
if err != nil {
return ErrorResp(ctx, err, 500)
}
if account.Type == "native" {
return ctx.SendFile(link)
} else {
return ctx.Redirect(link, 302)
}
}

10
server/driver.go Normal file
View File

@ -0,0 +1,10 @@
package server
import (
"github.com/Xhofe/alist/drivers"
"github.com/gofiber/fiber/v2"
)
func GetDrivers(ctx *fiber.Ctx) error {
return SuccessResp(ctx, drivers.GetDriverNames())
}

46
server/path.go Normal file
View File

@ -0,0 +1,46 @@
package server
import (
"github.com/Xhofe/alist/model"
"github.com/gofiber/fiber/v2"
)
type PathReq struct {
Path string `json:"Path"`
Password string `json:"Password"`
}
func Path(ctx *fiber.Ctx) error {
var req PathReq
if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx, err, 400)
}
if model.AccountsCount() > 1 && req.Path == "" {
return ctx.JSON(Resp{
Code: 200,
Msg: "folder",
Data: model.GetAccountFiles(),
})
}
account, path, driver, err := ParsePath(req.Path)
if err != nil {
return ErrorResp(ctx, err, 500)
}
file, files, err := driver.Path(path, account)
if err != nil {
return ErrorResp(ctx, err, 500)
}
if file != nil {
return ctx.JSON(Resp{
Code: 200,
Msg: "file",
Data: []*model.File{file},
})
} else {
return ctx.JSON(Resp{
Code: 200,
Msg: "folder",
Data: files,
})
}
}

25
server/router.go Normal file
View File

@ -0,0 +1,25 @@
package server
import "github.com/gofiber/fiber/v2"
func InitApiRouter(app *fiber.App) {
app.Get("/d/*", Down)
public := app.Group("/api/public")
{
// TODO check accounts
public.Post("/path", Path)
}
admin := app.Group("/api/admin")
{
// TODO auth
admin.Get("/settings", GetSettingsByType)
admin.Post("/settings", SaveSettings)
admin.Post("/account", SaveAccount)
admin.Get("/accounts", GetAccounts)
admin.Delete("/account", DeleteAccount)
admin.Get("/drivers", GetDrivers)
}
}

38
server/setting.go Normal file
View File

@ -0,0 +1,38 @@
package server
import (
"github.com/Xhofe/alist/model"
"github.com/gofiber/fiber/v2"
"strconv"
)
func SaveSettings(ctx *fiber.Ctx) error {
var req []model.SettingItem
if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx,err,400)
}
if err := validate.Struct(req); err != nil {
return ErrorResp(ctx,err,400)
}
if err := model.SaveSettings(req); err != nil {
return ctx.JSON(Resp{
Code: 500,
Msg: err.Error(),
Data: nil,
})
} else {
return SuccessResp(ctx)
}
}
func GetSettingsByType(ctx *fiber.Ctx) error {
t, err := strconv.Atoi(ctx.Query("type"))
if err != nil {
return ErrorResp(ctx, err, 400)
}
settings, err := model.GetSettingByType(t)
if err != nil {
return ErrorResp(ctx, err, 400)
}
return SuccessResp(ctx,settings)
}