diff --git a/server/common/common.go b/server/common/common.go index 89d217882fc..1bf6247525b 100644 --- a/server/common/common.go +++ b/server/common/common.go @@ -5,8 +5,10 @@ import ( log "github.com/sirupsen/logrus" ) -func ErrorResp(c *gin.Context, err error, code int, l ...bool) { - if len(l) != 0 && l[0] { +// ErrorResp is used to return error response +// @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) } c.JSON(200, Resp{ @@ -17,8 +19,10 @@ func ErrorResp(c *gin.Context, err error, code int, l ...bool) { c.Abort() } -func ErrorStrResp(c *gin.Context, str string, code int) { - log.Error(str) +func ErrorStrResp(c *gin.Context, str string, code int, l ...bool) { + if len(l) != 0 && l[0] { + log.Error(str) + } c.JSON(200, Resp{ Code: code, Message: str, diff --git a/server/controllers/login.go b/server/controllers/login.go index adc1c3d9239..acd8282cddf 100644 --- a/server/controllers/login.go +++ b/server/controllers/login.go @@ -4,7 +4,7 @@ import ( "github.com/Xhofe/go-cache" "github.com/alist-org/alist/v3/internal/db" "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" "time" ) @@ -25,34 +25,34 @@ func Login(c *gin.Context) { ip := c.ClientIP() count, ok := loginCache.Get(ip) 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) return } // check username var req LoginReq if err := c.ShouldBind(&req); err != nil { - common2.ErrorResp(c, err, 400) + common.ErrorResp(c, err, 400, true) return } user, err := db.GetUserByName(req.Username) if err != nil { - common2.ErrorResp(c, err, 400) + common.ErrorResp(c, err, 400, true) return } // validate password if err := user.ValidatePassword(req.Password); err != nil { - common2.ErrorResp(c, err, 400) + common.ErrorResp(c, err, 400, true) loginCache.Set(ip, count+1) return } // generate token - token, err := common2.GenerateToken(user.Username) + token, err := common.GenerateToken(user.Username) if err != nil { - common2.ErrorResp(c, err, 400) + common.ErrorResp(c, err, 400) return } - common2.SuccessResp(c, gin.H{"token": token}) + common.SuccessResp(c, gin.H{"token": token}) loginCache.Del(ip) } @@ -61,5 +61,5 @@ func Login(c *gin.Context) { func CurrentUser(c *gin.Context) { user := c.MustGet("user").(*model.User) user.Password = "" - common2.SuccessResp(c, gin.H{"user": user}) + common.SuccessResp(c, gin.H{"user": user}) } diff --git a/server/controllers/meta.go b/server/controllers/meta.go index b8a4c5b4deb..b5aa4ca9032 100644 --- a/server/controllers/meta.go +++ b/server/controllers/meta.go @@ -4,25 +4,25 @@ import ( "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/model" "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" log "github.com/sirupsen/logrus" "strconv" ) func ListMetas(c *gin.Context) { - var req common2.PageReq + var req common.PageReq if err := c.ShouldBind(&req); err != nil { - common2.ErrorResp(c, err, 400) + common.ErrorResp(c, err, 400, true) return } log.Debugf("%+v", req) metas, total, err := db.GetMetas(req.PageIndex, req.PageSize) if err != nil { - common2.ErrorResp(c, err, 500, true) + common.ErrorResp(c, err, 500) return } - common2.SuccessResp(c, common2.PageResp{ + common.SuccessResp(c, common.PageResp{ Content: metas, Total: total, }) @@ -31,28 +31,28 @@ func ListMetas(c *gin.Context) { func CreateMeta(c *gin.Context) { var req model.Meta if err := c.ShouldBind(&req); err != nil { - common2.ErrorResp(c, err, 400) + common.ErrorResp(c, err, 400, true) return } req.Path = utils.StandardizePath(req.Path) if err := db.CreateMeta(&req); err != nil { - common2.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500) } else { - common2.SuccessResp(c) + common.SuccessResp(c) } } func UpdateMeta(c *gin.Context) { var req model.Meta if err := c.ShouldBind(&req); err != nil { - common2.ErrorResp(c, err, 400) + common.ErrorResp(c, err, 400, true) return } req.Path = utils.StandardizePath(req.Path) if err := db.UpdateMeta(&req); err != nil { - common2.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500) } else { - common2.SuccessResp(c) + common.SuccessResp(c) } } @@ -60,12 +60,12 @@ func DeleteMeta(c *gin.Context) { idStr := c.Query("id") id, err := strconv.Atoi(idStr) if err != nil { - common2.ErrorResp(c, err, 400) + common.ErrorResp(c, err, 400, true) return } if err := db.DeleteMetaById(uint(id)); err != nil { - common2.ErrorResp(c, err, 500) + common.ErrorResp(c, err, 500) return } - common2.SuccessResp(c) + common.SuccessResp(c) } diff --git a/server/middlewares/auth.go b/server/middlewares/auth.go index a2ea8da30fa..9ae3623524b 100644 --- a/server/middlewares/auth.go +++ b/server/middlewares/auth.go @@ -14,7 +14,7 @@ func Auth(c *gin.Context) { if token == "" { guest, err := db.GetGuest() if err != nil { - common2.ErrorResp(c, err, 500, true) + common2.ErrorResp(c, err, 500) c.Abort() return } @@ -24,7 +24,7 @@ func Auth(c *gin.Context) { } userClaims, err := common2.ParseToken(token) if err != nil { - common2.ErrorResp(c, err, 401) + common2.ErrorResp(c, err, 401, true) c.Abort() return } diff --git a/server/router.go b/server/router.go index 52c127494af..17841be9326 100644 --- a/server/router.go +++ b/server/router.go @@ -3,7 +3,7 @@ package server import ( "github.com/alist-org/alist/v3/internal/conf" "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/gin-contrib/cors" "github.com/gin-gonic/gin" @@ -14,16 +14,16 @@ func Init(r *gin.Engine) { Cors(r) api := r.Group("/api", middlewares.Auth) - api.POST("/auth/login", controllers2.Login) - api.GET("/auth/current", controllers2.CurrentUser) + api.POST("/auth/login", controllers.Login) + api.GET("/auth/current", controllers.CurrentUser) admin := api.Group("/admin", middlewares.AuthAdmin) meta := admin.Group("/meta") - meta.GET("/list", controllers2.ListMetas) - meta.POST("/create", controllers2.CreateMeta) - meta.POST("/update", controllers2.UpdateMeta) - meta.POST("/delete", controllers2.DeleteMeta) + meta.GET("/list", controllers.ListMetas) + meta.POST("/create", controllers.CreateMeta) + meta.POST("/update", controllers.UpdateMeta) + meta.POST("/delete", controllers.DeleteMeta) } func Cors(r *gin.Engine) {