Skip to content

Commit

Permalink
Refactor some shared code to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Aug 3, 2024
1 parent e01be54 commit cb6a4b0
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 66 deletions.
18 changes: 9 additions & 9 deletions cmd/web_server/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/gin-gonic/gin"
"github.com/oj-lab/oj-lab-platform/cmd/web_server/middleware"
user_model "github.com/oj-lab/oj-lab-platform/models/user"
"github.com/oj-lab/oj-lab-platform/modules"
gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
auth_module "github.com/oj-lab/oj-lab-platform/modules/auth"
log_module "github.com/oj-lab/oj-lab-platform/modules/log"
gin_utils "github.com/oj-lab/oj-lab-platform/modules/utils/gin"
user_service "github.com/oj-lab/oj-lab-platform/services/user"
)

Expand All @@ -30,14 +30,14 @@ func githubCallback(ginCtx *gin.Context) {
code := ginCtx.Query("code")
tokenResponse, err := auth_module.GetGithubAccessToken(code)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to get github access token: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to get github access token: %v", err))
return
}

log_module.AppLogger().WithField("tokenResponse", tokenResponse).Info("github callback")
githubUser, err := auth_module.GetGithubUser(tokenResponse.AccessToken)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to get github user: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to get github user: %v", err))
return
}

Expand All @@ -50,14 +50,14 @@ func githubCallback(ginCtx *gin.Context) {
AvatarURL: githubUser.AvatarURL,
})
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to create user: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to create user: %v", err))
return
}
}

ls, err := user_service.StartLoginSession(ginCtx, user.Account)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to start login session: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to start login session: %v", err))
}

middleware.SetLoginSessionKeyCookie(ginCtx, ls.Key)
Expand All @@ -68,7 +68,7 @@ func githubCallback(ginCtx *gin.Context) {
func loginGithub(ginCtx *gin.Context) {
u, err := auth_module.GetGithubOauthEntryURL(callbackURL)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to get github oauth entry url: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to get github oauth entry url: %v", err))
}
ginCtx.Redirect(http.StatusFound, u.String())
}
Expand All @@ -91,19 +91,19 @@ func loginByPassword(ginCtx *gin.Context) {
body := &loginBody{}
err := ginCtx.BindJSON(body)
if err != nil {
modules.NewInvalidParamError("body", "invalid body").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "body", "invalid body")
return
}

db := gorm_agent.GetDefaultDB()
user, err := user_model.GetUserByAccountPassword(db, body.Account, body.Password)
if err != nil {
modules.NewUnauthorizedError("account or password incorrect").AppendToGin(ginCtx)
gin_utils.NewUnauthorizedError(ginCtx, "account or password incorrect")
}

ls, err := user_service.StartLoginSession(ginCtx, user.Account)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to login: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to login: %v", err))
return
}
middleware.SetLoginSessionKeyCookie(ginCtx, ls.Key)
Expand Down
12 changes: 6 additions & 6 deletions cmd/web_server/handler/judge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/google/uuid"
"github.com/oj-lab/oj-lab-platform/models"
judge_model "github.com/oj-lab/oj-lab-platform/models/judge"
"github.com/oj-lab/oj-lab-platform/modules"
gin_utils "github.com/oj-lab/oj-lab-platform/modules/utils/gin"
judge_service "github.com/oj-lab/oj-lab-platform/services/judge"
)

Expand All @@ -24,13 +24,13 @@ func getJudge(ginCtx *gin.Context) {
uidString := ginCtx.Param("uid")
uid, err := uuid.Parse(uidString)
if err != nil {
modules.NewInvalidParamError("uid", "invalid uid").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "uid", "invalid uid")
return
}

judge, err := judge_service.GetJudge(ginCtx, uid)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to get judge: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to get judge: %v", err))
return
}

Expand Down Expand Up @@ -63,12 +63,12 @@ func getJudgeList(ginCtx *gin.Context) {

limit, err := strconv.Atoi(limitQuery)
if err != nil {
modules.NewInvalidParamError("limit", "invalid limit").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "limit", "invalid limit")
return
}
offset, err := strconv.Atoi(offsetQuery)
if err != nil {
modules.NewInvalidParamError("offset", "invalid offset").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "offset", "invalid offset")
return
}

Expand All @@ -80,7 +80,7 @@ func getJudgeList(ginCtx *gin.Context) {

judges, total, err := judge_service.GetJudgeList(ginCtx, options)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to get judge list: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to get judge list: %v", err))
return
}

Expand Down
16 changes: 8 additions & 8 deletions cmd/web_server/handler/judge_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
judge_model "github.com/oj-lab/oj-lab-platform/models/judge"
"github.com/oj-lab/oj-lab-platform/modules"
gin_utils "github.com/oj-lab/oj-lab-platform/modules/utils/gin"
judge_service "github.com/oj-lab/oj-lab-platform/services/judge"
)

