🔨 switch fiber to gin
This commit is contained in:
@ -4,35 +4,35 @@ import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/drivers"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetAccounts(ctx *fiber.Ctx) error {
|
||||
func GetAccounts(c *gin.Context) {
|
||||
accounts, err := model.GetAccounts()
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
return SuccessResp(ctx, accounts)
|
||||
SuccessResp(c, accounts)
|
||||
}
|
||||
|
||||
func SaveAccount(ctx *fiber.Ctx) error {
|
||||
func SaveAccount(c *gin.Context) {
|
||||
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)
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
driver, ok := drivers.GetDriver(req.Type)
|
||||
if !ok {
|
||||
return ErrorResp(ctx, fmt.Errorf("no [%s] driver", req.Type), 400)
|
||||
ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400)
|
||||
return
|
||||
}
|
||||
old, ok := model.GetAccount(req.Name)
|
||||
now := time.Now()
|
||||
req.UpdatedAt = &now
|
||||
if err := model.SaveAccount(req); err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
} else {
|
||||
if ok {
|
||||
err = driver.Save(&req, &old)
|
||||
@ -40,16 +40,18 @@ func SaveAccount(ctx *fiber.Ctx) error {
|
||||
err = driver.Save(&req, nil)
|
||||
}
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
return SuccessResp(ctx)
|
||||
SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteAccount(ctx *fiber.Ctx) error {
|
||||
name := ctx.Query("name")
|
||||
func DeleteAccount(c *gin.Context) {
|
||||
name := c.Query("name")
|
||||
if err := model.DeleteAccount(name); err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
return SuccessResp(ctx)
|
||||
SuccessResp(c)
|
||||
}
|
||||
|
@ -2,14 +2,14 @@ package server
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ClearCache(ctx *fiber.Ctx) error {
|
||||
func ClearCache(c *gin.Context) {
|
||||
err := conf.Cache.Clear(conf.Ctx)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx,err,500)
|
||||
}else {
|
||||
return SuccessResp(ctx)
|
||||
ErrorResp(c, err, 500)
|
||||
} else {
|
||||
SuccessResp(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,37 +4,36 @@ import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Auth(ctx *fiber.Ctx) error {
|
||||
token := ctx.Get("Authorization")
|
||||
func Auth(c *gin.Context) {
|
||||
token := c.GetHeader("Authorization")
|
||||
password, err := model.GetSettingByKey("password")
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return ErrorResp(ctx, fmt.Errorf("password not set"), 400)
|
||||
ErrorResp(c, fmt.Errorf("password not set"), 400)
|
||||
return
|
||||
}
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if token != utils.GetMD5Encode(password.Value) {
|
||||
return ErrorResp(ctx, fmt.Errorf("wrong password"), 401)
|
||||
ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
return
|
||||
}
|
||||
return ctx.Next()
|
||||
c.Next()
|
||||
}
|
||||
|
||||
func Login(ctx *fiber.Ctx) error {
|
||||
return SuccessResp(ctx)
|
||||
func Login(c *gin.Context) {
|
||||
SuccessResp(c)
|
||||
}
|
||||
|
||||
func SetSuccess(ctx *fiber.Ctx) error {
|
||||
ctx.Status(200)
|
||||
return ctx.Next()
|
||||
}
|
||||
|
||||
func CheckAccount(ctx *fiber.Ctx) error {
|
||||
func CheckAccount(c *gin.Context) {
|
||||
if model.AccountsCount() == 0 {
|
||||
return ErrorResp(ctx,fmt.Errorf("no accounts,please add one first"),1001)
|
||||
ErrorResp(c, fmt.Errorf("no accounts,please add one first"), 1001)
|
||||
return
|
||||
}
|
||||
return ctx.Next()
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
|
@ -4,62 +4,61 @@ import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/drivers"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var validate = validator.New()
|
||||
|
||||
type Resp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func ParsePath(rawPath string) (*model.Account,string,drivers.Driver,error) {
|
||||
var path,name string
|
||||
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")
|
||||
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[2:],"/")
|
||||
paths := strings.Split(rawPath, "/")
|
||||
path = "/" + strings.Join(paths[2:], "/")
|
||||
name = paths[1]
|
||||
}
|
||||
account,ok := model.GetAccount(name)
|
||||
account, ok := model.GetAccount(name)
|
||||
if !ok {
|
||||
return nil,"",nil,fmt.Errorf("no [%s] account", name)
|
||||
return nil, "", nil, fmt.Errorf("no [%s] account", name)
|
||||
}
|
||||
driver,ok := drivers.GetDriver(account.Type)
|
||||
driver, ok := drivers.GetDriver(account.Type)
|
||||
if !ok {
|
||||
return nil,"",nil,fmt.Errorf("no [%s] driver",account.Type)
|
||||
return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type)
|
||||
}
|
||||
return &account,path,driver,nil
|
||||
return &account, path, driver, nil
|
||||
}
|
||||
|
||||
func ErrorResp(ctx *fiber.Ctx,err error,code int) error {
|
||||
return ctx.JSON(Resp{
|
||||
Code: code,
|
||||
Message: err.Error(),
|
||||
Data: nil,
|
||||
func ErrorResp(c *gin.Context, err error, code int) {
|
||||
c.JSON(200, Resp{
|
||||
Code: code,
|
||||
Message: err.Error(),
|
||||
Data: nil,
|
||||
})
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
func SuccessResp(ctx *fiber.Ctx, data ...interface{}) error {
|
||||
func SuccessResp(c *gin.Context, data ...interface{}) {
|
||||
if len(data) == 0 {
|
||||
return ctx.JSON(Resp{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: nil,
|
||||
c.JSON(200, Resp{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
return ctx.JSON(Resp{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: data[0],
|
||||
c.JSON(200, Resp{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: data[0],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -4,79 +4,66 @@ import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/proxy"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func Down(ctx *fiber.Ctx) error {
|
||||
rawPath, err := url.PathUnescape(ctx.Params("*"))
|
||||
func Down(c *gin.Context) {
|
||||
rawPath, err := url.PathUnescape(c.Param("path"))
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("down: %s", rawPath)
|
||||
account, path, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if account.Type == "Native" {
|
||||
return ctx.SendFile(link)
|
||||
c.File(link)
|
||||
return
|
||||
} else {
|
||||
return ctx.Redirect(link, 302)
|
||||
c.Redirect(302, link)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Proxy(ctx *fiber.Ctx) error {
|
||||
rawPath, err := url.PathUnescape(ctx.Params("*"))
|
||||
func Proxy(c *gin.Context) {
|
||||
rawPath, err := url.PathUnescape(c.Param("path"))
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("proxy: %s", rawPath)
|
||||
account, path, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
|
||||
return ErrorResp(ctx, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
|
||||
ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if account.Type == "Native" {
|
||||
return ctx.SendFile(link)
|
||||
c.File(link)
|
||||
return
|
||||
} else {
|
||||
driver.Proxy(ctx)
|
||||
//ctx.Response().ImmediateHeaderFlush = true
|
||||
//var ProxyNetHttp = fasthttpadaptor.NewFastHTTPHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// target, _ := url.Parse(link)
|
||||
// protocol := "https://"
|
||||
// if strings.HasPrefix(link, "http://") {
|
||||
// protocol = "http://"
|
||||
// }
|
||||
// targetHost, _ := url.Parse(fmt.Sprintf("%s%s", protocol, target.Host))
|
||||
// proxy := httputil.NewSingleHostReverseProxy(targetHost)
|
||||
// r.URL = target
|
||||
// r.Host = target.Host
|
||||
// proxy.ServeHTTP(w, r)
|
||||
//})
|
||||
//ProxyNetHttp(ctx.Context())
|
||||
if err := proxy.Do(ctx, link); err != nil {
|
||||
log.Errorf("proxy error: %s", err)
|
||||
return ErrorResp(ctx,err,500)
|
||||
}
|
||||
// Remove Server header from response
|
||||
ctx.Response().Header.Del(fiber.HeaderServer)
|
||||
ctx.Set("Access-Control-Allow-Origin","*")
|
||||
log.Debugf("proxy hedaer: %+v", ctx.Response().Header.String())
|
||||
return nil
|
||||
driver.Proxy(c)
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,9 @@ package server
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/drivers"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetDrivers(ctx *fiber.Ctx) error {
|
||||
return SuccessResp(ctx, drivers.GetDrivers())
|
||||
func GetDrivers(c *gin.Context) {
|
||||
SuccessResp(c, drivers.GetDrivers())
|
||||
}
|
@ -3,38 +3,37 @@ package server
|
||||
import (
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetMetas(ctx *fiber.Ctx) error {
|
||||
func GetMetas(c *gin.Context) {
|
||||
metas,err := model.GetMetas()
|
||||
if err != nil {
|
||||
return ErrorResp(ctx,err,500)
|
||||
ErrorResp(c,err,500)
|
||||
return
|
||||
}
|
||||
return SuccessResp(ctx, metas)
|
||||
SuccessResp(c, metas)
|
||||
}
|
||||
|
||||
func SaveMeta(ctx *fiber.Ctx) error {
|
||||
func SaveMeta(c *gin.Context) {
|
||||
var req model.Meta
|
||||
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 := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
req.Path = utils.ParsePath(req.Path)
|
||||
if err := model.SaveMeta(req); err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
} else {
|
||||
return SuccessResp(ctx)
|
||||
SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteMeta(ctx *fiber.Ctx) error {
|
||||
path := ctx.Query("path")
|
||||
func DeleteMeta(c *gin.Context) {
|
||||
path := c.Query("path")
|
||||
//path = utils.ParsePath(path)
|
||||
if err := model.DeleteMeta(path); err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
}
|
||||
return SuccessResp(ctx)
|
||||
SuccessResp(c)
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
)
|
||||
@ -14,44 +14,50 @@ type PathReq struct {
|
||||
Password string `json:"Password"`
|
||||
}
|
||||
|
||||
func Path(ctx *fiber.Ctx) error {
|
||||
func Path(c *gin.Context) {
|
||||
var req PathReq
|
||||
if err := ctx.BodyParser(&req); err != nil {
|
||||
return ErrorResp(ctx, err, 400)
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
req.Path = utils.ParsePath(req.Path)
|
||||
log.Debugf("path: %s", req.Path)
|
||||
meta, err := model.GetMetaByPath(req.Path)
|
||||
if err == nil {
|
||||
if meta.Password != "" && meta.Password != req.Password {
|
||||
return ErrorResp(ctx, fmt.Errorf("wrong password"), 401)
|
||||
ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
return
|
||||
}
|
||||
// TODO hide or ignore?
|
||||
}
|
||||
if model.AccountsCount() > 1 && req.Path == "/" {
|
||||
files, err := model.GetAccountFiles()
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
return ctx.JSON(Resp{
|
||||
c.JSON(200, Resp{
|
||||
Code: 200,
|
||||
Message: "folder",
|
||||
Data: files,
|
||||
})
|
||||
return
|
||||
}
|
||||
account, path, driver, err := ParsePath(req.Path)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
file, files, err := driver.Path(path, account)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if file != nil {
|
||||
if account.Type == "Native" {
|
||||
file.Url = fmt.Sprintf("%s://%s/p%s", ctx.Protocol(), ctx.Hostname(), req.Path)
|
||||
file.Url = fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path)
|
||||
}
|
||||
return ctx.JSON(Resp{
|
||||
c.JSON(200, Resp{
|
||||
Code: 200,
|
||||
Message: "file",
|
||||
Data: []*model.File{file},
|
||||
@ -67,7 +73,7 @@ func Path(ctx *fiber.Ctx) error {
|
||||
}
|
||||
files = tmpFiles
|
||||
}
|
||||
return ctx.JSON(Resp{
|
||||
c.JSON(200, Resp{
|
||||
Code: 200,
|
||||
Message: "folder",
|
||||
Data: files,
|
||||
@ -75,49 +81,56 @@ func Path(ctx *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
func Link(ctx *fiber.Ctx) error {
|
||||
func Link(c *gin.Context) {
|
||||
var req PathReq
|
||||
if err := ctx.BodyParser(&req); err != nil {
|
||||
return ErrorResp(ctx, err, 400)
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
rawPath := req.Path
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("link: %s", rawPath)
|
||||
account, path, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if account.Type == "Native" {
|
||||
return SuccessResp(ctx, fiber.Map{
|
||||
"url": fmt.Sprintf("%s://%s/p%s", ctx.Protocol(), ctx.Hostname(), rawPath),
|
||||
SuccessResp(c, gin.H{
|
||||
"url": fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path),
|
||||
})
|
||||
return
|
||||
} else {
|
||||
return SuccessResp(ctx, fiber.Map{
|
||||
SuccessResp(c, gin.H{
|
||||
"url": link,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Preview(ctx *fiber.Ctx) error {
|
||||
func Preview(c *gin.Context) {
|
||||
var req PathReq
|
||||
if err := ctx.BodyParser(&req); err != nil {
|
||||
return ErrorResp(ctx, err, 400)
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
rawPath := req.Path
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("preview: %s", rawPath)
|
||||
account, path, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
data, err := driver.Preview(path, account)
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
} else {
|
||||
return SuccessResp(ctx, data)
|
||||
SuccessResp(c, data)
|
||||
}
|
||||
}
|
||||
|
@ -1,42 +1,48 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func InitApiRouter(app *fiber.App) {
|
||||
func InitApiRouter(r *gin.Engine) {
|
||||
|
||||
// TODO from settings
|
||||
app.Use(cors.New())
|
||||
app.Get("/d/*", Down)
|
||||
// TODO check allow proxy?
|
||||
app.Get("/p/*", Proxy)
|
||||
Cors(r)
|
||||
r.GET("/d/*path", Down)
|
||||
r.GET("/p/*path", Proxy)
|
||||
|
||||
api := app.Group("/api")
|
||||
api.Use(SetSuccess)
|
||||
api := r.Group("/api")
|
||||
public := api.Group("/public")
|
||||
{
|
||||
public.Post("/path", CheckAccount, Path)
|
||||
public.Post("/preview", CheckAccount, Preview)
|
||||
public.Get("/settings", GetSettingsPublic)
|
||||
public.Post("/link", CheckAccount, Link)
|
||||
public.POST("/path", CheckAccount, Path)
|
||||
public.POST("/preview", CheckAccount, Preview)
|
||||
public.GET("/settings", GetSettingsPublic)
|
||||
public.POST("/link", CheckAccount, Link)
|
||||
}
|
||||
|
||||
admin := api.Group("/admin")
|
||||
{
|
||||
admin.Use(Auth)
|
||||
admin.Get("/login", Login)
|
||||
admin.Get("/settings", GetSettings)
|
||||
admin.Post("/settings", SaveSettings)
|
||||
admin.Post("/account", SaveAccount)
|
||||
admin.Get("/accounts", GetAccounts)
|
||||
admin.Delete("/account", DeleteAccount)
|
||||
admin.Get("/drivers", GetDrivers)
|
||||
admin.Get("/clear_cache",ClearCache)
|
||||
admin.GET("/login", Login)
|
||||
admin.GET("/settings", GetSettings)
|
||||
admin.POST("/settings", SaveSettings)
|
||||
admin.POST("/account", SaveAccount)
|
||||
admin.GET("/accounts", GetAccounts)
|
||||
admin.DELETE("/account", DeleteAccount)
|
||||
admin.GET("/drivers", GetDrivers)
|
||||
admin.GET("/clear_cache", ClearCache)
|
||||
|
||||
admin.Get("/metas", GetMetas)
|
||||
admin.Post("/meta", SaveMeta)
|
||||
admin.Delete("/meta", DeleteMeta)
|
||||
admin.GET("/metas", GetMetas)
|
||||
admin.POST("/meta", SaveMeta)
|
||||
admin.DELETE("/meta", DeleteMeta)
|
||||
}
|
||||
Static(r)
|
||||
}
|
||||
|
||||
func Cors(r *gin.Engine) {
|
||||
config := cors.DefaultConfig()
|
||||
config.AllowAllOrigins = true
|
||||
config.AllowHeaders = append(config.AllowHeaders, "Authorization")
|
||||
r.Use(cors.New(config))
|
||||
}
|
||||
|
@ -2,37 +2,37 @@ package server
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SaveSettings(ctx *fiber.Ctx) error {
|
||||
func SaveSettings(c *gin.Context) {
|
||||
var req []model.SettingItem
|
||||
if err := ctx.BodyParser(&req); err != nil {
|
||||
return ErrorResp(ctx, err, 400)
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
//if err := validate.Struct(req); err != nil {
|
||||
// return ErrorResp(ctx, err, 400)
|
||||
//}
|
||||
if err := model.SaveSettings(req); err != nil {
|
||||
return ErrorResp(ctx, err, 500)
|
||||
ErrorResp(c, err, 500)
|
||||
} else {
|
||||
model.LoadSettings()
|
||||
return SuccessResp(ctx)
|
||||
SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
func GetSettings(ctx *fiber.Ctx) error {
|
||||
func GetSettings(c *gin.Context) {
|
||||
settings, err := model.GetSettings()
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 400)
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
return SuccessResp(ctx, settings)
|
||||
SuccessResp(c, settings)
|
||||
}
|
||||
|
||||
func GetSettingsPublic(ctx *fiber.Ctx) error {
|
||||
func GetSettingsPublic(c *gin.Context) {
|
||||
settings, err := model.GetSettingsPublic()
|
||||
if err != nil {
|
||||
return ErrorResp(ctx, err, 400)
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
return SuccessResp(ctx, settings)
|
||||
SuccessResp(c, settings)
|
||||
}
|
||||
|
31
server/static.go
Normal file
31
server/static.go
Normal file
@ -0,0 +1,31 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/public"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var data []byte
|
||||
|
||||
func init() {
|
||||
index, _ := public.Public.Open("index.html")
|
||||
data, _ = ioutil.ReadAll(index)
|
||||
}
|
||||
|
||||
func Static(r *gin.Engine) {
|
||||
assets, err := fs.Sub(public.Public, "assets")
|
||||
if err != nil {
|
||||
log.Fatalf("can't find assets folder")
|
||||
}
|
||||
r.StaticFS("/assets/", http.FS(assets))
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
c.Status(200)
|
||||
c.Header("Content-Type", "text/html")
|
||||
_, _ = c.Writer.Write(data)
|
||||
c.Writer.Flush()
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user