Skip to content

Commit

Permalink
chore: change whether print log
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 26, 2022
1 parent c67f128 commit 6b9bca8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 36 deletions.
12 changes: 8 additions & 4 deletions server/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions server/controllers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}

Expand All @@ -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})
}
28 changes: 14 additions & 14 deletions server/controllers/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -31,41 +31,41 @@ 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)
}
}

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)
}
4 changes: 2 additions & 2 deletions server/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down

0 comments on commit 6b9bca8

Please sign in to comment.