Expand All @@ -24,20 +24,20 @@ type ReportJudgeResultCountBody struct {
func putReportJudgeResultCount(ginCtx *gin.Context) {
body := ReportJudgeResultCountBody{}
if err := ginCtx.ShouldBindJSON(&body); err != nil {
modules.NewInvalidParamError("body", "invalid body").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "body", "invalid body")
return
}

judgeUID, err := uuid.Parse(body.JudgeUID)
if err != nil {
modules.NewInvalidParamError("judgeUID", "invalid judgeUID").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "judgeUID", "invalid judgeUID")
return
}

if err := judge_service.ReportJudgeResultCount(
ginCtx, judgeUID, body.ResultCount,
); err != nil {
modules.NewInternalError(err.Error()).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, err.Error())
return
}

Expand All @@ -56,17 +56,17 @@ type ReportJudgeResultBody struct {
func postReportJudgeResult(ginCtx *gin.Context) {
body := ReportJudgeResultBody{}
if err := ginCtx.ShouldBindJSON(&body); err != nil {
modules.NewInvalidParamError("body", "invalid body").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "body", "invalid body")
}

judgeUID, err := uuid.Parse(body.JudgeUIDString)
if err != nil {
modules.NewInvalidParamError("judgeUID", "invalid judgeUID").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "judgeUID", "invalid judgeUID")
return
}
verdict := judge_model.JudgeVerdict(body.VerdictString)
if !verdict.IsValid() {
modules.NewInvalidParamError("verdict", "invalid verdict").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "verdict", "invalid verdict")
return
}

Expand All @@ -77,7 +77,7 @@ func postReportJudgeResult(ginCtx *gin.Context) {
MemoryUsageByte: body.MemoryUsageByte,
})
if err != nil {
modules.NewInternalError(err.Error()).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, err.Error())
return
}
}
4 changes: 2 additions & 2 deletions cmd/web_server/handler/judge_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package handler
import (
"github.com/gin-gonic/gin"
judge_model "github.com/oj-lab/oj-lab-platform/models/judge"
"github.com/oj-lab/oj-lab-platform/modules"
gin_utils "github.com/oj-lab/oj-lab-platform/modules/utils/gin"
judge_service "github.com/oj-lab/oj-lab-platform/services/judge"
"github.com/redis/go-redis/v9"
)
Expand Down Expand Up @@ -58,7 +58,7 @@ func putReportJudgeTask(ginCtx *gin.Context) {

verdict := judge_model.JudgeVerdict(body.VerdictString)
if !verdict.IsValid() {
modules.NewInvalidParamError("verdict", "invalid verdict").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "verdict", "invalid verdict")
return
}

Expand Down
32 changes: 28 additions & 4 deletions cmd/web_server/handler/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package handler

