Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Jun 15, 2024
1 parent 43fa647 commit 12d66a4
Show file tree
Hide file tree
Showing 43 changed files with 665 additions and 652 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dist.zip

# Ignore generated files
*.pb.go
**/swaggo-gen/docs.go
**/swaggo_gen/docs.go

bin/
__debug_bin**
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ run: build
.PHONY: clean
clean:
rm -rf bin
rm -rf cmd/web_server/swaggo-gen
rm -rf cmd/web_server/swaggo_gen

.PHONY: gen-swagger
gen-swagger: install-swaggo
swag fmt -d cmd/web_server
swag init -d cmd/web_server,models -ot go -o cmd/web_server/swaggo-gen
swag init -d cmd/web_server,models -ot go -o cmd/web_server/swaggo_gen

# Deprecated
# But still needed to pass the build
Expand Down Expand Up @@ -79,7 +79,7 @@ check: gen-proto install-cilint
golangci-lint run

.PHONY: test
test: gen-swagger setup-dependencies
test: build gen-swagger setup-dependencies
go test -cover -v -count=1 ./...

# Dependent targets
Expand Down
8 changes: 4 additions & 4 deletions cmd/init_db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
judge_model "github.com/oj-lab/oj-lab-platform/models/judge"
problem_model "github.com/oj-lab/oj-lab-platform/models/problem"
user_model "github.com/oj-lab/oj-lab-platform/models/user"
gormAgent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
"github.com/oj-lab/oj-lab-platform/modules/log"
)

func main() {
db := gormAgent.GetDefaultDB()
db := gorm_agent.GetDefaultDB()
err := db.AutoMigrate(
&user_model.User{},
&problem_model.Problem{},
&judge_model.JudgeTaskSubmission{},
&judge_model.Judger{})
&judge_model.Judge{},
)
if err != nil {
panic("failed to migrate database")
}
Expand Down
39 changes: 20 additions & 19 deletions cmd/problem_loader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@ import (
"context"
"fmt"
"io/fs"
"log"
"os"
"path"
"path/filepath"
"strings"

"github.com/minio/minio-go/v7"
problem_model "github.com/oj-lab/oj-lab-platform/models/problem"
gormAgent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
minioAgent "github.com/oj-lab/oj-lab-platform/modules/agent/minio"
gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
minio_agent "github.com/oj-lab/oj-lab-platform/modules/agent/minio"
"github.com/oj-lab/oj-lab-platform/modules/config"
"github.com/oj-lab/oj-lab-platform/modules/log"
yaml "gopkg.in/yaml.v2"
)

var ctx = context.Background()

