🎨 Improve the code structure

pull/548/head
微凉 2021-12-07 15:56:43 +08:00
parent 6e8d551420
commit 03580fd76c
14 changed files with 220 additions and 176 deletions

View File

@ -1,45 +1,17 @@
package server package common
import ( import (
"fmt"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gorm.io/gorm"
) )
func Auth(c *gin.Context) {
token := c.GetHeader("Authorization")
password, err := model.GetSettingByKey("password")
if err != nil {
if err == gorm.ErrRecordNotFound {
ErrorResp(c, fmt.Errorf("password not set"), 400)
return
}
ErrorResp(c, err, 500)
return
}
if token != utils.GetMD5Encode(password.Value) {
ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
}
c.Next()
}
func Login(c *gin.Context) { func Login(c *gin.Context) {
SuccessResp(c) SuccessResp(c)
} }
func CheckAccount(c *gin.Context) {
if model.AccountsCount() == 0 {
ErrorResp(c, fmt.Errorf("no accounts,please add one first"), 1001)
return
}
c.Next()
}
func CheckParent(path string, password string) bool { func CheckParent(path string, password string) bool {
meta, err := model.GetMetaByPath(path) meta, err := model.GetMetaByPath(path)
if err == nil { if err == nil {

View File

@ -1,4 +1,4 @@
package server package common
import ( import (
"fmt" "fmt"
@ -15,6 +15,11 @@ type Resp struct {
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
type PathReq struct {
Path string `json:"path"`
Password string `json:"password"`
}
func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) { func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
var path, name string var path, name string
switch model.AccountsCount() { switch model.AccountsCount() {

View File

@ -1,9 +1,10 @@
package server package controllers
import ( import (
"fmt" "fmt"
"github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strconv" "strconv"
@ -13,52 +14,52 @@ import (
func GetAccounts(c *gin.Context) { func GetAccounts(c *gin.Context) {
accounts, err := model.GetAccounts() accounts, err := model.GetAccounts()
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
SuccessResp(c, accounts) common.SuccessResp(c, accounts)
} }
func CreateAccount(c *gin.Context) { func CreateAccount(c *gin.Context) {
var req model.Account var req model.Account
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
driver, ok := base.GetDriver(req.Type) driver, ok := base.GetDriver(req.Type)
if !ok { if !ok {
ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400) common.ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400)
return return
} }
now := time.Now() now := time.Now()
req.UpdatedAt = &now req.UpdatedAt = &now
if err := model.CreateAccount(&req); err != nil { if err := model.CreateAccount(&req); err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
} else { } else {
log.Debugf("new account: %+v", req) log.Debugf("new account: %+v", req)
err = driver.Save(&req, nil) err = driver.Save(&req, nil)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
SuccessResp(c) common.SuccessResp(c)
} }
} }
func SaveAccount(c *gin.Context) { func SaveAccount(c *gin.Context) {
var req model.Account var req model.Account
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
driver, ok := base.GetDriver(req.Type) driver, ok := base.GetDriver(req.Type)
if !ok { if !ok {
ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400) common.ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400)
return return
} }
old, err := model.GetAccountById(req.ID) old, err := model.GetAccountById(req.ID)
if err != nil { if err != nil {
ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
now := time.Now() now := time.Now()
@ -67,15 +68,15 @@ func SaveAccount(c *gin.Context) {
model.DeleteAccountFromMap(old.Name) model.DeleteAccountFromMap(old.Name)
} }
if err := model.SaveAccount(&req); err != nil { if err := model.SaveAccount(&req); err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
} else { } else {
log.Debugf("save account: %+v", req) log.Debugf("save account: %+v", req)
err = driver.Save(&req, old) err = driver.Save(&req, old)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
SuccessResp(c) common.SuccessResp(c)
} }
} }
@ -83,12 +84,12 @@ func DeleteAccount(c *gin.Context) {
idStr := c.Query("id") idStr := c.Query("id")
id, err := strconv.Atoi(idStr) id, err := strconv.Atoi(idStr)
if err != nil { if err != nil {
ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
if err := model.DeleteAccount(uint(id)); err != nil { if err := model.DeleteAccount(uint(id)); err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
SuccessResp(c) common.SuccessResp(c)
} }

View File

@ -1,15 +1,16 @@
package server package controllers
import ( import (
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func ClearCache(c *gin.Context) { func ClearCache(c *gin.Context) {
err := conf.Cache.Clear(conf.Ctx) err := conf.Cache.Clear(conf.Ctx)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
} else { } else {
SuccessResp(c) common.SuccessResp(c)
} }
} }

View File

@ -1,8 +1,9 @@
package server package controllers
import ( import (
"fmt" "fmt"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
@ -17,55 +18,40 @@ func Down(c *gin.Context) {
rawPath := c.Param("path") rawPath := c.Param("path")
rawPath = utils.ParsePath(rawPath) rawPath = utils.ParsePath(rawPath)
log.Debugf("down: %s", rawPath) log.Debugf("down: %s", rawPath)
pw := c.Query("pw") account, path, driver, err := common.ParsePath(rawPath)
if !CheckDownLink(utils.Dir(rawPath), pw, utils.Base(rawPath)) {
ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
}
account, path, driver, err := ParsePath(rawPath)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
if account.Type == "GoogleDrive" { if driver.Config().OnlyProxy {
Proxy(c) Proxy(c)
return return
} }
link, err := driver.Link(path, account) link, err := driver.Link(path, account)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return
}
if account.Type == "Native" {
c.File(link)
return
} else {
c.Redirect(302, link)
return return
} }
c.Redirect(302, link)
return
} }
func Proxy(c *gin.Context) { func Proxy(c *gin.Context) {
rawPath := c.Param("path") rawPath := c.Param("path")
rawPath = utils.ParsePath(rawPath) rawPath = utils.ParsePath(rawPath)
log.Debugf("proxy: %s", rawPath) log.Debugf("proxy: %s", rawPath)
pw := c.Query("pw") account, path, driver, err := common.ParsePath(rawPath)
if !CheckDownLink(utils.Dir(rawPath), pw, utils.Base(rawPath)) {
ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
}
account, path, driver, err := ParsePath(rawPath)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT { if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403) common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
return return
} }
link, err := driver.Link(path, account) link, err := driver.Link(path, account)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
if account.Type == "Native" { if account.Type == "Native" {
@ -81,7 +67,7 @@ func Proxy(c *gin.Context) {
w := c.Writer w := c.Writer
target, err := url.Parse(link) target, err := url.Parse(link)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
protocol := "http://" protocol := "http://"
@ -106,7 +92,7 @@ func init() {
func Text(c *gin.Context, link string) { func Text(c *gin.Context, link string) {
res, err := client.R().Get(link) res, err := client.R().Get(link)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
text := res.String() text := res.String()
@ -115,7 +101,7 @@ func Text(c *gin.Context, link string) {
if t != utils.UTF8 { if t != utils.UTF8 {
body, err := utils.GbkToUtf8(res.Body()) body, err := utils.GbkToUtf8(res.Body())
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
text = string(body) text = string(body)

View File

@ -1,10 +1,11 @@
package server package controllers
import ( import (
"github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func GetDrivers(c *gin.Context) { func GetDrivers(c *gin.Context) {
SuccessResp(c, base.GetDrivers()) common.SuccessResp(c, base.GetDrivers())
} }

View File

@ -1,7 +1,8 @@
package server package controllers
import ( import (
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strconv" "strconv"
@ -10,37 +11,37 @@ import (
func GetMetas(c *gin.Context) { func GetMetas(c *gin.Context) {
metas,err := model.GetMetas() metas,err := model.GetMetas()
if err != nil { if err != nil {
ErrorResp(c,err,500) common.ErrorResp(c,err,500)
return return
} }
SuccessResp(c, metas) common.SuccessResp(c, metas)
} }
func CreateMeta(c *gin.Context) { func CreateMeta(c *gin.Context) {
var req model.Meta var req model.Meta
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
req.Path = utils.ParsePath(req.Path) req.Path = utils.ParsePath(req.Path)
if err := model.CreateMeta(req); err != nil { if err := model.CreateMeta(req); err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
} else { } else {
SuccessResp(c) common.SuccessResp(c)
} }
} }
func SaveMeta(c *gin.Context) { func SaveMeta(c *gin.Context) {
var req model.Meta var req model.Meta
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
req.Path = utils.ParsePath(req.Path) req.Path = utils.ParsePath(req.Path)
if err := model.SaveMeta(req); err != nil { if err := model.SaveMeta(req); err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
} else { } else {
SuccessResp(c) common.SuccessResp(c)
} }
} }
@ -48,13 +49,13 @@ func DeleteMeta(c *gin.Context) {
idStr := c.Query("id") idStr := c.Query("id")
id, err := strconv.Atoi(idStr) id, err := strconv.Atoi(idStr)
if err != nil { if err != nil {
ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
//path = utils.ParsePath(path) //path = utils.ParsePath(path)
if err := model.DeleteMeta(uint(id)); err != nil { if err := model.DeleteMeta(uint(id)); err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
SuccessResp(c) common.SuccessResp(c)
} }

View File

@ -1,74 +1,52 @@
package server package controllers
import ( import (
"fmt" "fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strings" "strings"
) )
type PathReq struct {
Path string `json:"path"`
Password string `json:"password"`
}
func Path(c *gin.Context) { func Path(c *gin.Context) {
var req PathReq reqV,_ := c.Get("req")
if err := c.ShouldBind(&req); err != nil { req := reqV.(common.PathReq)
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 {
ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
}
// TODO hide or ignore?
} else if conf.CheckParent {
if !CheckParent(utils.Dir(req.Path), req.Password) {
ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
}
}
if model.AccountsCount() > 1 && req.Path == "/" { if model.AccountsCount() > 1 && req.Path == "/" {
files, err := model.GetAccountFiles() files, err := model.GetAccountFiles()
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
c.JSON(200, Resp{ c.JSON(200, common.Resp{
Code: 200, Code: 200,
Message: "folder", Message: "folder",
Data: files, Data: files,
}) })
return return
} }
account, path, driver, err := ParsePath(req.Path) account, path, driver, err := common.ParsePath(req.Path)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
file, files, err := driver.Path(path, account) file, files, err := driver.Path(path, account)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
if file != nil { if file != nil {
if account.Type == "Native" { if driver.Config().OnlyProxy {
file.Url = fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path) file.Url = fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path)
} }
c.JSON(200, Resp{ c.JSON(200, common.Resp{
Code: 200, Code: 200,
Message: "file", Message: "file",
Data: []*model.File{file}, Data: []*model.File{file},
}) })
} else { } else {
meta, _ := model.GetMetaByPath(req.Path)
if meta != nil && meta.Hide != "" { if meta != nil && meta.Hide != "" {
tmpFiles := make([]model.File, 0) tmpFiles := make([]model.File, 0)
hideFiles := strings.Split(meta.Hide, ",") hideFiles := strings.Split(meta.Hide, ",")
@ -79,7 +57,7 @@ func Path(c *gin.Context) {
} }
files = tmpFiles files = tmpFiles
} }
c.JSON(200, Resp{ c.JSON(200, common.Resp{
Code: 200, Code: 200,
Message: "folder", Message: "folder",
Data: files, Data: files,
@ -88,31 +66,28 @@ func Path(c *gin.Context) {
} }
func Link(c *gin.Context) { func Link(c *gin.Context) {
var req PathReq reqV,_ := c.Get("req")
if err := c.ShouldBind(&req); err != nil { req := reqV.(common.PathReq)
ErrorResp(c, err, 400)
return
}
rawPath := req.Path rawPath := req.Path
rawPath = utils.ParsePath(rawPath) rawPath = utils.ParsePath(rawPath)
log.Debugf("link: %s", rawPath) log.Debugf("link: %s", rawPath)
account, path, driver, err := ParsePath(rawPath) account, path, driver, err := common.ParsePath(rawPath)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
link, err := driver.Link(path, account) link, err := driver.Link(path, account)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
if account.Type == "Native" { if driver.Config().OnlyProxy {
SuccessResp(c, gin.H{ common.SuccessResp(c, gin.H{
"url": fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path), "url": fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path),
}) })
return return
} else { } else {
SuccessResp(c, gin.H{ common.SuccessResp(c, gin.H{
"url": link, "url": link,
}) })
return return
@ -120,23 +95,20 @@ func Link(c *gin.Context) {
} }
func Preview(c *gin.Context) { func Preview(c *gin.Context) {
var req PathReq reqV,_ := c.Get("req")
if err := c.ShouldBind(&req); err != nil { req := reqV.(common.PathReq)
ErrorResp(c, err, 400)
return
}
rawPath := req.Path rawPath := req.Path
rawPath = utils.ParsePath(rawPath) rawPath = utils.ParsePath(rawPath)
log.Debugf("preview: %s", rawPath) log.Debugf("preview: %s", rawPath)
account, path, driver, err := ParsePath(rawPath) account, path, driver, err := common.ParsePath(rawPath)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
data, err := driver.Preview(path, account) data, err := driver.Preview(path, account)
if err != nil { if err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
} else { } else {
SuccessResp(c, data) common.SuccessResp(c, data)
} }
} }

View File

@ -1,38 +1,39 @@
package server package controllers
import ( import (
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func SaveSettings(c *gin.Context) { func SaveSettings(c *gin.Context) {
var req []model.SettingItem var req []model.SettingItem
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
if err := model.SaveSettings(req); err != nil { if err := model.SaveSettings(req); err != nil {
ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
} else { } else {
model.LoadSettings() model.LoadSettings()
SuccessResp(c) common.SuccessResp(c)
} }
} }
func GetSettings(c *gin.Context) { func GetSettings(c *gin.Context) {
settings, err := model.GetSettings() settings, err := model.GetSettings()
if err != nil { if err != nil {
ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
SuccessResp(c, settings) common.SuccessResp(c, settings)
} }
func GetSettingsPublic(c *gin.Context) { func GetSettingsPublic(c *gin.Context) {
settings, err := model.GetSettingsPublic() settings, err := model.GetSettingsPublic()
if err != nil { if err != nil {
ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
SuccessResp(c, settings) common.SuccessResp(c, settings)
} }

View File

@ -0,0 +1,16 @@
package middlewares
import (
"fmt"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
)
func CheckAccount(c *gin.Context) {
if model.AccountsCount() == 0 {
common.ErrorResp(c, fmt.Errorf("no accounts,please add one first"), 1001)
return
}
c.Next()
}

View File

@ -0,0 +1,28 @@
package middlewares
import (
"fmt"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func Auth(c *gin.Context) {
token := c.GetHeader("Authorization")
password, err := model.GetSettingByKey("password")
if err != nil {
if err == gorm.ErrRecordNotFound {
common.ErrorResp(c, fmt.Errorf("password not set"), 400)
return
}
common.ErrorResp(c, err, 500)
return
}
if token != utils.GetMD5Encode(password.Value) {
common.ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
}
c.Next()
}

View File

@ -0,0 +1,20 @@
package middlewares
import (
"fmt"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
func DownCheck(c *gin.Context) {
rawPath := c.Param("path")
rawPath = utils.ParsePath(rawPath)
pw := c.Query("pw")
if !common.CheckDownLink(utils.Dir(rawPath), pw, utils.Base(rawPath)) {
common.ErrorResp(c, fmt.Errorf("wrong password"), 401)
c.Abort()
return
}
c.Next()
}

View File

@ -0,0 +1,35 @@
package middlewares
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
func PathCheck(c *gin.Context) {
var req common.PathReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
req.Path = utils.ParsePath(req.Path)
c.Set("req",req)
meta, err := model.GetMetaByPath(req.Path)
if err == nil {
if meta.Password != "" && meta.Password != req.Password {
common.ErrorResp(c, fmt.Errorf("wrong password"), 401)
c.Abort()
return
}
} else if conf.CheckParent {
if !common.CheckParent(utils.Dir(req.Path), req.Password) {
common.ErrorResp(c, fmt.Errorf("wrong password"), 401)
c.Abort()
return
}
}
c.Next()
}

View File

@ -1,6 +1,9 @@
package server package server
import ( import (
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/server/controllers"
"github.com/Xhofe/alist/server/middlewares"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -9,35 +12,37 @@ func InitApiRouter(r *gin.Engine) {
// TODO from settings // TODO from settings
Cors(r) Cors(r)
r.GET("/d/*path", Down) r.GET("/d/*path", middlewares.DownCheck, controllers.Down)
r.GET("/p/*path", Proxy) r.GET("/p/*path", middlewares.DownCheck, controllers.Proxy)
api := r.Group("/api") api := r.Group("/api")
public := api.Group("/public") public := api.Group("/public")
{ {
public.POST("/path", CheckAccount, Path) path := public.Group("", middlewares.PathCheck, middlewares.CheckAccount)
public.POST("/preview", CheckAccount, Preview) path.POST("/path", controllers.Path)
public.GET("/settings", GetSettingsPublic) path.POST("/preview", controllers.Preview)
public.POST("/link", CheckAccount, Link) path.POST("/link", controllers.Link)
public.GET("/settings", controllers.GetSettingsPublic)
} }
admin := api.Group("/admin") admin := api.Group("/admin")
{ {
admin.Use(Auth) admin.Use(middlewares.Auth)
admin.GET("/login", Login) admin.GET("/login", common.Login)
admin.GET("/settings", GetSettings) admin.GET("/settings", controllers.GetSettings)
admin.POST("/settings", SaveSettings) admin.POST("/settings", controllers.SaveSettings)
admin.POST("/account/create", CreateAccount) admin.POST("/account/create", controllers.CreateAccount)
admin.POST("/account/save", SaveAccount) admin.POST("/account/save", controllers.SaveAccount)
admin.GET("/accounts", GetAccounts) admin.GET("/accounts", controllers.GetAccounts)
admin.DELETE("/account", DeleteAccount) admin.DELETE("/account", controllers.DeleteAccount)
admin.GET("/drivers", GetDrivers) admin.GET("/drivers", controllers.GetDrivers)
admin.GET("/clear_cache", ClearCache) admin.GET("/clear_cache", controllers.ClearCache)
admin.GET("/metas", GetMetas) admin.GET("/metas", controllers.GetMetas)
admin.POST("/meta/create", CreateMeta) admin.POST("/meta/create", controllers.CreateMeta)
admin.POST("/meta/save", SaveMeta) admin.POST("/meta/save", controllers.SaveMeta)
admin.DELETE("/meta", DeleteMeta) admin.DELETE("/meta", controllers.DeleteMeta)
} }
Static(r) Static(r)
WebDav(r) WebDav(r)