settings

This commit is contained in:
微凉
2021-10-30 00:35:29 +08:00
parent 57ad66b43a
commit 0324ea1fcb
13 changed files with 97 additions and 52 deletions

View File

@ -9,7 +9,7 @@ import (
)
func Auth(ctx *fiber.Ctx) error {
token := ctx.Get("token")
token := ctx.Get("Authorization")
password, err := model.GetSettingByKey("password")
if err != nil {
if err == gorm.ErrRecordNotFound {
@ -23,6 +23,15 @@ func Auth(ctx *fiber.Ctx) error {
return ctx.Next()
}
func Login(ctx *fiber.Ctx) error {
return SuccessResp(ctx)
}
func SetSuccess(ctx *fiber.Ctx) error {
ctx.Status(200)
return ctx.Next()
}
func CheckAccount(ctx *fiber.Ctx) error {
if model.AccountsCount() == 0 {
return ErrorResp(ctx,fmt.Errorf("no accounts,please add one first"),1001)

View File

@ -13,7 +13,7 @@ var validate = validator.New()
type Resp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
@ -44,7 +44,7 @@ func ParsePath(rawPath string) (*model.Account,string,drivers.Driver,error) {
func ErrorResp(ctx *fiber.Ctx,err error,code int) error {
return ctx.JSON(Resp{
Code: code,
Msg: err.Error(),
Message: err.Error(),
Data: nil,
})
}
@ -53,13 +53,13 @@ func SuccessResp(ctx *fiber.Ctx, data ...interface{}) error {
if len(data) == 0 {
return ctx.JSON(Resp{
Code: 200,
Msg: "success",
Message: "success",
Data: nil,
})
}
return ctx.JSON(Resp{
Code: 200,
Msg: "success",
Message: "success",
Data: data[0],
})
}

View File

@ -30,7 +30,7 @@ func Path(ctx *fiber.Ctx) error {
if model.AccountsCount() > 1 && req.Path == "/" {
return ctx.JSON(Resp{
Code: 200,
Msg: "folder",
Message: "folder",
Data: model.GetAccountFiles(),
})
}
@ -45,13 +45,13 @@ func Path(ctx *fiber.Ctx) error {
if file != nil {
return ctx.JSON(Resp{
Code: 200,
Msg: "file",
Message: "file",
Data: []*model.File{file},
})
} else {
return ctx.JSON(Resp{
Code: 200,
Msg: "folder",
Message: "folder",
Data: files,
})
}

View File

@ -13,17 +13,20 @@ func InitApiRouter(app *fiber.App) {
// TODO check allow proxy?
app.Get("/p/*", Proxy)
public := app.Group("/api/public")
api := app.Group("/api")
api.Use(SetSuccess)
public := api.Group("/public")
{
// TODO check accounts
public.Post("/path", CheckAccount, Path)
public.Get("/settings", GetSettingsPublic)
}
admin := app.Group("/api/admin")
admin := api.Group("/admin")
{
admin.Use(Auth)
admin.Get("/settings", GetSettingsByType)
admin.Get("/login", Login)
admin.Get("/settings", GetSettings)
admin.Post("/settings", SaveSettings)
admin.Post("/account", SaveAccount)
admin.Get("/accounts", GetAccounts)

View File

@ -9,38 +9,42 @@ import (
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)
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,
})
return ErrorResp(ctx, err, 500)
} else {
return SuccessResp(ctx)
}
}
func GetSettingsByType(ctx *fiber.Ctx) error {
func GetSettingsByGroup(ctx *fiber.Ctx) error {
t, err := strconv.Atoi(ctx.Query("type"))
if err != nil {
return ErrorResp(ctx, err, 400)
}
settings, err := model.GetSettingByType(t)
settings, err := model.GetSettingsByGroup(t)
if err != nil {
return ErrorResp(ctx, err, 400)
}
return SuccessResp(ctx,settings)
return SuccessResp(ctx, settings)
}
func GetSettings(ctx *fiber.Ctx) error {
settings, err := model.GetSettings()
if err != nil {
return ErrorResp(ctx, err, 400)
}
return SuccessResp(ctx, settings)
}
func GetSettingsPublic(ctx *fiber.Ctx) error {
settings, err := model.GetSettingByType(0)
settings, err := model.GetSettingsByGroup(0)
if err != nil {
return ErrorResp(ctx, err, 400)
}
return SuccessResp(ctx,settings)
}
return SuccessResp(ctx, settings)
}