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 20, 2024
1 parent bfc6e00 commit 76e4856
Show file tree
Hide file tree
Showing 36 changed files with 114 additions and 155 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
!package/configs/ini/example.ini
!package/configs/ini/test.ini

artifacts/
frontend/dist/
dist.zip

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

bin/
__debug_bin**

override.toml
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" ]
15 changes: 5 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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
DOCKER_COMPOSE_FILE := docker-compose.yml
FRONTEND_DIST_DIR := frontend/dist

.PHONY: help
help:
Expand Down Expand Up @@ -56,19 +54,16 @@ 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 -f $(DOCKER_COMPOSE_FILE) -p oj-lab-dbs stop
docker compose -f $(DOCKER_COMPOSE_FILE) -p oj-lab-dbs rm -f

.PHONY: setup-dependencies
setup-dependencies: unset-dependencies build
docker compose -f $(DB_DOCKER_COMPOSE_FILE) -p oj-lab-dbs up -d
docker compose -f $(DOCKER_COMPOSE_FILE) -p oj-lab-dbs up -d
@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

.PHONY: get-front
get-front:
Expand Down
4 changes: 2 additions & 2 deletions cmd/init_db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
user_model "github.com/oj-lab/oj-lab-platform/models/user"
casbin_agent "github.com/oj-lab/oj-lab-platform/modules/agent/casbin"
gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
"github.com/oj-lab/oj-lab-platform/modules/log"
log_module "github.com/oj-lab/oj-lab-platform/modules/log"
)