func main() {
db := gormAgent.GetDefaultDB()
minioClient := minioAgent.GetMinioClient()

log.Printf("%#v\n", minioClient) // minioClient is now set up
db := gorm_agent.GetDefaultDB()
minioClient := minio_agent.GetMinioClient()

// Read package files
// Search Problem under packagePath
Expand All @@ -38,39 +36,42 @@ func main() {
slug string
)
err := filepath.Walk(packagePath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
log.AppLogger().WithError(err).Error("Walk package path failed")
return err
}
if info == nil {
return fmt.Errorf("file info is nil")
}
if info.IsDir() {
return nil
}
relativePath := strings.Replace(path, packagePath, "", 1)
println("relativePath: ", relativePath)
log.AppLogger().WithField("relativePath", relativePath).Debug("Read file from package")
if filepath.Base(relativePath) == "problem.yaml" {
resultMap := make(map[string]interface{})
yamlFile, err := os.ReadFile(path)
if err != nil {
log.Println(err)
log.AppLogger().WithError(err).Error("Read problem.yaml failed")
}
err = yaml.Unmarshal(yamlFile, &resultMap)
if err != nil {
log.Printf("Unmarshal: %v\n", err)
log.AppLogger().WithError(err).Error("Unmarshal problem.yaml failed")
}
title = resultMap["name"].(string)
if title == "" {
log.Fatal("name key not exist in problem.yaml")
log.AppLogger().Error("Problem title is empty")
}
slug = strings.Split(relativePath, "/")[1]
log.Println("title: ", title)
log.Println("slug: ", slug)
log.AppLogger().WithField("title", title).WithField("slug", slug).Debug("Read problem.yaml")
}
if filepath.Base(relativePath) == "problem.md" {
content, err := os.ReadFile(path)
if err != nil {
log.Println(err)
log.AppLogger().WithError(err).Error("Read problem.md failed")
}
description := string(content)
println("description: ", description)
log.AppLogger().WithField("description", description).Debug("Read problem.md")
err = problem_model.CreateProblem(db, problem_model.Problem{
Slug: slug,
Title: title,
Expand All @@ -84,18 +85,18 @@ func main() {
}
}

_, minioErr := minioClient.FPutObject(ctx, minioAgent.GetBucketName(),
_, err = minioClient.FPutObject(ctx, minio_agent.GetBucketName(),
relativePath,
path,
minio.PutObjectOptions{})
if minioErr != nil {
log.Fatalln(minioErr)
if err != nil {
log.AppLogger().WithError(err).Error("Put object to minio failed")
}
return err
})
if err != nil {
panic(err)
}

log.Println("Read Problem Success!")
log.AppLogger().Info("Problem loaded")
}
92 changes: 56 additions & 36 deletions cmd/web_server/handler/judge.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,85 @@
package handler

import (
"fmt"
"strconv"

"github.com/gin-gonic/gin"
"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"
judge_service "github.com/oj-lab/oj-lab-platform/services/judge"
"github.com/redis/go-redis/v9"
)

func SetupJudgeRoute(baseRoute *gin.RouterGroup) {
func SetupJudgeRouter(baseRoute *gin.RouterGroup) {
g := baseRoute.Group("/judge")
{
g.POST("/task/pick", postPickJudgeTask)
g.POST("/task/report", postReportJudgeTaskResult)
g.GET("", getJudgeList)
g.GET("/:uid", getJudge)
}
}

type PickJudgeTaskBody struct {
Consumer string `json:"consumer"`
}

func postPickJudgeTask(ginCtx *gin.Context) {
body := PickJudgeTaskBody{}
if err := ginCtx.ShouldBindJSON(&body); err != nil {
_ = ginCtx.Error(err)
return
}

task, err := judge_service.PickJudgeTask(ginCtx, body.Consumer)
if err == redis.Nil {
ginCtx.Status(204)
return
}
func getJudge(ginCtx *gin.Context) {
uid := ginCtx.Param("uid")

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

ginCtx.JSON(200, gin.H{
"task": task,
})
ginCtx.JSON(200, judge)
}

type ReportJudgeTaskResultBody struct {
Consumer string `json:"consumer"`
StreamID string `json:"stream_id"`
VerdictJson string `json:"verdict_json"`
type getJudgeListResponse struct {
Total int64 `json:"total"`
List []*judge_model.Judge `json:"list"`
}

func postReportJudgeTaskResult(ginCtx *gin.Context) {
body := ReportJudgeTaskResultBody{}
if err := ginCtx.ShouldBindJSON(&body); err != nil {
_ = ginCtx.Error(err)
// Get Judge List
//
// @Summary Get Judge list
// @Description Get Judge list
// @Tags judge
// @Accept json
// @Param limit query int false "limit"
// @Param offset query int false "offset"
// @Router /judge [get] getJudgeListResponse
func getJudgeList(ginCtx *gin.Context) {
limitQuery, _ := ginCtx.GetQuery("limit")
offsetQuery, _ := ginCtx.GetQuery("offset")
if limitQuery == "" {
limitQuery = "10"
}
if offsetQuery == "" {
offsetQuery = "0"
}

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

options := judge_model.GetJudgeOptions{
Limit: &limit,
Offset: &offset,
OrderByColumns: []models.OrderByColumnOption{{Column: "create_at", Desc: true}},
}

if err := judge_service.ReportJudgeTaskResult(ginCtx, body.Consumer, body.StreamID, body.VerdictJson); err != nil {
_ = ginCtx.Error(err)
judges, total, err := judge_service.GetJudgeList(ginCtx, options)
if err != nil {
modules.NewInternalError(fmt.Sprintf("failed to get judge list: %v", err)).AppendToGin(ginCtx)
return
}

ginCtx.JSON(200, gin.H{
"message": "success",
ginCtx.JSON(200, getJudgeListResponse{
Total: total,
List: judges,
})
}
65 changes: 65 additions & 0 deletions cmd/web_server/handler/judge_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package handler

import (
"github.com/gin-gonic/gin"
judge_service "github.com/oj-lab/oj-lab-platform/services/judge"
"github.com/redis/go-redis/v9"
)

func SetupJudgeRoute(baseRoute *gin.RouterGroup) {
g := baseRoute.Group("/judge")
{
g.POST("/task/pick", postPickJudgeTask)
g.POST("/task/report", postReportJudgeTaskResult)
}
}

type PickJudgeTaskBody struct {
Consumer string `json:"consumer"`
}

func postPickJudgeTask(ginCtx *gin.Context) {
body := PickJudgeTaskBody{}
if err := ginCtx.ShouldBindJSON(&body); err != nil {
_ = ginCtx.Error(err)
return
}

task, err := judge_service.PickJudgeTask(ginCtx, body.Consumer)
if err == redis.Nil {
ginCtx.Status(204)
return
}

if err != nil {
_ = ginCtx.Error(err)
return
}

ginCtx.JSON(200, gin.H{
"task": task,
})
}

type ReportJudgeTaskResultBody struct {
Consumer string `json:"consumer"`
StreamID string `json:"stream_id"`
VerdictJson string `json:"verdict_json"`
}

func postReportJudgeTaskResult(ginCtx *gin.Context) {
body := ReportJudgeTaskResultBody{}
if err := ginCtx.ShouldBindJSON(&body); err != nil {
_ = ginCtx.Error(err)
return
}

if err := judge_service.ReportJudgeTask(ginCtx, body.Consumer, body.StreamID, body.VerdictJson); err != nil {
_ = ginCtx.Error(err)
return
}

ginCtx.JSON(200, gin.H{
"message": "success",
})
}
Loading

0 comments on commit 12d66a4

Please sign in to comment.