chore: change whether print log

refactor/fs
Noah Hsu 2022-06-26 19:20:19 +08:00
parent c67f128f15
commit 6b9bca893b
5 changed files with 40 additions and 36 deletions

View File

@ -5,8 +5,10 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func ErrorResp(c *gin.Context, err error, code int, l ...bool) { // ErrorResp is used to return error response
if len(l) != 0 && l[0] { // @param nl: if true, don't log error
func ErrorResp(c *gin.Context, err error, code int, nl ...bool) {
if len(nl) == 0 || !nl[0] {
log.Errorf("%+v", err) log.Errorf("%+v", err)
} }
c.JSON(200, Resp{ c.JSON(200, Resp{
@ -17,8 +19,10 @@ func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
c.Abort() c.Abort()
} }
func ErrorStrResp(c *gin.Context, str string, code int) { func ErrorStrResp(c *gin.Context, str string, code int, l ...bool) {
log.Error(str) if len(l) != 0 && l[0] {
log.Error(str)
}
c.JSON(200, Resp{ c.JSON(200, Resp{
Code: code, Code: code,
Message: str, Message: str,

View File

@ -4,7 +4,7 @@ import (
"github.com/Xhofe/go-cache" "github.com/Xhofe/go-cache"
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
common2 "github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"time" "time"
) )
@ -25,34 +25,34 @@ func Login(c *gin.Context) {
ip := c.ClientIP() ip := c.ClientIP()
count, ok := loginCache.Get(ip) count, ok := loginCache.Get(ip)
if ok && count >= defaultTimes { if ok && count >= defaultTimes {
common2.ErrorStrResp(c, "Too many unsuccessful sign-in attempts have been made using an incorrect password. Try again later.", 403) common.ErrorStrResp(c, "Too many unsuccessful sign-in attempts have been made using an incorrect password. Try again later.", 403)
loginCache.Expire(ip, defaultDuration) loginCache.Expire(ip, defaultDuration)
return return
} }
// check username // check username
var req LoginReq var req LoginReq
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common2.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400, true)
return return
} }
user, err := db.GetUserByName(req.Username) user, err := db.GetUserByName(req.Username)
if err != nil { if err != nil {
common2.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400, true)
return return
} }
// validate password // validate password
if err := user.ValidatePassword(req.Password); err != nil { if err := user.ValidatePassword(req.Password); err != nil {
common2.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400, true)
loginCache.Set(ip, count+1) loginCache.Set(ip, count+1)
return return
} }
// generate token // generate token
token, err := common2.GenerateToken(user.Username) token, err := common.GenerateToken(user.Username)
if err != nil { if err != nil {
common2.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
common2.SuccessResp(c, gin.H{"token": token}) common.SuccessResp(c, gin.H{"token": token})
loginCache.Del(ip) loginCache.Del(ip)
} }
@ -61,5 +61,5 @@ func Login(c *gin.Context) {
func CurrentUser(c *gin.Context) { func CurrentUser(c *gin.Context) {
user := c.MustGet("user").(*model.User) user := c.MustGet("user").(*model.User)
user.Password = "" user.Password = ""
common2.SuccessResp(c, gin.H{"user": user}) common.SuccessResp(c, gin.H{"user": user})
} }

View File

@ -4,25 +4,25 @@ import (
"github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils"
common2 "github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strconv" "strconv"
) )
func ListMetas(c *gin.Context) { func ListMetas(c *gin.Context) {
var req common2.PageReq var req common.PageReq
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
common2.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400, true)
return return
} }
log.Debugf("%+v", req) log.Debugf("%+v", req)
metas, total, err := db.GetMetas(req.PageIndex, req.PageSize) metas, total, err := db.GetMetas(req.PageIndex, req.PageSize)
if err != nil { if err != nil {
common2.ErrorResp(c, err, 500, true) common.ErrorResp(c, err, 500)
return return
} }
common2.SuccessResp(c, common2.PageResp{ common.SuccessResp(c, common.PageResp{
Content: metas, Content: metas,
Total: total, Total: total,
}) })
@ -31,28 +31,28 @@ func ListMetas(c *gin.Context) {
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 {
common2.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400, true)
return return
} }
req.Path = utils.StandardizePath(req.Path) req.Path = utils.StandardizePath(req.Path)
if err := db.CreateMeta(&req); err != nil { if err := db.CreateMeta(&req); err != nil {
common2.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
} else { } else {
common2.SuccessResp(c) common.SuccessResp(c)
} }
} }
func UpdateMeta(c *gin.Context) { func UpdateMeta(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 {
common2.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400, true)
return return
} }
req.Path = utils.StandardizePath(req.Path) req.Path = utils.StandardizePath(req.Path)
if err := db.UpdateMeta(&req); err != nil { if err := db.UpdateMeta(&req); err != nil {
common2.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
} else { } else {
common2.SuccessResp(c) common.SuccessResp(c)
} }
} }
@ -60,12 +60,12 @@ 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 {
common2.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400, true)
return return
} }
if err := db.DeleteMetaById(uint(id)); err != nil { if err := db.DeleteMetaById(uint(id)); err != nil {
common2.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
common2.SuccessResp(c) common.SuccessResp(c)
} }

View File

@ -14,7 +14,7 @@ func Auth(c *gin.Context) {
if token == "" { if token == "" {
guest, err := db.GetGuest() guest, err := db.GetGuest()
if err != nil { if err != nil {
common2.ErrorResp(c, err, 500, true) common2.ErrorResp(c, err, 500)
c.Abort() c.Abort()
return return
} }
@ -24,7 +24,7 @@ func Auth(c *gin.Context) {
} }
userClaims, err := common2.ParseToken(token) userClaims, err := common2.ParseToken(token)
if err != nil { if err != nil {
common2.ErrorResp(c, err, 401) common2.ErrorResp(c, err, 401, true)
c.Abort() c.Abort()
return return
} }

View File

@ -3,7 +3,7 @@ package server
import ( import (
"github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/server/common" "github.com/alist-org/alist/v3/server/common"
controllers2 "github.com/alist-org/alist/v3/server/controllers" "github.com/alist-org/alist/v3/server/controllers"
"github.com/alist-org/alist/v3/server/middlewares" "github.com/alist-org/alist/v3/server/middlewares"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -14,16 +14,16 @@ func Init(r *gin.Engine) {
Cors(r) Cors(r)
api := r.Group("/api", middlewares.Auth) api := r.Group("/api", middlewares.Auth)
api.POST("/auth/login", controllers2.Login) api.POST("/auth/login", controllers.Login)
api.GET("/auth/current", controllers2.CurrentUser) api.GET("/auth/current", controllers.CurrentUser)
admin := api.Group("/admin", middlewares.AuthAdmin) admin := api.Group("/admin", middlewares.AuthAdmin)
meta := admin.Group("/meta") meta := admin.Group("/meta")
meta.GET("/list", controllers2.ListMetas) meta.GET("/list", controllers.ListMetas)
meta.POST("/create", controllers2.CreateMeta) meta.POST("/create", controllers.CreateMeta)
meta.POST("/update", controllers2.UpdateMeta) meta.POST("/update", controllers.UpdateMeta)
meta.POST("/delete", controllers2.DeleteMeta) meta.POST("/delete", controllers.DeleteMeta)
} }
func Cors(r *gin.Engine) { func Cors(r *gin.Engine) {