Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactor project structure #76

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.devcontainer/
.github/
.vscode/
bin/
tests/
artifacts/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ dist.zip
**/swaggo-gen/docs.go

bin/
__debug_bin**
__debug_bin**
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"mode": "auto",
"cwd": "${workspaceFolder}",
"program": "src/application/server/main.go",
"program": "cmd/web_server/main.go",
}
]
}
13 changes: 3 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,12 +12,12 @@ 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

ENV OJ_LAB_SERVICE_ENV=production
ENV OJ_LAB_WORKDIR=/workdir
EXPOSE 8080
CMD [ "oj-lab-service" ]
CMD [ "web_server" ]
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ 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
.PHONY: gen-proto
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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down
47 changes: 47 additions & 0 deletions cmd/init_db/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
)
Expand Down Expand Up @@ -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"},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/application/rpc_server/main.go → cmd/rpc_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -20,7 +20,7 @@ const (
)

var (
port = core.AppConfig.GetInt(portProp)
port = config.AppConfig.GetInt(portProp)
)

func main() {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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++
Expand Down
Original file line number Diff line number Diff line change
@@ -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"`
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Loading
Loading