Skip to content

Commit

Permalink
Refactor project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Jul 21, 2024
1 parent bfc6e00 commit 633c511
Show file tree
Hide file tree
Showing 51 changed files with 393 additions and 420 deletions.
10 changes: 2 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

*.ini
!package/configs/ini/example.ini
!package/configs/ini/test.ini

artifacts/
dist.zip

# Ignore generated files
Expand All @@ -27,3 +19,5 @@ dist.zip

bin/
__debug_bin**

override.toml
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"ms-azuretools.vscode-docker",
"tamasfe.even-better-toml",
"pkief.material-icon-theme",
"bierner.markdown-preview-github-styles",
"davidanson.vscode-markdownlint"
]
}
10 changes: 2 additions & 8 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
{
"material-icon-theme.folders.associations": {
"application": "core",
"mapper": "database",
"migrate_db": "import",
"service": "components",
"business": "hook",
"postman": "api",
".github/workflows": "github",
"test-collection": "resource"
"problem_packages": "packages",
"modules/agent": "client"
},
"go.testFlags": [
"-v",
Expand Down
22 changes: 13 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
FROM golang:latest as build

COPY . /oj-lab-platform-build
WORKDIR /workdir

COPY . /workdir

WORKDIR /oj-lab-platform-build
RUN apt update && apt install -y make zip curl
RUN make build
RUN ./scripts/update-frontend-dist.sh /oj-lab-platform-build/frontend_dist
RUN make get-front


FROM ubuntu:latest

WORKDIR /workdir
WORKDIR /oj-lab-platform

COPY --from=build /workdir/bin/web_server /usr/local/bin/web_server
COPY --from=build /workdir/frontend/dist ./frontend_dist

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 config.toml ./config.toml

COPY workdirs/docker/config.toml /workdir/config.toml
ENV OJ_LAB_SERVICE_ENV='production'
ENV DATABASE_DSN='user=postgres password=postgres host=host.docker.internal port=5432 dbname=oj_lab sslmode=disable TimeZone=Asia/Shanghai'
ENV REDIS_HOSTS='["host.docker.internal:6379"]'
ENV MINIO_ENDPOINT='http://host.docker.internal:9000'

ENV OJ_LAB_SERVICE_ENV=production
ENV OJ_LAB_WORKDIR=/workdir
EXPOSE 8080
CMD [ "web_server" ]
68 changes: 40 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
OS := $(shell uname -s)

DEV_WORKDIR := workdirs/development
DB_DOCKER_COMPOSE_FILE := $(DEV_WORKDIR)/docker-compose.yml
JUDGER_DOCKER_COMPOSE_FILE := $(DEV_WORKDIR)/judger/docker-compose.yml
FRONTEND_DIST_DIR := $(DEV_WORKDIR)/frontend_dist
FRONTEND_DIST_DIR := frontend/dist
FRONTEND_DIST_URL := https://github.com/oj-lab/oj-lab-front/releases/download/v0.0.3/dist.zip
ICPC_PROBLEM_PACKAGES_DIR := problem_packages/icpc
ICPC_PROBLEM_PACKAGES_URL := https://github.com/oj-lab/problem-packages/releases/download/v0.0.1/icpc_problem.zip

.PHONY: help
help:
@echo "Usage: make [target]"
@echo ""
@echo "Targets:"
@echo " build - Build the application, swagger document will be generated"
@echo " run - Run the application"
@echo " clean - Clean the build"
@echo " check - Run go vet"
@echo " test - Run tests, database will be setup"
@echo " gen-swagger - Generate swagger document"
@echo " setup-dependencies - Setup the dependencies docker image"
@echo " unset-dependencies - Unset the dependencies docker image"
@echo " get-front - Get the frontend files"
@echo " build - Build the application, swagger document will be generated"
@echo " run - Run the application"
@echo " clean - Clean the build"
@echo " check - Run go vet"
@echo " test - Run tests, database will be setup"
@echo " gen-swagger - Generate swagger document"
@echo " setup-dependencies - Setup the dependencies docker image"
@echo " unset-dependencies - Unset the dependencies docker image"
@echo " get-front - Get the frontend files"
@echo " update-front - Update the frontend files"
@echo " get-problem-packages - Get the problem packages"
@echo " update-problem-packages - Update the problem packages"

.PHONY: build
build: gen-swagger gen-proto
@echo "Building on $(OS)"
go mod tidy
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
go build -o bin/ ./cmd/...

.PHONY: run
run: build
Expand Down Expand Up @@ -56,23 +56,35 @@ gen-proto: install-proto

.PHONY: unset-dependencies
unset-dependencies:
docker compose -f $(JUDGER_DOCKER_COMPOSE_FILE) -p oj-lab-judger stop
docker compose -f $(JUDGER_DOCKER_COMPOSE_FILE) -p oj-lab-judger rm -f
docker compose -f $(DB_DOCKER_COMPOSE_FILE) -p oj-lab-dbs stop
docker compose -f $(DB_DOCKER_COMPOSE_FILE) -p oj-lab-dbs rm -f
docker compose stop postgres redis minio clickhouse
docker compose rm -f postgres redis minio clickhouse

.PHONY: setup-dependencies
setup-dependencies: unset-dependencies build
docker compose -f $(DB_DOCKER_COMPOSE_FILE) -p oj-lab-dbs up -d
setup-dependencies: unset-dependencies build get-front get-problem-packages
docker compose up -d postgres redis minio clickhouse
@echo "Wait 10 seconds for db setup"
sleep 10s
./bin/init_db
./bin/problem_loader
docker compose -f $(JUDGER_DOCKER_COMPOSE_FILE) -p oj-lab-judger up -d
./bin/init

.PHONY: get-front
get-front:
./scripts/update-frontend-dist.sh $(FRONTEND_DIST_DIR)
./scripts/download_and_unzip.sh $(FRONTEND_DIST_DIR) $(FRONTEND_DIST_URL) \
OVERRIDE=false

.PHONY: update-front
update-front:
./scripts/download_and_unzip.sh $(FRONTEND_DIST_DIR) $(FRONTEND_DIST_URL) \
OVERRIDE=true

.PHONY: get-problem-packages
get-problem-packages:
./scripts/download_and_unzip.sh $(ICPC_PROBLEM_PACKAGES_DIR) $(ICPC_PROBLEM_PACKAGES_URL) \
OVERRIDE=false

.PHONY: update-problem-packages
update-problem-packages:
./scripts/download_and_unzip.sh $(ICPC_PROBLEM_PACKAGES_DIR) $(ICPC_PROBLEM_PACKAGES_URL) \
OVERRIDE=true

.PHONY: check
check: gen-proto install-cilint
Expand Down Expand Up @@ -101,7 +113,7 @@ install-cilint:
.PHONY: install-proto
install-proto:
@# Referencing https://grpc.io/docs/protoc-installation/
@./scripts/install-protoc.sh
@./scripts/install_protoc.sh
@# Track https://grpc.io/docs/languages/go/quickstart/ for update
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
39 changes: 14 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

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)
- PostgreSQL (or other SQL database in the future) for data storage
- Redis for caching & session management
- MinIO (or other S3 like storage) for file storage
- ClickHouse for analytics (currently not developed)
- [Judger](https://github.com/oj-lab/judger) for judging

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 All @@ -24,31 +25,19 @@ Run `make setup-dependencies` to start these services and load the initial data.
Launch the programs with VSCode launch configurations is the most recommended way.
It will automatically set the environment and run the program in debug mode.

### Optional
### Run Judger

There is also some optional approach you may want to use.
There is a `judger` service in the project's `docker-compose.yml`.
It won't start from the `make setup-dependencies` command by default
(since it takes time to let MinIO & PostgreSQL start up).

#### Serve frontend
Run `docker-compose up -d judger` to start the judger service.

Use `make get-front` to get the frontend dist codes.
> In development config by default, it points to the postion where the frontend codes are located,
> so you will automatically get the frontend view when you start the program.
### Manage DB data

#### Generate Swagger docs

Use `make gen-swagger` to generate swagger docs.

#### Set environment variables

The following environment variables are available to modify the behavior of the program:

- OJ_LAB_SERVICE_ENV: The environment of the service, default to `development`
- OJ_LAB_WORKDIR: Directly set the path of the workdir, application will automatically locate the workdir if not set
(it will be set to `workdirs/<service_env>` in this project by default)

#### Manage DB data

Along with the `make setup-dependencies`, we provide `adminer` to access PostgreSQL & MinIO with its web interface.
We provide `adminer` to access PostgreSQL data.
You can optionally run `docker-compose up -d adminer` to start the service,
or just continue to use your own database management tool.

> Remember to set the type of the database to `PostgreSQL` when login to adminer.
Expand Down
28 changes: 28 additions & 0 deletions cmd/init/casbin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"github.com/oj-lab/oj-lab-platform/cmd/web_server/handler"
casbin_agent "github.com/oj-lab/oj-lab-platform/modules/agent/casbin"
)

func loadCasbinPolicies() {
enforcer := casbin_agent.GetDefaultCasbinEnforcer()

_, err := enforcer.AddGroupingPolicies([][]string{
{`user_root`, `role_super`, `system`},
{`role_super`, `role_admin`, `system`},
})
if err != nil {
panic(err)
}

err = handler.AddUserCasbinPolicies()
if err != nil {
panic(err)
}

err = enforcer.SavePolicy()
if err != nil {
panic(err)
}
}
42 changes: 42 additions & 0 deletions cmd/init/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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"
gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
log_module "github.com/oj-lab/oj-lab-platform/modules/log"
)

func initDB() {
db := gorm_agent.GetDefaultDB()
err := db.AutoMigrate(
&user_model.User{},
&user_model.Role{},
&problem_model.Problem{},
&judge_model.Judge{},
&judge_model.JudgeResult{},
)
if err != nil {
panic("failed to migrate database")
}

err = user_model.CreateUser(db, user_model.User{
Name: "root",
Account: "root",
Password: func() *string { s := ""; return &s }(),
})
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 := ""; return &s }(),
})
if err != nil {
panic("failed to create anonymous user")
}
log_module.AppLogger().Info("migrate tables ans users success")
}
13 changes: 13 additions & 0 deletions cmd/init/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"context"
)

func main() {
ctx := context.Background()
initDB()
loadCasbinPolicies()
loadProblemPackages(ctx)
println("init success")
}
Loading

0 comments on commit 633c511

Please sign in to comment.