import (
"net/http"
"strconv"

"github.com/gin-gonic/gin"
judge_model "github.com/oj-lab/oj-lab-platform/models/judge"
problem_model "github.com/oj-lab/oj-lab-platform/models/problem"
"github.com/oj-lab/oj-lab-platform/modules"
gin_utils "github.com/oj-lab/oj-lab-platform/modules/utils/gin"
judge_service "github.com/oj-lab/oj-lab-platform/services/judge"
problem_service "github.com/oj-lab/oj-lab-platform/services/problem"
)
Expand Down Expand Up @@ -94,9 +95,32 @@ func deleteProblem(ginCtx *gin.Context) {
// @Accept json
// @Success 200
func getProblemInfoList(ginCtx *gin.Context) {
problemInfoList, total, err := problem_service.GetProblemInfoList(ginCtx)
limitStr := ginCtx.Query("limit")
offsetStr := ginCtx.Query("offset")
if limitStr == "" {
limitStr = "10"
}
if offsetStr == "" {
offsetStr = "0"
}
limit, err := strconv.Atoi(limitStr)
if err != nil {
_ = ginCtx.Error(err)
gin_utils.NewInvalidParamError(ginCtx, "limit", err.Error())
return
}
offset, err := strconv.Atoi(offsetStr)
if err != nil {
gin_utils.NewInvalidParamError(ginCtx, "offset", err.Error())
return
}

problemInfoList, total, err := problem_service.GetProblemInfoList(
ginCtx,
&limit,
&offset,
)
if err != nil {
gin_utils.NewInternalError(ginCtx, err.Error())
return
}

Expand Down Expand Up @@ -189,7 +213,7 @@ func postJudge(ginCtx *gin.Context) {
judge := judge_model.NewJudge("", slug, body.Code, body.Language)
result, err := judge_service.CreateJudge(ginCtx, judge)
if err != nil {
modules.NewInternalError(err.Error()).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, err.Error())
return
}

Expand Down
16 changes: 8 additions & 8 deletions cmd/web_server/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/gin-gonic/gin"
"github.com/oj-lab/oj-lab-platform/cmd/web_server/middleware"
user_model "github.com/oj-lab/oj-lab-platform/models/user"
"github.com/oj-lab/oj-lab-platform/modules"
casbin_agent "github.com/oj-lab/oj-lab-platform/modules/agent/casbin"
gin_utils "github.com/oj-lab/oj-lab-platform/modules/utils/gin"
user_service "github.com/oj-lab/oj-lab-platform/services/user"
)

Expand Down Expand Up @@ -68,7 +68,7 @@ func GetUserList(ginCtx *gin.Context) {

users, total, err := user_service.GetUserList(ginCtx, options)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to get user list: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to get user list: %v", err))
return
}

Expand All @@ -89,12 +89,12 @@ func GetUserList(ginCtx *gin.Context) {
func me(ginCtx *gin.Context) {
ls, err := middleware.GetLoginSessionFromGinCtx(ginCtx)
if err != nil {
modules.NewUnauthorizedError("cannot load login session from cookie").AppendToGin(ginCtx)
gin_utils.NewUnauthorizedError(ginCtx, "cannot load login session from cookie")
return
}
user, err := user_service.GetUser(ginCtx, ls.Key.Account)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to get user: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to get user: %v", err))
return
}

Expand All @@ -111,13 +111,13 @@ func grantUserRole(ginCtx *gin.Context) {
body := &grantUserRoleBody{}
err := ginCtx.BindJSON(body)
if err != nil {
modules.NewInvalidParamError("body", "invalid body").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "body", "invalid body")
return
}

err = user_service.GrantUserRole(ginCtx, account, body.Role, body.Domain)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to grant user role: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to grant user role: %v", err))
return
}

Expand All @@ -134,13 +134,13 @@ func revokeUserRole(ginCtx *gin.Context) {
body := &revokeUserRoleBody{}
err := ginCtx.BindJSON(body)
if err != nil {
modules.NewInvalidParamError("body", "invalid body").AppendToGin(ginCtx)
gin_utils.NewInvalidParamError(ginCtx, "body", "invalid body")
return
}

err = user_service.RevokeUserRole(ginCtx, account, body.Role, body.Domain)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to revoke user role: %v", err)).AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to revoke user role: %v", err))
return
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/web_server/middleware/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package middleware

import (
"github.com/gin-gonic/gin"
"github.com/oj-lab/oj-lab-platform/modules"
casbin_agent "github.com/oj-lab/oj-lab-platform/modules/agent/casbin"
log_module "github.com/oj-lab/oj-lab-platform/modules/log"
gin_utils "github.com/oj-lab/oj-lab-platform/modules/utils/gin"
)

func BuildCasbinEnforceHandlerWithDomain(domain string) gin.HandlerFunc {
Expand All @@ -14,20 +14,20 @@ func BuildCasbinEnforceHandlerWithDomain(domain string) gin.HandlerFunc {
method := ginCtx.Request.Method
ls, err := GetLoginSessionFromGinCtx(ginCtx)
if err != nil {
modules.NewUnauthorizedError("cannot load login session from cookie").AppendToGin(ginCtx)
gin_utils.NewUnauthorizedError(ginCtx, "cannot load login session from cookie")
ginCtx.Abort()
return
}

allow, err := enforcer.Enforce(casbin_agent.UserSubjectPrefix+ls.Key.Account, "_", domain, path, method)
if err != nil {
log_module.AppLogger().Errorf("Failed to enforce: %v", err)
modules.NewInternalError("Failed to enforce").AppendToGin(ginCtx)
gin_utils.NewInternalError(ginCtx, "Failed to enforce")
ginCtx.Abort()
return
}
if !allow {
modules.NewUnauthorizedError("Unauthorized").AppendToGin(ginCtx)
gin_utils.NewUnauthorizedError(ginCtx, "Unauthorized")
ginCtx.Abort()
return
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/web_server/middleware/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ package middleware

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/oj-lab/oj-lab-platform/modules"
log_module "github.com/oj-lab/oj-lab-platform/modules/log"
gin_utils "github.com/oj-lab/oj-lab-platform/modules/utils/gin"
)

func GetServiceError(ginErr gin.Error) *modules.SeviceError {
if modules.IsServiceError(ginErr.Meta) {
return ginErr.Meta.(*modules.SeviceError)
func GetServiceError(ginErr gin.Error) *gin_utils.SeviceError {
if gin_utils.IsServiceError(ginErr.Meta) {
return ginErr.Meta.(*gin_utils.SeviceError)
} else {
serviceErr := modules.NewInternalError(fmt.Sprintf("%v", ginErr.Err))
serviceErr := gin_utils.SeviceError{
Code: http.StatusInternalServerError,
Msg: fmt.Sprintf("%v", ginErr.Err),
}
serviceErr.CaptureStackTrace()
return serviceErr
return &serviceErr
}
}

Expand Down
Loading

0 comments on commit cb6a4b0

Please sign in to comment.