func main() {
Expand Down Expand Up @@ -58,7 +58,7 @@ func main() {
if err != nil {
panic("failed to create anonymous user")
}
log.AppLogger().Info("migrate tables ans users success")
log_module.AppLogger().Info("migrate tables ans users success")

err = casbin_agent.LoadDefaultCasbinPolicies()
if err != nil {
Expand Down
26 changes: 13 additions & 13 deletions cmd/problem_loader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
problem_model "github.com/oj-lab/oj-lab-platform/models/problem"
gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
minio_agent "github.com/oj-lab/oj-lab-platform/modules/agent/minio"
"github.com/oj-lab/oj-lab-platform/modules/config"
"github.com/oj-lab/oj-lab-platform/modules/log"
config_module "github.com/oj-lab/oj-lab-platform/modules/config"
log_module "github.com/oj-lab/oj-lab-platform/modules/log"
yaml "gopkg.in/yaml.v2"
)

Expand All @@ -31,13 +31,13 @@ func main() {
// parse problem.md as description.
// 2. insert object into minio storage.
var (
packagePath string = path.Join(config.Workdir, "problem_packages")
packagePath string = path.Join(config_module.ProjectRoot(), "problem_packages")
title string
slug string
)
err := filepath.Walk(packagePath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
log.AppLogger().WithError(err).Error("Walk package path failed")
log_module.AppLogger().WithError(err).Error("Walk package path failed")
return err
}
if info == nil {
Expand All @@ -47,31 +47,31 @@ func main() {
return nil
}
relativePath := strings.Replace(path, packagePath, "", 1)
log.AppLogger().WithField("relativePath", relativePath).Debug("Read file from package")
log_module.AppLogger().WithField("relativePath", relativePath).Debug("Read file from package")
if filepath.Base(relativePath) == "problem.yaml" {
resultMap := make(map[string]interface{})
yamlFile, err := os.ReadFile(path)
if err != nil {
log.AppLogger().WithError(err).Error("Read problem.yaml failed")
log_module.AppLogger().WithError(err).Error("Read problem.yaml failed")
}
err = yaml.Unmarshal(yamlFile, &resultMap)
if err != nil {
log.AppLogger().WithError(err).Error("Unmarshal problem.yaml failed")
log_module.AppLogger().WithError(err).Error("Unmarshal problem.yaml failed")
}
title = resultMap["name"].(string)
if title == "" {
log.AppLogger().Error("Problem title is empty")
log_module.AppLogger().Error("Problem title is empty")
}
slug = strings.Split(relativePath, "/")[1]
log.AppLogger().WithField("title", title).WithField("slug", slug).Debug("Read problem.yaml")
log_module.AppLogger().WithField("title", title).WithField("slug", slug).Debug("Read problem.yaml")
}
if filepath.Base(relativePath) == "problem.md" {
content, err := os.ReadFile(path)
if err != nil {
log.AppLogger().WithError(err).Error("Read problem.md failed")
log_module.AppLogger().WithError(err).Error("Read problem.md failed")
}
description := string(content)
log.AppLogger().WithField("description", description).Debug("Read problem.md")
log_module.AppLogger().WithField("description", description).Debug("Read problem.md")
err = problem_model.CreateProblem(db, problem_model.Problem{
Slug: slug,
Title: title,
Expand All @@ -90,13 +90,13 @@ func main() {
path,
minio.PutObjectOptions{})
if err != nil {
log.AppLogger().WithError(err).Error("Put object to minio failed")
log_module.AppLogger().WithError(err).Error("Put object to minio failed")
}
return err
})
if err != nil {
panic(err)
}

log.AppLogger().Info("Problem loaded")
log_module.AppLogger().Info("Problem loaded")
}
4 changes: 2 additions & 2 deletions cmd/rpc_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"net"

"github.com/oj-lab/oj-lab-platform/cmd/rpc_server/impls"
"github.com/oj-lab/oj-lab-platform/modules/config"
config_module "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 = config.AppConfig.GetInt(portProp)
port = config_module.AppConfig().GetInt(portProp)
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/web_server/handler/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/oj-lab/oj-lab-platform/modules/log"
log_module "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())
log.AppLogger().Infof("Send message:\n%s", message)
log_module.AppLogger().Infof("Send message:\n%s", message)
fmt.Fprint(w, message)
time.Sleep(1 * time.Second)
counter++
Expand Down
20 changes: 10 additions & 10 deletions cmd/web_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/oj-lab/oj-lab-platform/cmd/web_server/middleware"

"github.com/gin-gonic/gin"
"github.com/oj-lab/oj-lab-platform/modules/config"
"github.com/oj-lab/oj-lab-platform/modules/log"
config_module "github.com/oj-lab/oj-lab-platform/modules/config"
log_module "github.com/oj-lab/oj-lab-platform/modules/log"
)

const (
Expand All @@ -30,11 +30,11 @@ var (
)

func init() {
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)
serviceForceConsoleColor = config_module.AppConfig().GetBool(serviceForceConsoleColorProp)
servicePort = config_module.AppConfig().GetString(servicePortProp)
serviceMode = config_module.AppConfig().GetString(serviceModeProp)
swaggerOn = config_module.AppConfig().GetBool(swaggerOnProp)
frontendDist = config_module.AppConfig().GetString(frontendDistProp)
}

func GetProjectDir() string {
Expand All @@ -56,16 +56,16 @@ func main() {
if frontendDist != "" {
// If dist folder is not empty, serve frontend
if _, err := os.Stat(frontendDist); os.IsNotExist(err) {
log.AppLogger().Warn("Frontend dist is set but folder not found")
log_module.AppLogger().Warn("Frontend dist is set but folder not found")
} else {
log.AppLogger().Info("Serving frontend...")
log_module.AppLogger().Info("Serving frontend...")
r.LoadHTMLFiles(frontendDist + "/index.html")
handler.SetupFrontendRoute(baseRouter, frontendDist)
}
}

if swaggerOn {
log.AppLogger().Info("Serving swagger Doc...")
log_module.AppLogger().Info("Serving swagger Doc...")
handler.SetupSwaggoRouter(baseRouter)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/web_server/middleware/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/oj-lab/oj-lab-platform/modules"
casbin_agent "github.com/oj-lab/oj-lab-platform/modules/agent/casbin"
"github.com/oj-lab/oj-lab-platform/modules/log"
log_module "github.com/oj-lab/oj-lab-platform/modules/log"
)

func BuildCasbinEnforceHandlerWithDomain(domain string) gin.HandlerFunc {
Expand All @@ -20,7 +20,7 @@ func BuildCasbinEnforceHandlerWithDomain(domain string) gin.HandlerFunc {

allow, err := enforcer.Enforce(ls.Key.Account, "_", domain, path, method)
if err != nil {
log.AppLogger().Errorf("Failed to enforce: %v", err)
log_module.AppLogger().Errorf("Failed to enforce: %v", err)
modules.NewInternalError("Failed to enforce").AppendToGin(ginCtx)
return
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/web_server/middleware/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

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

func GetServiceError(ginErr gin.Error) *modules.SeviceError {
Expand All @@ -23,7 +23,7 @@ func HandleError(ginCtx *gin.Context) {

errCount := len(ginCtx.Errors)
if errCount > 0 {
log.AppLogger().Errorf("Last error from GIN middleware: %+v", ginCtx.Errors[errCount-1].Err)
log_module.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,
Expand Down
3 changes: 1 addition & 2 deletions workdirs/development/config.toml → config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ duration = "24h"
force_console_color = true
auth_on = true
port = ":8080"
cookie.age = "24h"
mode = "debug"
swagger_on = true
frontend_dist = "workdirs/development/frontend_dist"
frontend_dist = "frontend/dist"

[rpc-server]
port = 50051
Expand Down
12 changes: 11 additions & 1 deletion workdirs/development/docker-compose.yml → docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ services:
- "9100:9000"
environment:
- CLICKHOUSE_USER=clickhouse
- CLICKHOUSE_PASSWORD=clickhouse
- CLICKHOUSE_PASSWORD=clickhouse

judger:
image: ghcr.io/oj-lab/judger:main
pull_policy: always
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
- ENABLE_RCLONE=true
ports:
- 8000:8000
6 changes: 3 additions & 3 deletions modules/agent/casbin/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
rediswatcher "github.com/casbin/redis-watcher/v2"
gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
redis_agent "github.com/oj-lab/oj-lab-platform/modules/agent/redis"
"github.com/oj-lab/oj-lab-platform/modules/log"
log_module "github.com/oj-lab/oj-lab-platform/modules/log"
)

var casbinEnforcer *casbin.SyncedCachedEnforcer
Expand Down Expand Up @@ -54,10 +54,10 @@ func GetDefaultCasbinEnforcer() *casbin.SyncedCachedEnforcer {
if err != nil {
panic(err)
}
log.AppLogger().Info("Casbin enforcer watcher initialized")
log_module.AppLogger().Info("Casbin enforcer watcher initialized")
}
casbinEnforcer.AddFunction("keyMatchGin", keyMatchGinFunc)
log.AppLogger().Info("Casbin enforcer initialized")
log_module.AppLogger().Info("Casbin enforcer initialized")
}

return casbinEnforcer
Expand Down
4 changes: 2 additions & 2 deletions modules/agent/gorm/database.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gorm_agent

import (
"github.com/oj-lab/oj-lab-platform/modules/config"
config_module "github.com/oj-lab/oj-lab-platform/modules/config"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
Expand All @@ -13,7 +13,7 @@ var db *gorm.DB
var dsn string

func init() {
dsn = config.AppConfig.GetString(dsnProp)
dsn = config_module.AppConfig().GetString(dsnProp)
if dsn == "" {
panic("database dsn is not set")
}
Expand Down
Loading

0 comments on commit 76e4856

Please sign in to comment.