Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: request Limiter. #71

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions api/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/NJUPT-SAST/sast-link-backend/service"
"github.com/NJUPT-SAST/sast-link-backend/util"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

var controllerLogger = log.Log
Expand Down Expand Up @@ -91,10 +90,7 @@ func CheckVerifyCode(ctx *gin.Context) {
func UserInfo(ctx *gin.Context) {
user, err := service.UserInfo(ctx)
if err != nil {
controllerLogger.WithFields(
logrus.Fields{
"username": user.Uid,
}).Error(err)
controllerLogger.Errorf("user token error: %s", err.Error())
ctx.JSON(http.StatusOK, result.Failed(result.GetUserinfoFail))
return
}
Expand All @@ -106,7 +102,7 @@ func UserInfo(ctx *gin.Context) {
}

func SendEmail(ctx *gin.Context) {
//获取传入TICKET参数
// Get TICKET from header
var ticket, flag string
if ctx.GetHeader("REGISTER-TICKET") != "" {
ticket = ctx.GetHeader("REGISTER-TICKET")
Expand All @@ -123,10 +119,7 @@ func SendEmail(ctx *gin.Context) {
// 错误处理机制写玉玉了
// 我开始乱写了啊啊啊啊
if usernameErr != nil {
controllerLogger.WithFields(
logrus.Fields{
"username": username,
}).Error(usernameErr)
controllerLogger.Errorf("username parse error: %s", usernameErr.Error())
ctx.JSON(http.StatusUnauthorized, result.Failed(result.TicketNotCorrect))
return
}
Expand All @@ -145,10 +138,7 @@ func SendEmail(ctx *gin.Context) {
}
err := service.SendEmail(ctx, username, ticket, title)
if err != nil {
controllerLogger.WithFields(
logrus.Fields{
"username": username,
}).Error(err)
controllerLogger.Errorf("send email fail: %s", usernameErr.Error())
ctx.JSON(http.StatusOK, result.Failed(result.HandleError(err)))
} else {
ctx.JSON(http.StatusOK, result.Success(nil))
Expand Down Expand Up @@ -178,11 +168,7 @@ func VerifyAccount(ctx *gin.Context) {

ticket, err := service.VerifyAccount(ctx, username, flag)
if err != nil {
controllerLogger.WithFields(
logrus.Fields{
"username": username,
}).Error(err)

controllerLogger.Errorf("verify account fail: %s", err.Error())
ctx.JSON(http.StatusOK, result.Failed(result.HandleError(err)))
return
}
Expand All @@ -207,15 +193,10 @@ func Login(ctx *gin.Context) {
ctx.JSON(http.StatusOK, result.Failed(result.TicketNotCorrect))
return
}
//verify if the user is deleted

// Check the password
uid, err := service.Login(username, password)
if err != nil {
controllerLogger.WithFields(
logrus.Fields{
"username": username,
}).Error(err)
controllerLogger.Errorf("login fail: %s", err.Error())
//ctx.JSON(http.StatusUnauthorized, result.Failed(result.VerifyAccountError))
ctx.AbortWithStatusJSON(http.StatusUnauthorized, result.Failed(result.VerifyPasswordError))
return
Expand Down
22 changes: 14 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module github.com/NJUPT-SAST/sast-link-backend
go 1.20

require (
github.com/gin-gonic/gin v1.9.0
github.com/gin-gonic/gin v1.9.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.3
gorm.io/driver/postgres v1.5.0
gorm.io/gorm v1.25.0
)

require (
github.com/didip/tollbooth/v7 v7.0.1
github.com/go-oauth2/oauth2/v4 v4.5.2
github.com/go-session/redis/v3 v3.1.0
github.com/go-session/session/v3 v3.1.5
Expand Down Expand Up @@ -39,17 +40,20 @@ require (
)

require (
github.com/bytedance/sonic v1.8.8 // indirect
github.com/bytedance/sonic v1.9.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/clbanning/mxj v1.8.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/frankban/quicktest v1.14.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/logger v0.2.6 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-pkgz/expirable-cache v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.13.0 // indirect
github.com/go-playground/validator/v10 v10.14.1 // indirect
github.com/go-redis/redis/v8 v8.11.4 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
Expand All @@ -71,23 +75,25 @@ require (
github.com/jmoiron/sqlx v1.3.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mozillazg/go-httpheader v0.4.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/zerolog v1.29.1 // indirect
github.com/smarty/assertions v1.15.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/vgarvardt/pgx-helpers/v4 v4.0.0-20200225100150-876aee3d1a22 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
Expand Down
Loading