From a4c75da003f78d3f141c295069ec39b5ce287264 Mon Sep 17 00:00:00 2001 From: slhmy Date: Sun, 19 May 2024 17:04:54 +0800 Subject: [PATCH] Refactor project structure --- .dockerignore | 3 +- .gitignore | 2 +- .vscode/launch.json | 2 +- Dockerfile | 13 +-- Makefile | 20 ++--- README.md | 4 +- cmd/init_db/main.go | 47 ++++++++++ .../problem_loader}/main.go | 18 ++-- .../rpc_server/impls/greeter.go | 2 +- .../rpc_server/impls/streamer.go | 2 +- {src/application => cmd}/rpc_server/main.go | 8 +- {src/application => cmd}/schedule/main.go | 0 .../web_server}/handler/event.go | 4 +- .../web_server}/handler/frontend.go | 0 .../web_server}/handler/judge.go | 25 +----- .../web_server}/handler/problem.go | 36 ++++---- .../web_server}/handler/submission.go | 33 +++---- cmd/web_server/handler/swaggo.go | 33 +++++++ .../server => cmd/web_server}/handler/user.go | 29 +++--- .../server => cmd/web_server}/main.go | 23 ++--- go.mod | 35 ++++---- go.sum | 88 +++++++------------ {src/service/model => models/judge}/judge.go | 6 +- .../model => models/judge}/submission.go | 29 +++--- .../judge/submission_db.go | 28 +++--- {src/service/model => models}/meta.go | 2 +- .../model => models/problem}/problem.go | 12 +-- .../problem/problem_db.go | 32 +++---- {src/service/model => models/user}/user.go | 10 ++- .../mapper/user.go => models/user/user_db.go | 41 ++++----- {src/core => modules}/agent/gorm/database.go | 4 +- {src/core => modules}/agent/judger/client.go | 0 {src/core => modules}/agent/judger/judge.go | 0 {src/core => modules}/agent/judger/state.go | 0 {src/core => modules}/agent/minio/client.go | 14 +-- {src/core => modules}/agent/minio/local.go | 0 {src/core => modules}/agent/redis/client.go | 4 +- .../agent/redis/login_session.go | 0 {src/core => modules}/auth/jwt.go | 6 +- {src/core => modules}/auth/login_session.go | 2 +- {src/core => modules/config}/config.go | 3 +- {src/core => modules}/error.go | 2 +- {src/core => modules/log}/log.go | 9 +- modules/middleware/error.go | 33 +++++++ .../middleware/login_session.go | 8 +- .../proto => proto}/oj_lab_greeting.proto | 2 +- .../proto => proto}/oj_lab_stream.proto | 2 +- scripts/update-frontend-dist.sh | 2 +- .../judge}/judge_stream.go | 24 ++--- services/judge/submission.go | 54 ++++++++++++ {src/service => services/judge}/task.go | 74 ++++++++-------- {src/service => services/problem}/problem.go | 29 +++--- services/problem/problem_info.go | 26 ++++++ .../problem}/problem_package.go | 8 +- services/user/user.go | 56 ++++++++++++ src/application/init_db/main.go | 42 --------- src/application/server/handler/swaggo.go | 33 ------- src/core/middleware/error.go | 32 ------- src/service/business/problem_info.go | 27 ------ src/service/judge.go | 13 --- src/service/mapper/judger.go | 16 ---- src/service/submission.go | 59 ------------- src/service/user.go | 57 ------------ tests/core/jwt_test.go | 2 +- tests/core/redis_test.go | 2 +- tests/mapper/judger_test.go | 16 ---- tests/{mapper => models}/problem_test.go | 25 +++--- tests/{mapper => models}/user_test.go | 19 ++-- 68 files changed, 600 insertions(+), 692 deletions(-) create mode 100644 cmd/init_db/main.go rename {src/application/problem_package_loader => cmd/problem_loader}/main.go (82%) rename {src/application => cmd}/rpc_server/impls/greeter.go (86%) rename {src/application => cmd}/rpc_server/impls/streamer.go (93%) rename {src/application => cmd}/rpc_server/main.go (75%) rename {src/application => cmd}/schedule/main.go (100%) rename {src/application/server => cmd/web_server}/handler/event.go (90%) rename {src/application/server => cmd/web_server}/handler/frontend.go (100%) rename {src/application/server => cmd/web_server}/handler/judge.go (64%) rename {src/application/server => cmd/web_server}/handler/problem.go (76%) rename {src/application/server => cmd/web_server}/handler/submission.go (62%) create mode 100644 cmd/web_server/handler/swaggo.go rename {src/application/server => cmd/web_server}/handler/user.go (62%) rename {src/application/server => cmd/web_server}/main.go (68%) rename {src/service/model => models/judge}/judge.go (98%) rename {src/service/model => models/judge}/submission.go (56%) rename src/service/mapper/submission.go => models/judge/submission_db.go (75%) rename {src/service/model => models}/meta.go (97%) rename {src/service/model => models/problem}/problem.go (78%) rename src/service/mapper/problem.go => models/problem/problem_db.go (70%) rename {src/service/model => models/user}/user.go (74%) rename src/service/mapper/user.go => models/user/user_db.go (65%) rename {src/core => modules}/agent/gorm/database.go (84%) rename {src/core => modules}/agent/judger/client.go (100%) rename {src/core => modules}/agent/judger/judge.go (100%) rename {src/core => modules}/agent/judger/state.go (100%) rename {src/core => modules}/agent/minio/client.go (78%) rename {src/core => modules}/agent/minio/local.go (100%) rename {src/core => modules}/agent/redis/client.go (76%) rename {src/core => modules}/agent/redis/login_session.go (100%) rename {src/core => modules}/auth/jwt.go (91%) rename {src/core => modules}/auth/login_session.go (92%) rename {src/core => modules/config}/config.go (98%) rename {src/core => modules}/error.go (98%) rename {src/core => modules/log}/log.go (78%) create mode 100644 modules/middleware/error.go rename {src/core => modules}/middleware/login_session.go (77%) rename {src/service/proto => proto}/oj_lab_greeting.proto (79%) rename {src/service/proto => proto}/oj_lab_stream.proto (83%) rename {src/service/business => services/judge}/judge_stream.go (67%) create mode 100644 services/judge/submission.go rename {src/service => services/judge}/task.go (50%) rename {src/service => services/problem}/problem.go (58%) create mode 100644 services/problem/problem_info.go rename {src/service/business => services/problem}/problem_package.go (80%) create mode 100644 services/user/user.go delete mode 100644 src/application/init_db/main.go delete mode 100644 src/application/server/handler/swaggo.go delete mode 100644 src/core/middleware/error.go delete mode 100644 src/service/business/problem_info.go delete mode 100644 src/service/judge.go delete mode 100644 src/service/mapper/judger.go delete mode 100644 src/service/submission.go delete mode 100644 src/service/user.go delete mode 100644 tests/mapper/judger_test.go rename tests/{mapper => models}/problem_test.go (56%) rename tests/{mapper => models}/user_test.go (58%) diff --git a/.dockerignore b/.dockerignore index ab3d0b2..9f8926e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,5 @@ +.devcontainer/ .github/ .vscode/ +bin/ tests/ -artifacts/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 009ab95..4590a00 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,4 @@ dist.zip **/swaggo-gen/docs.go bin/ -__debug_bin** \ No newline at end of file +__debug_bin** diff --git a/.vscode/launch.json b/.vscode/launch.json index efe6b3d..18d9c05 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "mode": "auto", "cwd": "${workspaceFolder}", - "program": "src/application/server/main.go", + "program": "cmd/web_server/main.go", } ] } \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 1618200..872f0df 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,6 @@ FROM golang:latest as build -COPY go.mod /oj-lab-platform-build/go.mod -COPY go.sum /oj-lab-platform-build/go.sum -COPY scripts/ /oj-lab-platform-build/scripts/ -COPY Makefile /oj-lab-platform-build/Makefile - -COPY src/application/ /oj-lab-platform-build/src/application/ -COPY src/core/ /oj-lab-platform-build/src/core/ -COPY src/service/ /oj-lab-platform-build/src/service/ +COPY . /oj-lab-platform-build WORKDIR /oj-lab-platform-build RUN apt update && apt install -y make zip curl @@ -19,7 +12,7 @@ FROM ubuntu:latest WORKDIR /workdir -COPY --from=build /oj-lab-platform-build/bin/service /usr/local/bin/oj-lab-service +COPY --from=build /oj-lab-platform-build/bin/web_server /usr/local/bin/web_server COPY --from=build /oj-lab-platform-build/frontend_dist /workdir/frontend_dist COPY workdirs/docker/config.toml /workdir/config.toml @@ -27,4 +20,4 @@ COPY workdirs/docker/config.toml /workdir/config.toml ENV OJ_LAB_SERVICE_ENV=production ENV OJ_LAB_WORKDIR=/workdir EXPOSE 8080 -CMD [ "oj-lab-service" ] \ No newline at end of file +CMD [ "web_server" ] \ No newline at end of file diff --git a/Makefile b/Makefile index f53e795..532f3bd 100644 --- a/Makefile +++ b/Makefile @@ -24,24 +24,24 @@ help: build: gen-swagger gen-proto @echo "Building on $(OS)" go mod tidy - go build -o bin/init_db src/application/init_db/main.go - go build -o bin/service src/application/server/main.go - go build -o bin/schedule src/application/schedule/main.go - go build -o bin/problem_package_loader src/application/problem_package_loader/main.go + go build -o bin/init_db cmd/init_db/main.go + go build -o bin/web_server cmd/web_server/main.go + go build -o bin/schedule cmd/schedule/main.go + go build -o bin/problem_loader cmd/problem_loader/main.go .PHONY: run run: build - ./bin/service + ./bin/web_server .PHONY: clean clean: rm -rf bin - rm -rf src/application/server/swaggo-gen + rm -rf cmd/web_server/swaggo-gen .PHONY: gen-swagger gen-swagger: install-swaggo - swag fmt -d src/application/server - swag init -d src/application/server,src/service/model -ot go -o src/application/server/swaggo-gen + swag fmt -d cmd/web_server + swag init -d cmd/web_server,models -ot go -o cmd/web_server/swaggo-gen # Deprecated # But still needed to pass the build @@ -49,7 +49,7 @@ gen-swagger: install-swaggo gen-proto: install-proto protoc --go_out=. --go_opt=paths=source_relative \ --go-grpc_out=. --go-grpc_opt=paths=source_relative \ - src/service/proto/*.proto + proto/*.proto # Targets for development @@ -67,7 +67,7 @@ setup-dependencies: unset-dependencies build @echo "Wait 10 seconds for db setup" sleep 10s ./bin/init_db - ./bin/problem_package_loader + ./bin/problem_loader docker compose -f $(JUDGER_DOCKER_COMPOSE_FILE) -p oj-lab-judger up -d .PHONY: get-front diff --git a/README.md b/README.md index 6ee300d..84e83d0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # OJ Lab Platform -![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/OJ-lab/oj-lab-platform/build-and-test.yaml?logo=github&label=Tests) +![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/oj-lab/oj-lab-platform/build-and-test.yaml?logo=github&label=Tests) ![Codespace Supported](https://img.shields.io/badge/Codespace_Supported-000000?style=flat&logo=github) ## Development @@ -14,7 +14,7 @@ OJ Lab Platform depends on several foundational services, including: - PostgreSQL (or other SQL database in the future) - Redis - MinIO -- [Judger](https://github.com/OJ-lab/judger) +- [Judger](https://github.com/oj-lab/judger) This project provides a Makefile to help you quickly set up dependencies & other optional choices. Run `make setup-dependencies` to start these services and load the initial data. diff --git a/cmd/init_db/main.go b/cmd/init_db/main.go new file mode 100644 index 0000000..5752003 --- /dev/null +++ b/cmd/init_db/main.go @@ -0,0 +1,47 @@ +package main + +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" + "github.com/oj-lab/oj-lab-platform/modules/log" +) + +func main() { + db := gormAgent.GetDefaultDB() + err := db.AutoMigrate( + &user_model.User{}, + &problem_model.Problem{}, + &judge_model.JudgeTaskSubmission{}, + &judge_model.Judger{}) + if err != nil { + panic("failed to migrate database") + } + + err = user_model.CreateUser(db, user_model.User{ + Name: "admin", + Account: "admin", + Password: func() *string { s := "admin"; return &s }(), + Roles: []*user_model.Role{ + {Name: "admin"}, + }, + }) + if err != nil { + panic("failed to create admin user") + } + + err = user_model.CreateUser(db, user_model.User{ + Name: "anonymous", + Account: "anonymous", + Password: func() *string { s := "anonymous"; return &s }(), + Roles: []*user_model.Role{ + {Name: "anonymous"}, + }, + }) + if err != nil { + panic("failed to create anonymous user") + } + + log.AppLogger().Info("migrate tables ans users success") +} diff --git a/src/application/problem_package_loader/main.go b/cmd/problem_loader/main.go similarity index 82% rename from src/application/problem_package_loader/main.go rename to cmd/problem_loader/main.go index fd894d4..abec531 100644 --- a/src/application/problem_package_loader/main.go +++ b/cmd/problem_loader/main.go @@ -10,14 +10,12 @@ import ( "path/filepath" "strings" - "github.com/OJ-lab/oj-lab-services/src/core" - gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" - minioAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/minio" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" - yaml "gopkg.in/yaml.v2" - "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" + "github.com/oj-lab/oj-lab-platform/modules/config" + yaml "gopkg.in/yaml.v2" ) var ctx = context.Background() @@ -35,7 +33,7 @@ func main() { // parse problem.md as description. // 2. insert object into minio storage. var ( - packagePath string = path.Join(core.Workdir, "problem_packages") + packagePath string = path.Join(config.Workdir, "problem_packages") title string slug string ) @@ -73,11 +71,11 @@ func main() { } description := string(content) println("description: ", description) - err = mapper.CreateProblem(db, model.Problem{ + err = problem_model.CreateProblem(db, problem_model.Problem{ Slug: slug, Title: title, Description: &description, - Tags: []*model.AlgorithmTag{ + Tags: []*problem_model.AlgorithmTag{ {Name: "to-be-add"}, }, }) diff --git a/src/application/rpc_server/impls/greeter.go b/cmd/rpc_server/impls/greeter.go similarity index 86% rename from src/application/rpc_server/impls/greeter.go rename to cmd/rpc_server/impls/greeter.go index ee94fbb..c964411 100644 --- a/src/application/rpc_server/impls/greeter.go +++ b/cmd/rpc_server/impls/greeter.go @@ -4,7 +4,7 @@ import ( "context" "log" - "github.com/OJ-lab/oj-lab-services/src/service/proto" + "github.com/oj-lab/oj-lab-platform/proto" ) type GreeterServer struct { diff --git a/src/application/rpc_server/impls/streamer.go b/cmd/rpc_server/impls/streamer.go similarity index 93% rename from src/application/rpc_server/impls/streamer.go rename to cmd/rpc_server/impls/streamer.go index 5ec863d..f892185 100644 --- a/src/application/rpc_server/impls/streamer.go +++ b/cmd/rpc_server/impls/streamer.go @@ -4,7 +4,7 @@ import ( "log" "time" - "github.com/OJ-lab/oj-lab-services/src/service/proto" + "github.com/oj-lab/oj-lab-platform/proto" ) type StreamerServer struct { diff --git a/src/application/rpc_server/main.go b/cmd/rpc_server/main.go similarity index 75% rename from src/application/rpc_server/main.go rename to cmd/rpc_server/main.go index 7bd2216..d10a864 100644 --- a/src/application/rpc_server/main.go +++ b/cmd/rpc_server/main.go @@ -8,9 +8,9 @@ import ( "log" "net" - "github.com/OJ-lab/oj-lab-services/src/application/rpc_server/impls" - "github.com/OJ-lab/oj-lab-services/src/core" - "github.com/OJ-lab/oj-lab-services/src/service/proto" + "github.com/oj-lab/oj-lab-platform/cmd/rpc_server/impls" + "github.com/oj-lab/oj-lab-platform/modules/config" + "github.com/oj-lab/oj-lab-platform/proto" "google.golang.org/grpc" "google.golang.org/grpc/reflection" ) @@ -20,7 +20,7 @@ const ( ) var ( - port = core.AppConfig.GetInt(portProp) + port = config.AppConfig.GetInt(portProp) ) func main() { diff --git a/src/application/schedule/main.go b/cmd/schedule/main.go similarity index 100% rename from src/application/schedule/main.go rename to cmd/schedule/main.go diff --git a/src/application/server/handler/event.go b/cmd/web_server/handler/event.go similarity index 90% rename from src/application/server/handler/event.go rename to cmd/web_server/handler/event.go index b156b92..5e9a0cf 100644 --- a/src/application/server/handler/event.go +++ b/cmd/web_server/handler/event.go @@ -5,8 +5,8 @@ import ( "io" "time" - "github.com/OJ-lab/oj-lab-services/src/core" "github.com/gin-gonic/gin" + "github.com/oj-lab/oj-lab-platform/modules/log" ) func SetupEventRouter(baseRoute *gin.RouterGroup) { @@ -34,7 +34,7 @@ func Stream(ginCtx *gin.Context) { ginCtx.Stream(func(w io.Writer) bool { // With event type message := fmt.Sprintf("event: %s\ndata: %s\n\n", "eventType", time.Now().String()) - core.AppLogger().Infof("Send message:\n%s", message) + log.AppLogger().Infof("Send message:\n%s", message) fmt.Fprint(w, message) time.Sleep(1 * time.Second) counter++ diff --git a/src/application/server/handler/frontend.go b/cmd/web_server/handler/frontend.go similarity index 100% rename from src/application/server/handler/frontend.go rename to cmd/web_server/handler/frontend.go diff --git a/src/application/server/handler/judge.go b/cmd/web_server/handler/judge.go similarity index 64% rename from src/application/server/handler/judge.go rename to cmd/web_server/handler/judge.go index be46765..99c88ee 100644 --- a/src/application/server/handler/judge.go +++ b/cmd/web_server/handler/judge.go @@ -1,38 +1,19 @@ package handler import ( - "github.com/OJ-lab/oj-lab-services/src/service" - "github.com/OJ-lab/oj-lab-services/src/service/model" "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("/add-judger", postJudger) g.POST("/task/pick", postPickJudgeTask) g.POST("/task/report", postReportJudgeTaskResult) } } -func postJudger(ginCtx *gin.Context) { - judger := model.Judger{} - if err := ginCtx.ShouldBindJSON(&judger); err != nil { - _ = ginCtx.Error(err) - return - } - - if err := service.AddJudger(ginCtx, judger); err != nil { - _ = ginCtx.Error(err) - return - } - - ginCtx.JSON(200, gin.H{ - "message": "success", - }) -} - type PickJudgeTaskBody struct { Consumer string `json:"consumer"` } @@ -44,7 +25,7 @@ func postPickJudgeTask(ginCtx *gin.Context) { return } - task, err := service.PickJudgeTask(ginCtx, body.Consumer) + task, err := judge_service.PickJudgeTask(ginCtx, body.Consumer) if err == redis.Nil { ginCtx.Status(204) return @@ -73,7 +54,7 @@ func postReportJudgeTaskResult(ginCtx *gin.Context) { return } - if err := service.ReportJudgeTaskResult(ginCtx, body.Consumer, body.StreamID, body.VerdictJson); err != nil { + if err := judge_service.ReportJudgeTaskResult(ginCtx, body.Consumer, body.StreamID, body.VerdictJson); err != nil { _ = ginCtx.Error(err) return } diff --git a/src/application/server/handler/problem.go b/cmd/web_server/handler/problem.go similarity index 76% rename from src/application/server/handler/problem.go rename to cmd/web_server/handler/problem.go index 4903743..3a7440c 100644 --- a/src/application/server/handler/problem.go +++ b/cmd/web_server/handler/problem.go @@ -3,10 +3,12 @@ package handler import ( "net/http" - "github.com/OJ-lab/oj-lab-services/src/service" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" "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" + judge_service "github.com/oj-lab/oj-lab-platform/services/judge" + problem_service "github.com/oj-lab/oj-lab-platform/services/problem" ) func SetupProblemRoute(baseRoute *gin.RouterGroup) { @@ -36,7 +38,7 @@ func SetupProblemRoute(baseRoute *gin.RouterGroup) { func getProblem(ginCtx *gin.Context) { slug := ginCtx.Param("slug") - problemInfo, err := service.GetProblem(ginCtx, slug) + problemInfo, err := problem_service.GetProblem(ginCtx, slug) if err != nil { _ = ginCtx.Error(err) return @@ -46,7 +48,7 @@ func getProblem(ginCtx *gin.Context) { "slug": problemInfo.Slug, "title": problemInfo.Title, "description": problemInfo.Description, - "tags": mapper.GetTagsList(*problemInfo), + "tags": problem_model.GetTagsList(*problemInfo), }) } @@ -58,13 +60,13 @@ func getProblem(ginCtx *gin.Context) { // @Tags problem // @Accept json func putProblem(ginCtx *gin.Context) { - problem := model.Problem{} + problem := problem_model.Problem{} if err := ginCtx.ShouldBindJSON(&problem); err != nil { _ = ginCtx.Error(err) return } - err := service.PutProblem(ginCtx, problem) + err := problem_service.PutProblem(ginCtx, problem) if err != nil { _ = ginCtx.Error(err) return @@ -81,7 +83,7 @@ func putProblem(ginCtx *gin.Context) { func deleteProblem(ginCtx *gin.Context) { slug := ginCtx.Param("slug") - err := service.DeleteProblem(ginCtx, slug) + err := problem_service.DeleteProblem(ginCtx, slug) if err != nil { _ = ginCtx.Error(err) return @@ -97,7 +99,7 @@ func deleteProblem(ginCtx *gin.Context) { // @Accept json // @Success 200 func getProblemInfoList(ginCtx *gin.Context) { - problemInfoList, total, err := service.GetProblemInfoList(ginCtx) + problemInfoList, total, err := problem_service.GetProblemInfoList(ginCtx) if err != nil { _ = ginCtx.Error(err) return @@ -130,7 +132,7 @@ func putProblemPackage(ginCtx *gin.Context) { return } - err = service.PutProblemPackage(ginCtx, slug, zipFile) + err = problem_service.PutProblemPackage(ginCtx, slug, zipFile) if err != nil { _ = ginCtx.Error(err) return @@ -151,7 +153,7 @@ func putProblemPackage(ginCtx *gin.Context) { func checkProblemSlug(ginCtx *gin.Context) { slug := ginCtx.Param("slug") - valid, err := service.CheckProblemSlug(ginCtx, slug) + valid, err := problem_service.CheckProblemSlug(ginCtx, slug) if err != nil { _ = ginCtx.Error(err) return @@ -168,8 +170,8 @@ func checkProblemSlug(ginCtx *gin.Context) { // @Property code (string) required "The source code of the submission" minlength(1) // @Property language (SubmissionLanguage) required "The programming language used for the submission" type PostSubmissionBody struct { - Code string `json:"code" binding:"required"` - Language model.SubmissionLanguage `json:"language" binding:"required"` + Code string `json:"code" binding:"required"` + Language judge_model.SubmissionLanguage `json:"language" binding:"required"` } // postSubmission @@ -189,10 +191,10 @@ func postSubmission(ginCtx *gin.Context) { return } - submission := model.NewSubmission("", slug, body.Code, body.Language) - result, svcErr := service.CreateJudgeTaskSubmission(ginCtx, submission) - if svcErr != nil { - svcErr.AppendToGin(ginCtx) + submission := judge_model.NewSubmission("", slug, body.Code, body.Language) + result, err := judge_service.CreateJudgeTaskSubmission(ginCtx, submission) + if err != nil { + modules.NewInternalError(err.Error()).AppendToGin(ginCtx) return } diff --git a/src/application/server/handler/submission.go b/cmd/web_server/handler/submission.go similarity index 62% rename from src/application/server/handler/submission.go rename to cmd/web_server/handler/submission.go index f1dc506..d8f09e5 100644 --- a/src/application/server/handler/submission.go +++ b/cmd/web_server/handler/submission.go @@ -1,13 +1,14 @@ package handler import ( + "fmt" "strconv" - "github.com/OJ-lab/oj-lab-services/src/core" - "github.com/OJ-lab/oj-lab-services/src/service" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" "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" ) func SetupSubmissionRouter(baseRoute *gin.RouterGroup) { @@ -21,9 +22,9 @@ func SetupSubmissionRouter(baseRoute *gin.RouterGroup) { func getSubmission(ginCtx *gin.Context) { uid := ginCtx.Param("uid") - submission, svcErr := service.GetJudgeTaskSubmission(ginCtx, uid) - if svcErr != nil { - svcErr.AppendToGin(ginCtx) + submission, err := judge_service.GetJudgeTaskSubmission(ginCtx, uid) + if err != nil { + modules.NewInternalError(fmt.Sprintf("failed to get submission: %v", err)).AppendToGin(ginCtx) return } @@ -43,8 +44,8 @@ func getSubmission(ginCtx *gin.Context) { } type getSubmissionListResponse struct { - Total int64 `json:"total"` - List []*model.JudgeTaskSubmission `json:"list"` + Total int64 `json:"total"` + List []*judge_model.JudgeTaskSubmission `json:"list"` } // Get Submission List @@ -68,24 +69,24 @@ func getSubmissionList(ginCtx *gin.Context) { limit, err := strconv.Atoi(limitQuery) if err != nil { - core.NewInvalidParamError("limit", "invalid limit").AppendToGin(ginCtx) + modules.NewInvalidParamError("limit", "invalid limit").AppendToGin(ginCtx) return } offset, err := strconv.Atoi(offsetQuery) if err != nil { - core.NewInvalidParamError("offset", "invalid offset").AppendToGin(ginCtx) + modules.NewInvalidParamError("offset", "invalid offset").AppendToGin(ginCtx) return } - options := mapper.GetSubmissionOptions{ + options := judge_model.GetSubmissionOptions{ Limit: &limit, Offset: &offset, - OrderByColumns: []model.OrderByColumnOption{{Column: "create_at", Desc: true}}, + OrderByColumns: []models.OrderByColumnOption{{Column: "create_at", Desc: true}}, } - submissions, total, svcErr := service.GetJudgeTaskSubmissionList(ginCtx, options) - if svcErr != nil { - svcErr.AppendToGin(ginCtx) + submissions, total, err := judge_service.GetJudgeTaskSubmissionList(ginCtx, options) + if err != nil { + modules.NewInternalError(fmt.Sprintf("failed to get submission list: %v", err)).AppendToGin(ginCtx) return } diff --git a/cmd/web_server/handler/swaggo.go b/cmd/web_server/handler/swaggo.go new file mode 100644 index 0000000..cca708d --- /dev/null +++ b/cmd/web_server/handler/swaggo.go @@ -0,0 +1,33 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + swaggo_gen "github.com/oj-lab/oj-lab-platform/cmd/web_server/swaggo-gen" + "github.com/spf13/viper" + swagger_files "github.com/swaggo/files" + gin_swagger "github.com/swaggo/gin-swagger" +) + +const ( + servicePortProp = "service.port" +) + +var ( + swaggerHost string +) + +func SetupSwaggoRouter(r *gin.RouterGroup) { + r.GET("/swagger/*any", gin_swagger.WrapHandler(swagger_files.Handler)) +} + +func init() { + sevicePort := viper.GetString(servicePortProp) + swaggerHost = "localhost" + sevicePort + println("Swagger host is set to: " + swaggerHost) + // programmatically set swagger info + swaggo_gen.SwaggerInfo.Title = "OJ Lab Services API" + swaggo_gen.SwaggerInfo.Version = "1.0" + swaggo_gen.SwaggerInfo.Host = swaggerHost + swaggo_gen.SwaggerInfo.BasePath = "/api/v1" + swaggo_gen.SwaggerInfo.Schemes = []string{"http"} +} diff --git a/src/application/server/handler/user.go b/cmd/web_server/handler/user.go similarity index 62% rename from src/application/server/handler/user.go rename to cmd/web_server/handler/user.go index d89ed8b..625990f 100644 --- a/src/application/server/handler/user.go +++ b/cmd/web_server/handler/user.go @@ -1,12 +1,13 @@ package handler import ( + "fmt" "net/http" - "github.com/OJ-lab/oj-lab-services/src/core" - "github.com/OJ-lab/oj-lab-services/src/core/middleware" - "github.com/OJ-lab/oj-lab-services/src/service" "github.com/gin-gonic/gin" + "github.com/oj-lab/oj-lab-platform/modules" + "github.com/oj-lab/oj-lab-platform/modules/middleware" + user_service "github.com/oj-lab/oj-lab-platform/services/user" ) func SetupUserRouter(baseRoute *gin.RouterGroup) { @@ -39,13 +40,13 @@ func login(ginCtx *gin.Context) { body := &loginBody{} err := ginCtx.BindJSON(body) if err != nil { - core.NewInvalidParamError("body", "invalid body").AppendToGin(ginCtx) + modules.NewInvalidParamError("body", "invalid body").AppendToGin(ginCtx) return } - lsId, svcErr := service.StartLoginSession(ginCtx, body.Account, body.Password) - if svcErr != nil { - svcErr.AppendToGin(ginCtx) + lsId, err := user_service.StartLoginSession(ginCtx, body.Account, body.Password) + if err != nil { + modules.NewInternalError(fmt.Sprintf("failed to login: %v", err)).AppendToGin(ginCtx) return } middleware.SetLoginSessionCookie(ginCtx, *lsId) @@ -64,12 +65,12 @@ func login(ginCtx *gin.Context) { func me(ginCtx *gin.Context) { ls := middleware.GetLoginSession(ginCtx) if ls == nil { - core.NewUnauthorizedError("not logined").AppendToGin(ginCtx) + modules.NewUnauthorizedError("not logined").AppendToGin(ginCtx) return } - user, svcErr := service.GetUser(ginCtx, ls.Account) - if svcErr != nil { - svcErr.AppendToGin(ginCtx) + user, err := user_service.GetUser(ginCtx, ls.Account) + if err != nil { + modules.NewInternalError(fmt.Sprintf("failed to get user: %v", err)).AppendToGin(ginCtx) return } @@ -79,13 +80,13 @@ func me(ginCtx *gin.Context) { func checkUserExist(ginCtx *gin.Context) { account := ginCtx.Query("account") if account == "" { - core.NewInvalidParamError("account", "account cannot be empty").AppendToGin(ginCtx) + modules.NewInvalidParamError("account", "account cannot be empty").AppendToGin(ginCtx) return } - exist, err := service.CheckUserExist(ginCtx, account) + exist, err := user_service.CheckUserExist(ginCtx, account) if err != nil { - _ = ginCtx.Error(err) + modules.NewInternalError(fmt.Sprintf("failed to check user exist: %v", err)).AppendToGin(ginCtx) return } diff --git a/src/application/server/main.go b/cmd/web_server/main.go similarity index 68% rename from src/application/server/main.go rename to cmd/web_server/main.go index 6fe3abf..f665668 100644 --- a/src/application/server/main.go +++ b/cmd/web_server/main.go @@ -5,11 +5,12 @@ import ( "path/filepath" "runtime" - "github.com/OJ-lab/oj-lab-services/src/application/server/handler" - "github.com/OJ-lab/oj-lab-services/src/core" + "github.com/oj-lab/oj-lab-platform/cmd/web_server/handler" - "github.com/OJ-lab/oj-lab-services/src/core/middleware" "github.com/gin-gonic/gin" + "github.com/oj-lab/oj-lab-platform/modules/config" + "github.com/oj-lab/oj-lab-platform/modules/log" + "github.com/oj-lab/oj-lab-platform/modules/middleware" ) const ( @@ -29,11 +30,11 @@ var ( ) func init() { - serviceForceConsoleColor = core.AppConfig.GetBool(serviceForceConsoleColorProp) - servicePort = core.AppConfig.GetString(servicePortProp) - serviceMode = core.AppConfig.GetString(serviceModeProp) - swaggerOn = core.AppConfig.GetBool(swaggerOnProp) - frontendDist = core.AppConfig.GetString(frontendDistProp) + serviceForceConsoleColor = config.AppConfig.GetBool(serviceForceConsoleColorProp) + servicePort = config.AppConfig.GetString(servicePortProp) + serviceMode = config.AppConfig.GetString(serviceModeProp) + swaggerOn = config.AppConfig.GetBool(swaggerOnProp) + frontendDist = config.AppConfig.GetString(frontendDistProp) } func GetProjectDir() string { @@ -55,16 +56,16 @@ func main() { if frontendDist != "" { // If dist folder is not empty, serve frontend if _, err := os.Stat(frontendDist); os.IsNotExist(err) { - core.AppLogger().Warn("Frontend dist is set but folder not found") + log.AppLogger().Warn("Frontend dist is set but folder not found") } else { - core.AppLogger().Info("Serving frontend...") + log.AppLogger().Info("Serving frontend...") r.LoadHTMLFiles(frontendDist + "/index.html") handler.SetupFrontendRoute(baseRouter, frontendDist) } } if swaggerOn { - core.AppLogger().Info("Serving swagger Doc...") + log.AppLogger().Info("Serving swagger Doc...") handler.SetupSwaggoRouter(baseRouter) } diff --git a/go.mod b/go.mod index fd57829..c91a304 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/OJ-lab/oj-lab-services +module github.com/oj-lab/oj-lab-platform go 1.21 @@ -11,21 +11,19 @@ require ( gorm.io/gorm v1.25.10 ) -require github.com/swaggo/swag v1.16.3 // indirect +require github.com/swaggo/swag v1.16.3 require ( + github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect - go.uber.org/atomic v1.9.0 // indirect - go.uber.org/multierr v1.9.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect ) require ( github.com/KyleBanks/depth v1.2.1 // indirect - github.com/PuerkitoBio/purell v1.1.1 // indirect - github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/bytedance/sonic v1.10.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect @@ -34,20 +32,19 @@ require ( github.com/dustin/go-humanize v1.0.1 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect - github.com/go-openapi/jsonpointer v0.19.5 // indirect - github.com/go-openapi/jsonreference v0.19.6 // indirect - github.com/go-openapi/spec v0.20.4 // indirect - github.com/go-openapi/swag v0.19.15 // indirect + github.com/go-openapi/jsonpointer v0.21.0 // indirect + github.com/go-openapi/jsonreference v0.21.0 // indirect + github.com/go-openapi/spec v0.21.0 // indirect + github.com/go-openapi/swag v0.23.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/jackc/pgx/v5 v5.4.3 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/klauspost/compress v1.17.6 // indirect github.com/klauspost/cpuid/v2 v2.2.6 // indirect github.com/magiconair/properties v1.8.7 // indirect - github.com/mailru/easyjson v0.7.6 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/minio/md5-simd v1.1.2 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect - github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/rs/xid v1.5.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect @@ -55,7 +52,7 @@ require ( github.com/subosito/gotenv v1.6.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect golang.org/x/arch v0.4.0 // indirect - golang.org/x/tools v0.13.0 // indirect + golang.org/x/tools v0.21.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect google.golang.org/grpc v1.63.2 gopkg.in/ini.v1 v1.67.0 // indirect @@ -82,13 +79,13 @@ require ( github.com/minio/minio-go/v7 v7.0.70 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/swaggo/files v1.0.1 github.com/swaggo/gin-swagger v1.6.0 github.com/ugorji/go/codec v1.2.11 // indirect - golang.org/x/crypto v0.21.0 // indirect - golang.org/x/net v0.23.0 // indirect - golang.org/x/sys v0.18.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect google.golang.org/protobuf v1.34.1 ) diff --git a/go.sum b/go.sum index a83942d..440c1bb 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,5 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= -github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI= -github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= -github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= -github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/alexedwards/argon2id v1.0.0 h1:wJzDx66hqWX7siL/SRUmgz3F8YMrd/nfX/xHHcQQP0w= github.com/alexedwards/argon2id v1.0.0/go.mod h1:tYKkqIjzXvZdzPvADMWOEZ+l6+BD6CtBXMj5fnJppiw= github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= @@ -22,7 +18,6 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo= github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -43,16 +38,14 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= -github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= -github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY= -github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= -github.com/go-openapi/jsonreference v0.19.6 h1:UBIxjkht+AWIgYzCDSv2GN+E/togfwXUJFRTWhl2Jjs= -github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns= -github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M= -github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I= -github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= -github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM= -github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= +github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ= +github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY= +github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF2+tg1TRrwQ= +github.com/go-openapi/jsonreference v0.21.0/go.mod h1:LmZmgsrTkVg9LG4EaHeY8cBDslNPMo06cago5JNLkm4= +github.com/go-openapi/spec v0.21.0 h1:LTVzPc3p/RzRnkQqLRndbAzjY0d0BCL72A6j3CdL9ZY= +github.com/go-openapi/spec v0.21.0/go.mod h1:78u6VdPw81XU44qEWGhtr982gJ5BWg2c0I5XwVMotYk= +github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE= +github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= @@ -95,21 +88,16 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02 github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= -github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= -github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= -github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= -github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= @@ -123,16 +111,15 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= -github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= +github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8= github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= @@ -155,14 +142,14 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE= @@ -176,39 +163,37 @@ github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2 github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= -go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= -go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc= golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= -golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= -golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ= +golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= -golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -217,8 +202,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -226,19 +211,18 @@ golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw= +golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY= google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY= @@ -247,17 +231,13 @@ google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDom google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM= diff --git a/src/service/model/judge.go b/models/judge/judge.go similarity index 98% rename from src/service/model/judge.go rename to models/judge/judge.go index f1c5882..68858e1 100644 --- a/src/service/model/judge.go +++ b/models/judge/judge.go @@ -1,7 +1,9 @@ -package model +package judge import ( "strings" + + "github.com/oj-lab/oj-lab-platform/models" ) // Should contains a priority definition @@ -53,7 +55,7 @@ const ( ) type Judger struct { - MetaFields + models.MetaFields Host string `gorm:"primaryKey" json:"host"` State JudgerState `gorm:"default:offline" json:"status"` } diff --git a/src/service/model/submission.go b/models/judge/submission.go similarity index 56% rename from src/service/model/submission.go rename to models/judge/submission.go index 0d747d6..d8b2463 100644 --- a/src/service/model/submission.go +++ b/models/judge/submission.go @@ -1,7 +1,10 @@ -package model +package judge import ( "github.com/google/uuid" + "github.com/oj-lab/oj-lab-platform/models" + problem_model "github.com/oj-lab/oj-lab-platform/models/problem" + user_model "github.com/oj-lab/oj-lab-platform/models/user" ) type SubmissionStatus string @@ -27,18 +30,18 @@ const ( // Using relationship according to https://gorm.io/docs/belongs_to.html type JudgeTaskSubmission struct { - MetaFields - UID uuid.UUID `gorm:"primaryKey" json:"UID"` - RedisStreamID string `json:"redisStreamID"` - UserAccount string `gorm:"not null" json:"userAccount"` - User User `json:"user"` - ProblemSlug string `gorm:"not null" json:"problemSlug"` - Problem Problem `json:"problem"` - Code string `gorm:"not null" json:"code"` - Language SubmissionLanguage `gorm:"not null" json:"language"` - Status SubmissionStatus `gorm:"default:pending" json:"status"` - VerdictJson string `json:"verdictJson"` - MainResult JudgeVerdict `json:"mainResult"` + models.MetaFields + UID uuid.UUID `gorm:"primaryKey" json:"UID"` + RedisStreamID string `json:"redisStreamID"` + UserAccount string `gorm:"not null" json:"userAccount"` + User user_model.User `json:"user"` + ProblemSlug string `gorm:"not null" json:"problemSlug"` + Problem problem_model.Problem `json:"problem"` + Code string `gorm:"not null" json:"code"` + Language SubmissionLanguage `gorm:"not null" json:"language"` + Status SubmissionStatus `gorm:"default:pending" json:"status"` + VerdictJson string `json:"verdictJson"` + MainResult JudgeVerdict `json:"mainResult"` } func NewSubmission( diff --git a/src/service/mapper/submission.go b/models/judge/submission_db.go similarity index 75% rename from src/service/mapper/submission.go rename to models/judge/submission_db.go index f8fb60a..cbb377c 100644 --- a/src/service/mapper/submission.go +++ b/models/judge/submission_db.go @@ -1,17 +1,17 @@ -package mapper +package judge import ( "fmt" - "github.com/OJ-lab/oj-lab-services/src/service/model" "github.com/google/uuid" + "github.com/oj-lab/oj-lab-platform/models" "gorm.io/gorm" "gorm.io/gorm/clause" ) -func CreateSubmission(tx *gorm.DB, submission model.JudgeTaskSubmission) (*model.JudgeTaskSubmission, error) { +func CreateSubmission(tx *gorm.DB, submission JudgeTaskSubmission) (*JudgeTaskSubmission, error) { submission.UID = uuid.New() - submission.MetaFields = model.NewMetaFields() + submission.MetaFields = models.NewMetaFields() if submission.UserAccount == "" { submission.UserAccount = "anonymous" } @@ -19,9 +19,9 @@ func CreateSubmission(tx *gorm.DB, submission model.JudgeTaskSubmission) (*model return &submission, tx.Create(&submission).Error } -func GetSubmission(tx *gorm.DB, uid string) (*model.JudgeTaskSubmission, error) { - db_submission := model.JudgeTaskSubmission{} - err := tx.Model(&model.JudgeTaskSubmission{}).Preload("User").Preload("Problem").Where("UID = ?", uid).First(&db_submission).Error +func GetSubmission(tx *gorm.DB, uid string) (*JudgeTaskSubmission, error) { + db_submission := JudgeTaskSubmission{} + err := tx.Model(&JudgeTaskSubmission{}).Preload("User").Preload("Problem").Where("UID = ?", uid).First(&db_submission).Error if err != nil { return nil, err } @@ -31,16 +31,16 @@ func GetSubmission(tx *gorm.DB, uid string) (*model.JudgeTaskSubmission, error) type GetSubmissionOptions struct { Selection []string - Statuses []model.SubmissionStatus + Statuses []SubmissionStatus UserAccount *string ProblemSlug *string Offset *int Limit *int - OrderByColumns []model.OrderByColumnOption + OrderByColumns []models.OrderByColumnOption } func BuildGetSubmissionTXByOptions(tx *gorm.DB, options GetSubmissionOptions, isCount bool) *gorm.DB { - tx = tx.Model(&model.JudgeTaskSubmission{}). + tx = tx.Model(&JudgeTaskSubmission{}). Preload(clause.Associations) // See more in: https://gorm.io/docs/preload.html // Preload("User.Roles").Preload("Problem.Tags").Preload(clause.Associations) @@ -72,9 +72,9 @@ func BuildGetSubmissionTXByOptions(tx *gorm.DB, options GetSubmissionOptions, is return tx } -func GetSubmissionListByOptions(tx *gorm.DB, options GetSubmissionOptions) ([]*model.JudgeTaskSubmission, int64, error) { +func GetSubmissionListByOptions(tx *gorm.DB, options GetSubmissionOptions) ([]*JudgeTaskSubmission, int64, error) { tx = BuildGetSubmissionTXByOptions(tx, options, false) - var submissions []*model.JudgeTaskSubmission + var submissions []*JudgeTaskSubmission err := tx.Find(&submissions).Error if err != nil { return nil, 0, err @@ -90,8 +90,8 @@ func GetSubmissionListByOptions(tx *gorm.DB, options GetSubmissionOptions) ([]*m return submissions, count, nil } -func UpdateSubmission(tx *gorm.DB, submission model.JudgeTaskSubmission) error { - updatingSubmission := model.JudgeTaskSubmission{} +func UpdateSubmission(tx *gorm.DB, submission JudgeTaskSubmission) error { + updatingSubmission := JudgeTaskSubmission{} if submission.UID != uuid.Nil { err := tx.Where("uid = ?", submission.UID).First(&updatingSubmission).Error if err != nil { diff --git a/src/service/model/meta.go b/models/meta.go similarity index 97% rename from src/service/model/meta.go rename to models/meta.go index 25e77b9..e2fd099 100644 --- a/src/service/model/meta.go +++ b/models/meta.go @@ -1,4 +1,4 @@ -package model +package models import "time" diff --git a/src/service/model/problem.go b/models/problem/problem.go similarity index 78% rename from src/service/model/problem.go rename to models/problem/problem.go index aed75c6..014258d 100644 --- a/src/service/model/problem.go +++ b/models/problem/problem.go @@ -1,7 +1,9 @@ -package model +package problem + +import "github.com/oj-lab/oj-lab-platform/models" type Problem struct { - MetaFields + models.MetaFields Slug string `gorm:"primaryKey" json:"slug"` Title string `gorm:"not null" json:"title"` Description *string `gorm:"not null" json:"description,omitempty"` @@ -9,19 +11,19 @@ type Problem struct { } type AlgorithmTag struct { - MetaFields + models.MetaFields Name string `gorm:"primaryKey" json:"name"` Problems []*Problem `gorm:"many2many:problem_algorithm_tags;" json:"problems,omitempty"` } type ProblemInfo struct { - MetaFields + models.MetaFields Slug string `json:"slug"` Title string `json:"title"` Tags []*AlgorithmTag `json:"tags"` } -var ProblemInfoSelection = append([]string{"slug", "title"}, MetaFieldsSelection...) +var ProblemInfoSelection = append([]string{"slug", "title"}, models.MetaFieldsSelection...) func (p Problem) ToProblemInfo() ProblemInfo { return ProblemInfo{ diff --git a/src/service/mapper/problem.go b/models/problem/problem_db.go similarity index 70% rename from src/service/mapper/problem.go rename to models/problem/problem_db.go index d5893fc..858590f 100644 --- a/src/service/mapper/problem.go +++ b/models/problem/problem_db.go @@ -1,18 +1,17 @@ -package mapper +package problem import ( - "github.com/OJ-lab/oj-lab-services/src/service/model" "gorm.io/gorm" "gorm.io/gorm/clause" ) -func CreateProblem(tx *gorm.DB, problem model.Problem) error { +func CreateProblem(tx *gorm.DB, problem Problem) error { return tx.Create(&problem).Error } -func GetProblem(tx *gorm.DB, slug string) (*model.Problem, error) { - db_problem := model.Problem{} - err := tx.Model(&model.Problem{}).Preload("Tags").Where("Slug = ?", slug).First(&db_problem).Error +func GetProblem(tx *gorm.DB, slug string) (*Problem, error) { + db_problem := Problem{} + err := tx.Model(&Problem{}).Preload("Tags").Where("Slug = ?", slug).First(&db_problem).Error if err != nil { return nil, err } @@ -21,27 +20,22 @@ func GetProblem(tx *gorm.DB, slug string) (*model.Problem, error) { } func DeleteProblem(tx *gorm.DB, slug string) error { - var problem model.Problem + var problem Problem if err := tx.Where("slug = ?", slug).First(&problem).Error; err != nil { return err } - - if err := tx.Where("problem_slug = ?", slug).Delete(&model.JudgeTaskSubmission{}).Error; err != nil { - return err - } - return tx.Select(clause.Associations).Delete(&problem).Error } -func UpdateProblem(tx *gorm.DB, problem model.Problem) error { - return tx.Model(&model.Problem{Slug: problem.Slug}).Updates(problem).Error +func UpdateProblem(tx *gorm.DB, problem Problem) error { + return tx.Model(&Problem{Slug: problem.Slug}).Updates(problem).Error } type GetProblemOptions struct { Selection []string Slug *string Title *string - Tags []*model.AlgorithmTag + Tags []*AlgorithmTag Offset *int Limit *int } @@ -51,7 +45,7 @@ func buildGetProblemTXByOptions(tx *gorm.DB, options GetProblemOptions, isCount for _, tag := range options.Tags { tagsList = append(tagsList, tag.Name) } - tx = tx.Model(&model.Problem{}) + tx = tx.Model(&Problem{}) if len(options.Selection) > 0 { tx = tx.Select(options.Selection) } @@ -88,13 +82,13 @@ func CountProblemByOptions(tx *gorm.DB, options GetProblemOptions) (int64, error return count, err } -func GetProblemListByOptions(tx *gorm.DB, options GetProblemOptions) ([]model.Problem, int64, error) { +func GetProblemListByOptions(tx *gorm.DB, options GetProblemOptions) ([]Problem, int64, error) { total, err := CountProblemByOptions(tx, options) if err != nil { return nil, 0, err } - problemList := []model.Problem{} + problemList := []Problem{} tx = buildGetProblemTXByOptions(tx, options, false) err = tx.Find(&problemList).Error @@ -105,7 +99,7 @@ func GetProblemListByOptions(tx *gorm.DB, options GetProblemOptions) ([]model.Pr return problemList, total, nil } -func GetTagsList(problem model.Problem) []string { +func GetTagsList(problem Problem) []string { tagsList := []string{} for _, tag := range problem.Tags { tagsList = append(tagsList, tag.Name) diff --git a/src/service/model/user.go b/models/user/user.go similarity index 74% rename from src/service/model/user.go rename to models/user/user.go index 0f9c182..6f4648d 100644 --- a/src/service/model/user.go +++ b/models/user/user.go @@ -1,7 +1,9 @@ -package model +package user + +import "github.com/oj-lab/oj-lab-platform/models" type User struct { - MetaFields + models.MetaFields Account string `gorm:"primaryKey" json:"account"` Name string `json:"name"` Password *string `gorm:"-:all" json:"password,omitempty"` @@ -11,10 +13,10 @@ type User struct { Mobile *string `gorm:"unique" json:"mobile,omitempty"` } -var PublicUserSelection = append([]string{"account", "name"}, MetaFieldsSelection...) +var PublicUserSelection = append([]string{"account", "name"}, models.MetaFieldsSelection...) type Role struct { - MetaFields + models.MetaFields Name string `gorm:"primaryKey" json:"name"` Users []*User `gorm:"many2many:user_roles" json:"users,omitempty"` } diff --git a/src/service/mapper/user.go b/models/user/user_db.go similarity index 65% rename from src/service/mapper/user.go rename to models/user/user_db.go index 3381020..ed82867 100644 --- a/src/service/mapper/user.go +++ b/models/user/user_db.go @@ -1,20 +1,19 @@ -package mapper +package user import ( - "github.com/OJ-lab/oj-lab-services/src/service/model" "github.com/alexedwards/argon2id" "gorm.io/gorm" "gorm.io/gorm/clause" ) // Account, Password, Roles will be used to create a new user. -func CreateUser(tx *gorm.DB, user model.User) error { +func CreateUser(tx *gorm.DB, user User) error { hashedPassword, err := argon2id.CreateHash(*user.Password, argon2id.DefaultParams) if err != nil { return err } - User := model.User{ + User := User{ Name: user.Name, Account: user.Account, HashedPassword: hashedPassword, @@ -24,9 +23,9 @@ func CreateUser(tx *gorm.DB, user model.User) error { return tx.Create(&User).Error } -func GetUser(tx *gorm.DB, account string) (*model.User, error) { - db_user := model.User{} - err := tx.Model(&model.User{}).Preload("Roles").Where("account = ?", account).First(&db_user).Error +func GetUser(tx *gorm.DB, account string) (*User, error) { + db_user := User{} + err := tx.Model(&User{}).Preload("Roles").Where("account = ?", account).First(&db_user).Error if err != nil { return nil, err } @@ -34,10 +33,10 @@ func GetUser(tx *gorm.DB, account string) (*model.User, error) { return &db_user, err } -func GetPublicUser(tx *gorm.DB, account string) (*model.User, error) { +func GetPublicUser(tx *gorm.DB, account string) (*User, error) { - db_user := model.User{} - err := tx.Model(&model.User{}).Preload("Roles").Select(model.PublicUserSelection).Where("account = ?", account).First(&db_user).Error + db_user := User{} + err := tx.Model(&User{}).Preload("Roles").Select(PublicUserSelection).Where("account = ?", account).First(&db_user).Error if err != nil { return nil, err } @@ -45,16 +44,12 @@ func GetPublicUser(tx *gorm.DB, account string) (*model.User, error) { return &db_user, err } -func DeleteUser(tx *gorm.DB, user model.User) error { - if err := tx.Where("user_account = ?", user.Account).Delete(&model.JudgeTaskSubmission{}).Error; err != nil { - return err - } - - return tx.Select(clause.Associations).Delete(&model.User{Account: user.Account}).Error +func DeleteUser(tx *gorm.DB, user User) error { + return tx.Select(clause.Associations).Delete(&User{Account: user.Account}).Error } -func UpdateUser(tx *gorm.DB, update model.User) error { - old := model.User{} +func UpdateUser(tx *gorm.DB, update User) error { + old := User{} err := tx.Where("account = ?", update.Account).First(&old).Error if err != nil { return err @@ -76,7 +71,7 @@ func UpdateUser(tx *gorm.DB, update model.User) error { new.Roles = update.Roles } - return tx.Model(&model.User{Account: new.Account}).Updates(new).Error + return tx.Model(&User{Account: new.Account}).Updates(new).Error } type GetUserOptions struct { @@ -93,7 +88,7 @@ func CountUserByOptions(tx *gorm.DB, options GetUserOptions) (int64, error) { var count int64 tx = tx. - Model(&model.User{}). + Model(&User{}). Where("account = ?", options.Account). Or("email = ?", options.Email). Or("mobile = ?", options.Mobile) @@ -103,13 +98,13 @@ func CountUserByOptions(tx *gorm.DB, options GetUserOptions) (int64, error) { return count, err } -func GetUserByOptions(tx *gorm.DB, options GetUserOptions) ([]model.User, int64, error) { +func GetUserByOptions(tx *gorm.DB, options GetUserOptions) ([]User, int64, error) { total, err := CountUserByOptions(tx, options) if err != nil { return nil, 0, err } - db_users := []model.User{} + db_users := []User{} tx = tx. Where("account = ?", options.Account). @@ -131,7 +126,7 @@ func GetUserByOptions(tx *gorm.DB, options GetUserOptions) ([]model.User, int64, } func CheckUserPassword(tx *gorm.DB, account string, password string) (bool, error) { - user := model.User{} + user := User{} err := tx.Where("account = ?", account).First(&user).Error if err != nil { return false, err diff --git a/src/core/agent/gorm/database.go b/modules/agent/gorm/database.go similarity index 84% rename from src/core/agent/gorm/database.go rename to modules/agent/gorm/database.go index 4547ff4..95705b1 100644 --- a/src/core/agent/gorm/database.go +++ b/modules/agent/gorm/database.go @@ -1,7 +1,7 @@ package gormAgent import ( - "github.com/OJ-lab/oj-lab-services/src/core" + "github.com/oj-lab/oj-lab-platform/modules/config" "gorm.io/driver/postgres" "gorm.io/gorm" ) @@ -13,7 +13,7 @@ var db *gorm.DB var dsn string func init() { - dsn = core.AppConfig.GetString(dsnProp) + dsn = config.AppConfig.GetString(dsnProp) if dsn == "" { panic("database dsn is not set") } diff --git a/src/core/agent/judger/client.go b/modules/agent/judger/client.go similarity index 100% rename from src/core/agent/judger/client.go rename to modules/agent/judger/client.go diff --git a/src/core/agent/judger/judge.go b/modules/agent/judger/judge.go similarity index 100% rename from src/core/agent/judger/judge.go rename to modules/agent/judger/judge.go diff --git a/src/core/agent/judger/state.go b/modules/agent/judger/state.go similarity index 100% rename from src/core/agent/judger/state.go rename to modules/agent/judger/state.go diff --git a/src/core/agent/minio/client.go b/modules/agent/minio/client.go similarity index 78% rename from src/core/agent/minio/client.go rename to modules/agent/minio/client.go index 646ec03..b32adb6 100644 --- a/src/core/agent/minio/client.go +++ b/modules/agent/minio/client.go @@ -4,9 +4,9 @@ import ( "context" "log" - "github.com/OJ-lab/oj-lab-services/src/core" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" + "github.com/oj-lab/oj-lab-platform/modules/config" ) const ( @@ -29,12 +29,12 @@ var ( ) func init() { - endpoint = core.AppConfig.GetString(minioEndpointProp) - accessKeyID = core.AppConfig.GetString(minioAccessKeyProp) - secretAccessKey = core.AppConfig.GetString(minioSecretAccessKeyProp) - useSSL = core.AppConfig.GetBool(minioUseSSLProp) - region = core.AppConfig.GetString(minioRegionProp) - bucketName = core.AppConfig.GetString(minioBucketNameProp) + endpoint = config.AppConfig.GetString(minioEndpointProp) + accessKeyID = config.AppConfig.GetString(minioAccessKeyProp) + secretAccessKey = config.AppConfig.GetString(minioSecretAccessKeyProp) + useSSL = config.AppConfig.GetBool(minioUseSSLProp) + region = config.AppConfig.GetString(minioRegionProp) + bucketName = config.AppConfig.GetString(minioBucketNameProp) } func GetBucketName() string { diff --git a/src/core/agent/minio/local.go b/modules/agent/minio/local.go similarity index 100% rename from src/core/agent/minio/local.go rename to modules/agent/minio/local.go diff --git a/src/core/agent/redis/client.go b/modules/agent/redis/client.go similarity index 76% rename from src/core/agent/redis/client.go rename to modules/agent/redis/client.go index 68d37df..2c06fcb 100644 --- a/src/core/agent/redis/client.go +++ b/modules/agent/redis/client.go @@ -1,7 +1,7 @@ package redisAgent import ( - "github.com/OJ-lab/oj-lab-services/src/core" + "github.com/oj-lab/oj-lab-platform/modules/config" "github.com/redis/go-redis/v9" ) @@ -14,7 +14,7 @@ var ( ) func init() { - redisHost = core.AppConfig.GetString(redisHostProp) + redisHost = config.AppConfig.GetString(redisHostProp) } var redisClient *redis.Client diff --git a/src/core/agent/redis/login_session.go b/modules/agent/redis/login_session.go similarity index 100% rename from src/core/agent/redis/login_session.go rename to modules/agent/redis/login_session.go diff --git a/src/core/auth/jwt.go b/modules/auth/jwt.go similarity index 91% rename from src/core/auth/jwt.go rename to modules/auth/jwt.go index 73c4bfe..084294e 100644 --- a/src/core/auth/jwt.go +++ b/modules/auth/jwt.go @@ -4,16 +4,16 @@ import ( "errors" "time" - "github.com/OJ-lab/oj-lab-services/src/core" "github.com/golang-jwt/jwt/v4" + "github.com/oj-lab/oj-lab-platform/modules/config" ) var jwtSecret string var jwtDuration time.Duration func init() { - jwtSecret = core.AppConfig.GetString("jwt.secret") - jwtDuration = core.AppConfig.GetDuration("jwt.duration") + jwtSecret = config.AppConfig.GetString("jwt.secret") + jwtDuration = config.AppConfig.GetDuration("jwt.duration") } type AuthToken struct { diff --git a/src/core/auth/login_session.go b/modules/auth/login_session.go similarity index 92% rename from src/core/auth/login_session.go rename to modules/auth/login_session.go index 2c5f5ed..cfc11ec 100644 --- a/src/core/auth/login_session.go +++ b/modules/auth/login_session.go @@ -4,8 +4,8 @@ import ( "context" "encoding/json" - redisAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/redis" "github.com/google/uuid" + redisAgent "github.com/oj-lab/oj-lab-platform/modules/agent/redis" ) type LoginSession struct { diff --git a/src/core/config.go b/modules/config/config.go similarity index 98% rename from src/core/config.go rename to modules/config/config.go index 69e865f..58f48ab 100644 --- a/src/core/config.go +++ b/modules/config/config.go @@ -1,4 +1,4 @@ -package core +package config import ( "fmt" @@ -99,5 +99,4 @@ func init() { if err := loadConfig(); err != nil { panic(fmt.Sprintf("Load config with error: %v", err)) } - setupLog() } diff --git a/src/core/error.go b/modules/error.go similarity index 98% rename from src/core/error.go rename to modules/error.go index 88cc0fa..305190e 100644 --- a/src/core/error.go +++ b/modules/error.go @@ -1,4 +1,4 @@ -package core +package modules import ( "fmt" diff --git a/src/core/log.go b/modules/log/log.go similarity index 78% rename from src/core/log.go rename to modules/log/log.go index fdcd0e4..18bac59 100644 --- a/src/core/log.go +++ b/modules/log/log.go @@ -1,9 +1,10 @@ -package core +package log import ( "os" "runtime" + "github.com/oj-lab/oj-lab-platform/modules/config" "github.com/sirupsen/logrus" ) @@ -24,10 +25,14 @@ func setupLog() { logrus.SetOutput(os.Stdout) logrus.SetLevel(logrus.DebugLevel) - lvl := AppConfig.GetString(logLevelProp) + lvl := config.AppConfig.GetString(logLevelProp) logLevel, err := logrus.ParseLevel(lvl) if err == nil { println("log level:", lvl) logrus.SetLevel(logLevel) } } + +func init() { + setupLog() +} diff --git a/modules/middleware/error.go b/modules/middleware/error.go new file mode 100644 index 0000000..cb010db --- /dev/null +++ b/modules/middleware/error.go @@ -0,0 +1,33 @@ +package middleware + +import ( + "fmt" + + "github.com/gin-gonic/gin" + "github.com/oj-lab/oj-lab-platform/modules" + "github.com/oj-lab/oj-lab-platform/modules/log" +) + +func GetServiceError(ginErr gin.Error) *modules.SeviceError { + if modules.IsServiceError(ginErr.Meta) { + return ginErr.Meta.(*modules.SeviceError) + } else { + serviceErr := modules.NewInternalError(fmt.Sprintf("%v", ginErr.Err)) + serviceErr.CaptureStackTrace() + return serviceErr + } +} + +func HandleError(ginCtx *gin.Context) { + ginCtx.Next() + + errCount := len(ginCtx.Errors) + if errCount > 0 { + log.AppLogger().Errorf("Last error from GIN middleware: %+v", ginCtx.Errors[errCount-1].Err) + err := GetServiceError(*ginCtx.Errors[errCount-1]) + ginCtx.JSON(err.Code, gin.H{ + "code": err.Code, + "msg": err.Msg, + }) + } +} diff --git a/src/core/middleware/login_session.go b/modules/middleware/login_session.go similarity index 77% rename from src/core/middleware/login_session.go rename to modules/middleware/login_session.go index 86e83ec..13383f8 100644 --- a/src/core/middleware/login_session.go +++ b/modules/middleware/login_session.go @@ -3,9 +3,9 @@ package middleware import ( "time" - "github.com/OJ-lab/oj-lab-services/src/core" - "github.com/OJ-lab/oj-lab-services/src/core/auth" "github.com/gin-gonic/gin" + "github.com/oj-lab/oj-lab-platform/modules" + "github.com/oj-lab/oj-lab-platform/modules/auth" ) const ( @@ -17,14 +17,14 @@ const ( func HandleRequireLogin(ginCtx *gin.Context) { cookie, err := ginCtx.Cookie(loginSessionIdCookieName) if err != nil { - core.NewUnauthorizedError("login session not found").AppendToGin(ginCtx) + modules.NewUnauthorizedError("login session not found").AppendToGin(ginCtx) ginCtx.Abort() return } ls, err := auth.CheckLoginSession(ginCtx, cookie) if err != nil { - core.NewUnauthorizedError("invalid login session").AppendToGin(ginCtx) + modules.NewUnauthorizedError("invalid login session").AppendToGin(ginCtx) ginCtx.Abort() return } diff --git a/src/service/proto/oj_lab_greeting.proto b/proto/oj_lab_greeting.proto similarity index 79% rename from src/service/proto/oj_lab_greeting.proto rename to proto/oj_lab_greeting.proto index 8a4dec8..bf7cbee 100644 --- a/src/service/proto/oj_lab_greeting.proto +++ b/proto/oj_lab_greeting.proto @@ -4,7 +4,7 @@ syntax = "proto3"; package oj_lab_greeting.protos; -option go_package = "github.com/OJ-lab/oj-lab-services/src/service/proto"; +option go_package = "github.com/oj-lab/oj-lab-platform/proto"; service Greeter { rpc Greeting (GreetingRequest) returns (GreetingResponse) {} diff --git a/src/service/proto/oj_lab_stream.proto b/proto/oj_lab_stream.proto similarity index 83% rename from src/service/proto/oj_lab_stream.proto rename to proto/oj_lab_stream.proto index c3e4f73..1596315 100644 --- a/src/service/proto/oj_lab_stream.proto +++ b/proto/oj_lab_stream.proto @@ -4,7 +4,7 @@ syntax = "proto3"; package oj_lab_stream.protos; -option go_package = "github.com/OJ-lab/oj-lab-services/src/service/proto"; +option go_package = "github.com/oj-lab/oj-lab-platform/proto"; service Streamer { rpc StartStream (StreamRequest) returns (stream StreamResponse) {} diff --git a/scripts/update-frontend-dist.sh b/scripts/update-frontend-dist.sh index 2cf66f8..bcffc5a 100755 --- a/scripts/update-frontend-dist.sh +++ b/scripts/update-frontend-dist.sh @@ -23,6 +23,6 @@ else mkdir -p "$FRONTEND_DIST_PATH" fi -curl -o dist.zip -L https://github.com/OJ-lab/oj-lab-front/releases/download/v0.0.2/dist.zip +curl -o dist.zip -L https://github.com/oj-lab/oj-lab-front/releases/download/v0.0.2/dist.zip unzip -o dist.zip -d "$FRONTEND_DIST_PATH" rm dist.zip \ No newline at end of file diff --git a/src/service/business/judge_stream.go b/services/judge/judge_stream.go similarity index 67% rename from src/service/business/judge_stream.go rename to services/judge/judge_stream.go index 4559685..ac824fe 100644 --- a/src/service/business/judge_stream.go +++ b/services/judge/judge_stream.go @@ -1,10 +1,10 @@ -package business +package judge import ( "context" - redisAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/redis" - "github.com/OJ-lab/oj-lab-services/src/service/model" + judge_model "github.com/oj-lab/oj-lab-platform/models/judge" + redis_agent "github.com/oj-lab/oj-lab-platform/modules/agent/redis" "github.com/redis/go-redis/v9" ) @@ -15,15 +15,15 @@ const ( ) func init() { - redisAgent := redisAgent.GetDefaultRedisClient() + redisAgent := redis_agent.GetDefaultRedisClient() _, err := redisAgent.XGroupCreateMkStream(context.Background(), streamName, consumerGroupName, "0").Result() if err != nil && err != redis.Nil && err.Error() != "BUSYGROUP Consumer Group name already exists" { panic(err) } } -func AddTaskToStream(ctx context.Context, task *model.JudgeTask) (*string, error) { - redisAgent := redisAgent.GetDefaultRedisClient() +func addTaskToStream(ctx context.Context, task *judge_model.JudgeTask) (*string, error) { + redisAgent := redis_agent.GetDefaultRedisClient() id, err := redisAgent.XAdd(ctx, &redis.XAddArgs{ Stream: streamName, Values: task.ToStringMap(), @@ -35,8 +35,8 @@ func AddTaskToStream(ctx context.Context, task *model.JudgeTask) (*string, error return &id, err } -func GetTaskFromStream(ctx context.Context, consumer string) (*model.JudgeTask, error) { - redisAgent := redisAgent.GetDefaultRedisClient() +func getTaskFromStream(ctx context.Context, consumer string) (*judge_model.JudgeTask, error) { + redisAgent := redis_agent.GetDefaultRedisClient() if consumer == "" { consumer = defaultConsumerName } @@ -55,17 +55,17 @@ func GetTaskFromStream(ctx context.Context, consumer string) (*model.JudgeTask, return nil, nil } - task := model.JudgeTask{} + task := judge_model.JudgeTask{} for _, message := range result[0].Messages { - task = *model.JudgeTaskFromMap(message.Values) + task = *judge_model.JudgeTaskFromMap(message.Values) task.RedisStreamID = &message.ID } return &task, nil } -func AckTaskFromStream(ctx context.Context, consumer string, streamID string) error { - redisAgent := redisAgent.GetDefaultRedisClient() +func ackTaskFromStream(ctx context.Context, consumer string, streamID string) error { + redisAgent := redis_agent.GetDefaultRedisClient() // TODO: Some ineffectual assignment here, need to find out why // if consumer == "" { // consumer = defaultConsumerName diff --git a/services/judge/submission.go b/services/judge/submission.go new file mode 100644 index 0000000..5dccf93 --- /dev/null +++ b/services/judge/submission.go @@ -0,0 +1,54 @@ +package judge + +import ( + "context" + + judge_model "github.com/oj-lab/oj-lab-platform/models/judge" + gormAgent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm" +) + +func GetJudgeTaskSubmission(ctx context.Context, uid string) (*judge_model.JudgeTaskSubmission, error) { + db := gormAgent.GetDefaultDB() + submission, err := judge_model.GetSubmission(db, uid) + if err != nil { + return nil, err + } + + return submission, nil +} + +func GetJudgeTaskSubmissionList( + ctx context.Context, options judge_model.GetSubmissionOptions, +) ([]*judge_model.JudgeTaskSubmission, int64, error) { + db := gormAgent.GetDefaultDB() + submissions, total, err := judge_model.GetSubmissionListByOptions(db, options) + if err != nil { + return nil, 0, err + } + + return submissions, total, nil +} + +func CreateJudgeTaskSubmission( + ctx context.Context, submission judge_model.JudgeTaskSubmission, +) (*judge_model.JudgeTaskSubmission, error) { + db := gormAgent.GetDefaultDB() + newSubmission, err := judge_model.CreateSubmission(db, submission) + if err != nil { + return nil, err + } + + task := newSubmission.ToJudgeTask() + streamId, err := addTaskToStream(ctx, &task) + if err != nil { + return nil, err + } + + newSubmission.RedisStreamID = *streamId + err = judge_model.UpdateSubmission(db, *newSubmission) + if err != nil { + return nil, err + } + + return newSubmission, nil +} diff --git a/src/service/task.go b/services/judge/task.go similarity index 50% rename from src/service/task.go rename to services/judge/task.go index 8ffbcd9..3c4e3cd 100644 --- a/src/service/task.go +++ b/services/judge/task.go @@ -1,29 +1,29 @@ -package service +package judge import ( "context" "encoding/json" - "github.com/OJ-lab/oj-lab-services/src/core" - gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" - "github.com/OJ-lab/oj-lab-services/src/service/business" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" + "fmt" + "github.com/google/uuid" + judge_model "github.com/oj-lab/oj-lab-platform/models/judge" + gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm" + "github.com/oj-lab/oj-lab-platform/modules/log" ) -func PickJudgeTask(ctx context.Context, consumer string) (*model.JudgeTask, error) { - task, err := business.GetTaskFromStream(ctx, consumer) +func PickJudgeTask(ctx context.Context, consumer string) (*judge_model.JudgeTask, error) { + task, err := getTaskFromStream(ctx, consumer) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get task from stream: %w", err) } - db := gormAgent.GetDefaultDB() - err = mapper.UpdateSubmission(db, model.JudgeTaskSubmission{ + db := gorm_agent.GetDefaultDB() + err = judge_model.UpdateSubmission(db, judge_model.JudgeTaskSubmission{ UID: uuid.MustParse(task.SubmissionUID), - Status: model.SubmissionStatusRunning, + Status: judge_model.SubmissionStatusRunning, }) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to update submission status: %w", err) } return task, nil @@ -33,15 +33,15 @@ func ReportJudgeTaskResult( ctx context.Context, consumer string, streamID string, verdictJson string, ) error { - db := gormAgent.GetDefaultDB() + db := gorm_agent.GetDefaultDB() mainVerdict, err := parseVerdictJson(verdictJson) if err != nil { return err } - err = mapper.UpdateSubmission(db, model.JudgeTaskSubmission{ + err = judge_model.UpdateSubmission(db, judge_model.JudgeTaskSubmission{ RedisStreamID: streamID, - Status: model.SubmissionStatusFinished, + Status: judge_model.SubmissionStatusFinished, VerdictJson: verdictJson, MainResult: mainVerdict, }) @@ -50,7 +50,7 @@ func ReportJudgeTaskResult( return err } - err = business.AckTaskFromStream(ctx, consumer, streamID) + err = ackTaskFromStream(ctx, consumer, streamID) if err != nil { return err } @@ -71,7 +71,7 @@ type Time struct { Nanos float64 `json:"nanos"` } -func parseVerdictJson(verdictString string) (model.JudgeVerdict, error) { +func parseVerdictJson(verdictString string) (judge_model.JudgeVerdict, error) { var tests []VerdictJson err := json.Unmarshal([]byte(verdictString), &tests) if err != nil { @@ -86,28 +86,28 @@ func parseVerdictJson(verdictString string) (model.JudgeVerdict, error) { AvgMemoryBytes = 0.0 MaxMemoryBytes = 0.0 ) - verdictPriorityMap := map[model.JudgeVerdict]int{ - model.JudgeVerdictCompileError: 0, - model.JudgeVerdictRuntimeError: 1, - model.JudgeVerdictTimeLimitExceeded: 2, - model.JudgeVerdictMemoryLimitExceeded: 3, - model.JudgeVerdictSystemError: 4, - model.JudgeVerdictWrongAnswer: 5, - model.JudgeVerdictAccepted: 6, + verdictPriorityMap := map[judge_model.JudgeVerdict]int{ + judge_model.JudgeVerdictCompileError: 0, + judge_model.JudgeVerdictRuntimeError: 1, + judge_model.JudgeVerdictTimeLimitExceeded: 2, + judge_model.JudgeVerdictMemoryLimitExceeded: 3, + judge_model.JudgeVerdictSystemError: 4, + judge_model.JudgeVerdictWrongAnswer: 5, + judge_model.JudgeVerdictAccepted: 6, } - priorityVerdictMap := map[int]model.JudgeVerdict{ - 0: model.JudgeVerdictCompileError, - 1: model.JudgeVerdictRuntimeError, - 2: model.JudgeVerdictTimeLimitExceeded, - 3: model.JudgeVerdictMemoryLimitExceeded, - 4: model.JudgeVerdictSystemError, - 5: model.JudgeVerdictWrongAnswer, - 6: model.JudgeVerdictAccepted, + priorityVerdictMap := map[int]judge_model.JudgeVerdict{ + 0: judge_model.JudgeVerdictCompileError, + 1: judge_model.JudgeVerdictRuntimeError, + 2: judge_model.JudgeVerdictTimeLimitExceeded, + 3: judge_model.JudgeVerdictMemoryLimitExceeded, + 4: judge_model.JudgeVerdictSystemError, + 5: judge_model.JudgeVerdictWrongAnswer, + 6: judge_model.JudgeVerdictAccepted, } for _, test := range tests { tempMiles := test.TimeUsage.Secs*1000 + test.TimeUsage.Nanos/1000000 - Priority = min(Priority, verdictPriorityMap[model.JudgeVerdict(test.Verdict)]) + Priority = min(Priority, verdictPriorityMap[judge_model.JudgeVerdict(test.Verdict)]) AvgMiles += tempMiles MaxMiles = max(MaxMiles, tempMiles) AvgMemoryBytes += test.MemoryUsageBytes @@ -118,9 +118,9 @@ func parseVerdictJson(verdictString string) (model.JudgeVerdict, error) { AvgMemoryBytes /= float64(totolTestPoint) finalVerdict := priorityVerdictMap[Priority] - core.AppLogger().Debugln(totolTestPoint, finalVerdict, AvgMiles, MaxMiles, AvgMemoryBytes, MaxMemoryBytes) + log.AppLogger().Debugln(totolTestPoint, finalVerdict, AvgMiles, MaxMiles, AvgMemoryBytes, MaxMemoryBytes) - // model.JudgeResult{ + // models.JudgeResult{ // MainVerdict: finalVerdict, // TestPointCount: uint64(totolTestPoint), // MaxTimeMs: uint64(MaxMiles), diff --git a/src/service/problem.go b/services/problem/problem.go similarity index 58% rename from src/service/problem.go rename to services/problem/problem.go index b98a2cf..f4d023a 100644 --- a/src/service/problem.go +++ b/services/problem/problem.go @@ -1,27 +1,26 @@ -package service +package problem import ( "context" - gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" - "github.com/OJ-lab/oj-lab-services/src/service/business" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" + problem_model "github.com/oj-lab/oj-lab-platform/models/problem" + gormAgent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm" + "gorm.io/gorm" ) -func GetProblem(ctx context.Context, slug string) (*model.Problem, error) { +func GetProblem(ctx context.Context, slug string) (*problem_model.Problem, error) { db := gormAgent.GetDefaultDB() - problem, err := mapper.GetProblem(db, slug) + problem, err := problem_model.GetProblem(db, slug) if err != nil { return nil, err } return problem, nil } -func PutProblem(ctx context.Context, problem model.Problem) error { +func PutProblem(ctx context.Context, problem problem_model.Problem) error { db := gormAgent.GetDefaultDB() - err := mapper.CreateProblem(db, problem) + err := problem_model.CreateProblem(db, problem) if err != nil { return err } @@ -30,7 +29,7 @@ func PutProblem(ctx context.Context, problem model.Problem) error { func DeleteProblem(ctx context.Context, slug string) error { db := gormAgent.GetDefaultDB() - err := mapper.DeleteProblem(db, slug) + err := problem_model.DeleteProblem(db, slug) if err != nil { return err } @@ -39,7 +38,7 @@ func DeleteProblem(ctx context.Context, slug string) error { func CheckProblemSlug(ctx context.Context, slug string) (bool, error) { db := gormAgent.GetDefaultDB() - problem, err := mapper.GetProblem(db, slug) + problem, err := problem_model.GetProblem(db, slug) if err != nil { if err == gorm.ErrRecordNotFound { return true, nil @@ -49,18 +48,18 @@ func CheckProblemSlug(ctx context.Context, slug string) (bool, error) { return problem == nil, nil } -func GetProblemInfoList(ctx context.Context) ([]model.ProblemInfo, int64, error) { - return business.GetProblemInfoList(ctx) +func GetProblemInfoList(ctx context.Context) ([]problem_model.ProblemInfo, int64, error) { + return getProblemInfoList(ctx) } func PutProblemPackage(ctx context.Context, slug, zipFile string) error { localDir := "/tmp/" + slug - err := business.UnzipProblemPackage(ctx, zipFile, localDir) + err := unzipProblemPackage(ctx, zipFile, localDir) if err != nil { return err } - err = business.PutProblemPackage(ctx, slug, localDir) + err = putProblemPackage(ctx, slug, localDir) if err != nil { return err } diff --git a/services/problem/problem_info.go b/services/problem/problem_info.go new file mode 100644 index 0000000..43b4700 --- /dev/null +++ b/services/problem/problem_info.go @@ -0,0 +1,26 @@ +package problem + +import ( + "context" + + problem_model "github.com/oj-lab/oj-lab-platform/models/problem" + gormAgent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm" +) + +func getProblemInfoList(ctx context.Context) ([]problem_model.ProblemInfo, int64, error) { + db := gormAgent.GetDefaultDB() + getOptions := problem_model.GetProblemOptions{ + Selection: problem_model.ProblemInfoSelection, + } + + problemList, total, err := problem_model.GetProblemListByOptions(db, getOptions) + if err != nil { + return nil, 0, err + } + + problemInfoList := []problem_model.ProblemInfo{} + for _, problem := range problemList { + problemInfoList = append(problemInfoList, problem.ToProblemInfo()) + } + return problemInfoList, total, nil +} diff --git a/src/service/business/problem_package.go b/services/problem/problem_package.go similarity index 80% rename from src/service/business/problem_package.go rename to services/problem/problem_package.go index 725420c..ee59292 100644 --- a/src/service/business/problem_package.go +++ b/services/problem/problem_package.go @@ -1,4 +1,4 @@ -package business +package problem import ( "archive/zip" @@ -7,10 +7,10 @@ import ( "os" "path/filepath" - minioAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/minio" + minioAgent "github.com/oj-lab/oj-lab-platform/modules/agent/minio" ) -func UnzipProblemPackage(ctx context.Context, zipFile, targetDir string) error { +func unzipProblemPackage(ctx context.Context, zipFile, targetDir string) error { err := os.RemoveAll(targetDir) if err != nil { return err @@ -52,7 +52,7 @@ func UnzipProblemPackage(ctx context.Context, zipFile, targetDir string) error { return nil } -func PutProblemPackage(ctx context.Context, slug string, pkgDir string) error { +func putProblemPackage(ctx context.Context, slug string, pkgDir string) error { err := minioAgent.PutLocalObjects(ctx, slug, pkgDir) if err != nil { return err diff --git a/services/user/user.go b/services/user/user.go new file mode 100644 index 0000000..02c62b6 --- /dev/null +++ b/services/user/user.go @@ -0,0 +1,56 @@ +package user + +import ( + "context" + + user_model "github.com/oj-lab/oj-lab-platform/models/user" + gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm" + "github.com/oj-lab/oj-lab-platform/modules/auth" + "github.com/oj-lab/oj-lab-platform/modules/log" +) + +func GetUser(ctx context.Context, account string) (*user_model.User, error) { + db := gorm_agent.GetDefaultDB() + user, err := user_model.GetUser(db, account) + if err != nil { + return nil, err + } + + return user, nil +} + +func CheckUserExist(ctx context.Context, account string) (bool, error) { + getOptions := user_model.GetUserOptions{ + Account: account, + } + db := gorm_agent.GetDefaultDB() + count, err := user_model.CountUserByOptions(db, getOptions) + if err != nil { + return false, err + } + + if count > 1 { + log.AppLogger().Warnf("user %s has %d records", account, count) + } + + return count > 0, nil +} + +func StartLoginSession(ctx context.Context, account, password string) (*string, error) { + db := gorm_agent.GetDefaultDB() + match, err := user_model.CheckUserPassword(db, account, password) + if err != nil { + return nil, err + } + if !match { + return nil, err + } + + loginSession := auth.NewLoginSession(account) + err = loginSession.SaveToRedis(ctx) + if err != nil { + return nil, err + } + + return &loginSession.Id, nil +} diff --git a/src/application/init_db/main.go b/src/application/init_db/main.go deleted file mode 100644 index ff832be..0000000 --- a/src/application/init_db/main.go +++ /dev/null @@ -1,42 +0,0 @@ -package main - -import ( - "github.com/OJ-lab/oj-lab-services/src/core" - gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" -) - -func main() { - db := gormAgent.GetDefaultDB() - err := db.AutoMigrate(&model.User{}, &model.Problem{}, &model.JudgeTaskSubmission{}, &model.Judger{}) - if err != nil { - panic("failed to migrate database") - } - - err = mapper.CreateUser(db, model.User{ - Name: "admin", - Account: "admin", - Password: func() *string { s := "admin"; return &s }(), - Roles: []*model.Role{ - {Name: "admin"}, - }, - }) - if err != nil { - panic("failed to create admin user") - } - - err = mapper.CreateUser(db, model.User{ - Name: "anonymous", - Account: "anonymous", - Password: func() *string { s := "anonymous"; return &s }(), - Roles: []*model.Role{ - {Name: "anonymous"}, - }, - }) - if err != nil { - panic("failed to create anonymous user") - } - - core.AppLogger().Info("migrate tables ans users success") -} diff --git a/src/application/server/handler/swaggo.go b/src/application/server/handler/swaggo.go deleted file mode 100644 index cce86c6..0000000 --- a/src/application/server/handler/swaggo.go +++ /dev/null @@ -1,33 +0,0 @@ -package handler - -import ( - swaggoGen "github.com/OJ-lab/oj-lab-services/src/application/server/swaggo-gen" - "github.com/gin-gonic/gin" - "github.com/spf13/viper" - swaggerFiles "github.com/swaggo/files" - ginSwagger "github.com/swaggo/gin-swagger" -) - -const ( - servicePortProp = "service.port" -) - -var ( - swaggerHost string -) - -func SetupSwaggoRouter(r *gin.RouterGroup) { - r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) -} - -func init() { - sevicePort := viper.GetString(servicePortProp) - swaggerHost = "localhost" + sevicePort - println("Swagger host is set to: " + swaggerHost) - // programmatically set swagger info - swaggoGen.SwaggerInfo.Title = "OJ Lab Services API" - swaggoGen.SwaggerInfo.Version = "1.0" - swaggoGen.SwaggerInfo.Host = swaggerHost - swaggoGen.SwaggerInfo.BasePath = "/api/v1" - swaggoGen.SwaggerInfo.Schemes = []string{"http"} -} diff --git a/src/core/middleware/error.go b/src/core/middleware/error.go deleted file mode 100644 index 9011049..0000000 --- a/src/core/middleware/error.go +++ /dev/null @@ -1,32 +0,0 @@ -package middleware - -import ( - "fmt" - - "github.com/OJ-lab/oj-lab-services/src/core" - "github.com/gin-gonic/gin" -) - -func GetServiceError(ginErr gin.Error) *core.SeviceError { - if core.IsServiceError(ginErr.Meta) { - return ginErr.Meta.(*core.SeviceError) - } else { - serviceErr := core.NewInternalError(fmt.Sprintf("%v", ginErr.Err)) - serviceErr.CaptureStackTrace() - return serviceErr - } -} - -func HandleError(ginCtx *gin.Context) { - ginCtx.Next() - - errCount := len(ginCtx.Errors) - if errCount > 0 { - core.AppLogger().Errorf("Last error from GIN middleware: %+v", ginCtx.Errors[errCount-1].Err) - err := GetServiceError(*ginCtx.Errors[errCount-1]) - ginCtx.JSON(err.Code, gin.H{ - "code": err.Code, - "msg": err.Msg, - }) - } -} diff --git a/src/service/business/problem_info.go b/src/service/business/problem_info.go deleted file mode 100644 index 2b570e6..0000000 --- a/src/service/business/problem_info.go +++ /dev/null @@ -1,27 +0,0 @@ -package business - -import ( - "context" - - gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" -) - -func GetProblemInfoList(ctx context.Context) ([]model.ProblemInfo, int64, error) { - db := gormAgent.GetDefaultDB() - getOptions := mapper.GetProblemOptions{ - Selection: model.ProblemInfoSelection, - } - - problemList, total, err := mapper.GetProblemListByOptions(db, getOptions) - if err != nil { - return nil, 0, err - } - - problemInfoList := []model.ProblemInfo{} - for _, problem := range problemList { - problemInfoList = append(problemInfoList, problem.ToProblemInfo()) - } - return problemInfoList, total, nil -} diff --git a/src/service/judge.go b/src/service/judge.go deleted file mode 100644 index 469a129..0000000 --- a/src/service/judge.go +++ /dev/null @@ -1,13 +0,0 @@ -package service - -import ( - "context" - - "github.com/OJ-lab/oj-lab-services/src/service/model" -) - -func AddJudger(ctx context.Context, judger model.Judger) error { - // TODO: validate judger - - return nil -} diff --git a/src/service/mapper/judger.go b/src/service/mapper/judger.go deleted file mode 100644 index 54bda43..0000000 --- a/src/service/mapper/judger.go +++ /dev/null @@ -1,16 +0,0 @@ -package mapper - -import ( - "github.com/OJ-lab/oj-lab-services/src/service/model" - "gorm.io/gorm" -) - -func CreateJudger(tx *gorm.DB, judger model.Judger) error { - return tx.Create(&judger).Error -} - -func GetJudgerList(tx *gorm.DB) ([]model.Judger, error) { - var judgers []model.Judger - err := tx.Find(&judgers).Error - return judgers, err -} diff --git a/src/service/submission.go b/src/service/submission.go deleted file mode 100644 index 968820b..0000000 --- a/src/service/submission.go +++ /dev/null @@ -1,59 +0,0 @@ -package service - -import ( - "context" - "fmt" - - "github.com/OJ-lab/oj-lab-services/src/core" - gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" - "github.com/OJ-lab/oj-lab-services/src/service/business" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" -) - -func GetJudgeTaskSubmission(ctx context.Context, uid string) (*model.JudgeTaskSubmission, *core.SeviceError) { - db := gormAgent.GetDefaultDB() - submission, err := mapper.GetSubmission(db, uid) - if err != nil { - return nil, core.NewInternalError("failed to get submission by uid") - } - - return submission, nil -} - - -func GetJudgeTaskSubmissionList( - ctx context.Context, options mapper.GetSubmissionOptions, -) ([]*model.JudgeTaskSubmission, int64, *core.SeviceError) { - db := gormAgent.GetDefaultDB() - submissions, total, err := mapper.GetSubmissionListByOptions(db, options) - if err != nil { - return nil, 0, core.NewInternalError("failed to get submission list") - } - - return submissions, total, nil -} - -func CreateJudgeTaskSubmission( - ctx context.Context, submission model.JudgeTaskSubmission, -) (*model.JudgeTaskSubmission, *core.SeviceError) { - db := gormAgent.GetDefaultDB() - newSubmission, err := mapper.CreateSubmission(db, submission) - if err != nil { - return nil, core.NewInternalError("failed to create submission") - } - - task := newSubmission.ToJudgeTask() - streamId, err := business.AddTaskToStream(ctx, &task) - if err != nil { - return nil, core.NewInternalError(fmt.Sprintf("failed to add task to stream %v", err)) - } - - newSubmission.RedisStreamID = *streamId - err = mapper.UpdateSubmission(db, *newSubmission) - if err != nil { - return nil, core.NewInternalError("failed to update submission") - } - - return newSubmission, nil -} diff --git a/src/service/user.go b/src/service/user.go deleted file mode 100644 index b140cf9..0000000 --- a/src/service/user.go +++ /dev/null @@ -1,57 +0,0 @@ -package service - -import ( - "context" - - "github.com/OJ-lab/oj-lab-services/src/core" - gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" - "github.com/OJ-lab/oj-lab-services/src/core/auth" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" -) - -func GetUser(ctx context.Context, account string) (*model.User, *core.SeviceError) { - db := gormAgent.GetDefaultDB() - user, err := mapper.GetUser(db, account) - if err != nil { - return nil, core.NewInternalError("failed to get user") - } - - return user, nil -} - -func CheckUserExist(ctx context.Context, account string) (bool, error) { - getOptions := mapper.GetUserOptions{ - Account: account, - } - db := gormAgent.GetDefaultDB() - count, err := mapper.CountUserByOptions(db, getOptions) - if err != nil { - return false, err - } - - if count > 1 { - core.AppLogger().Warnf("user %s has %d records", account, count) - } - - return count > 0, nil -} - -func StartLoginSession(ctx context.Context, account, password string) (*string, *core.SeviceError) { - db := gormAgent.GetDefaultDB() - match, err := mapper.CheckUserPassword(db, account, password) - if err != nil { - return nil, core.NewInternalError(err.Error()) - } - if !match { - return nil, core.NewUnauthorizedError("invalid account or password") - } - - loginSession := auth.NewLoginSession(account) - err = loginSession.SaveToRedis(ctx) - if err != nil { - return nil, core.NewInternalError(err.Error()) - } - - return &loginSession.Id, nil -} diff --git a/tests/core/jwt_test.go b/tests/core/jwt_test.go index 930426a..a368014 100644 --- a/tests/core/jwt_test.go +++ b/tests/core/jwt_test.go @@ -4,7 +4,7 @@ import ( "log" "testing" - "github.com/OJ-lab/oj-lab-services/src/core/auth" + "github.com/oj-lab/oj-lab-platform/modules/auth" ) func TestGenerateTokenString(t *testing.T) { diff --git a/tests/core/redis_test.go b/tests/core/redis_test.go index 17ad692..ff955fe 100644 --- a/tests/core/redis_test.go +++ b/tests/core/redis_test.go @@ -6,7 +6,7 @@ import ( "testing" "time" - oj_lab_proto "github.com/OJ-lab/oj-lab-services/src/service/proto" + oj_lab_proto "github.com/oj-lab/oj-lab-platform/proto" "github.com/redis/go-redis/v9" "google.golang.org/protobuf/proto" ) diff --git a/tests/mapper/judger_test.go b/tests/mapper/judger_test.go deleted file mode 100644 index fce9f22..0000000 --- a/tests/mapper/judger_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package mapper_test - -import ( - "testing" - - gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" -) - -func TestJudgerMapper(t *testing.T) { - db := gormAgent.GetDefaultDB() - _, err := mapper.GetJudgerList(db) - if err != nil { - t.Error(err) - } -} diff --git a/tests/mapper/problem_test.go b/tests/models/problem_test.go similarity index 56% rename from tests/mapper/problem_test.go rename to tests/models/problem_test.go index a7a76e9..f2f4ded 100644 --- a/tests/mapper/problem_test.go +++ b/tests/models/problem_test.go @@ -1,31 +1,30 @@ -package mapper_test +package models_test import ( "encoding/json" "fmt" "testing" - gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" + problem_model "github.com/oj-lab/oj-lab-platform/models/problem" + gormAgent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm" ) func TestProblemMapper(t *testing.T) { db := gormAgent.GetDefaultDB() description := "Given two integer A and B, please output the answer of A+B." - problem := model.Problem{ + problem := problem_model.Problem{ Slug: "a-plus-b-problem", Title: "A+B Problem", Description: &description, - Tags: []*model.AlgorithmTag{{Name: "tag1"}, {Name: "tag2"}}, + Tags: []*problem_model.AlgorithmTag{{Name: "tag1"}, {Name: "tag2"}}, } - err := mapper.CreateProblem(db, problem) + err := problem_model.CreateProblem(db, problem) if err != nil { t.Error(err) } - dbProblem, err := mapper.GetProblem(db, problem.Slug) + dbProblem, err := problem_model.GetProblem(db, problem.Slug) if err != nil { t.Error(err) } @@ -36,13 +35,13 @@ func TestProblemMapper(t *testing.T) { } fmt.Printf("%+v\n", string(problemJson)) - problemOption := mapper.GetProblemOptions{ - Selection: model.ProblemInfoSelection, - Tags: []*model.AlgorithmTag{{Name: "tag1"}}, + problemOption := problem_model.GetProblemOptions{ + Selection: problem_model.ProblemInfoSelection, + Tags: []*problem_model.AlgorithmTag{{Name: "tag1"}}, Slug: &problem.Slug, } - problemList, problemCount, err := mapper.GetProblemListByOptions(db, problemOption) + problemList, problemCount, err := problem_model.GetProblemListByOptions(db, problemOption) if err != nil { t.Error(err) } @@ -57,7 +56,7 @@ func TestProblemMapper(t *testing.T) { } fmt.Printf("%+v\n", string(problemListJson)) - err = mapper.DeleteProblem(db, problem.Slug) + err = problem_model.DeleteProblem(db, problem.Slug) if err != nil { t.Error(err) } diff --git a/tests/mapper/user_test.go b/tests/models/user_test.go similarity index 58% rename from tests/mapper/user_test.go rename to tests/models/user_test.go index fbc95d1..4109454 100644 --- a/tests/mapper/user_test.go +++ b/tests/models/user_test.go @@ -1,28 +1,27 @@ -package mapper_test +package models_test import ( "encoding/json" "fmt" "testing" - gormAgent "github.com/OJ-lab/oj-lab-services/src/core/agent/gorm" - "github.com/OJ-lab/oj-lab-services/src/service/mapper" - "github.com/OJ-lab/oj-lab-services/src/service/model" + user_model "github.com/oj-lab/oj-lab-platform/models/user" + gormAgent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm" ) func TestUserMapper(t *testing.T) { db := gormAgent.GetDefaultDB() - user := model.User{ + user := user_model.User{ Account: "test", Password: func() *string { s := "test"; return &s }(), - Roles: []*model.Role{{Name: "test"}}, + Roles: []*user_model.Role{{Name: "test"}}, } - err := mapper.CreateUser(db, user) + err := user_model.CreateUser(db, user) if err != nil { t.Error(err) } - dbUser, err := mapper.GetUser(db, user.Account) + dbUser, err := user_model.GetUser(db, user.Account) if err != nil { t.Error(err) } @@ -32,7 +31,7 @@ func TestUserMapper(t *testing.T) { } fmt.Printf("%+v\n", string(userJson)) - dbPublicUser, err := mapper.GetPublicUser(db, user.Account) + dbPublicUser, err := user_model.GetPublicUser(db, user.Account) if err != nil { t.Error(err) } @@ -42,7 +41,7 @@ func TestUserMapper(t *testing.T) { } fmt.Printf("%+v\n", string(publicUserJson)) - err = mapper.DeleteUser(db, user) + err = user_model.DeleteUser(db, user) if err != nil { t.Error(err) }