Skip to content

Commit

Permalink
Slhmy/refactor structure (#33)
Browse files Browse the repository at this point in the history
* Reduce root folders & do some perf

* Fix docker
  • Loading branch information
slhmy authored Jan 14, 2024
1 parent 291c4d7 commit 5fdf3f6
Show file tree
Hide file tree
Showing 91 changed files with 265 additions and 352 deletions.
7 changes: 2 additions & 5 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
.github/
.vscode/
test/
test-collection/
frontend/
postman/
oj-lab-services-structure.drawio
tests/
artifacts/
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
# vendor/

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

config/override.toml
bin/
configs/override.toml
artifacts/
dist.zip
frontend/dist/

# Ignore generated files
*.pb.go
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

60 changes: 32 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
OS := $(shell uname -s)

.PHONY: help
help:
@echo "Usage: make [target]"
@echo ""
@echo "Targets:"
@echo " build - Build the application"
@echo " clear-db - Clear the database"
@echo " setup-db - Setup the database"
@echo " check - Run go vet"
@echo " test - Run tests"

.PHONY: get-front
get-front:
./script/update-frontend-dist.sh
./scripts/update-frontend-dist.sh artifacts/oj-lab-front/dist

.PHONY: install-tools
install-tools:
go install github.com/swaggo/swag/cmd/swag@latest
@# Referencing https://grpc.io/docs/protoc-installation/
@./script/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]
Expand All @@ -17,34 +28,34 @@ install-tools:
gen-proto: install-tools
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
service/proto/*.proto
src/service/proto/*.proto

.PHONY: gen-swagger
gen-swagger: install-tools
swag fmt -d application/server
swag init -d application/server,service/model -ot go -o application/server/swaggo-gen
swag fmt -d src/application/server
swag init -d src/application/server,src/service/model -ot go -o src/application/server/swaggo-gen

.PHONY: build
build: gen-proto gen-swagger
@echo "Building on $(OS)"
go mod tidy
go build -o bin/migrate_db application/migrate_db/main.go
go build -o bin/service application/server/main.go
go build -o bin/asynq_worker application/asynq_worker/main.go
go build -o bin/rpc_server application/rpc_server/main.go
go build -o bin/schedule application/schedule/main.go
go build -o artifacts/bin/migrate_db src/application/migrate_db/main.go
go build -o artifacts/bin/service src/application/server/main.go
go build -o artifacts/bin/asynq_worker src/application/asynq_worker/main.go
go build -o artifacts/bin/rpc_server src/application/rpc_server/main.go
go build -o artifacts/bin/schedule src/application/schedule/main.go

.PHONY: clear-db
clear-db:
docker-compose stop
docker-compose rm -f
docker-compose -f environment/docker-compose.yml -p oj-lab-platform stop
docker-compose -f environment/docker-compose.yml -p oj-lab-platform rm -f

.PHONY: setup-db
setup-db: clear-db build
docker-compose up -d
docker-compose -f environment/docker-compose.yml -p oj-lab-platform up -d
@echo "Wait 10 seconds for db setup"
sleep 10s
./bin/migrate_db
./artifacts/bin/migrate_db

.PHONY: check
check: gen-proto
Expand All @@ -56,19 +67,19 @@ test: gen-swagger check setup-db

.PHONY: run-task-worker
run-task-worker: build check
./bin/asynq_worker
./artifacts/bin/asynq_worker

.PHONY: run-schedule
run-schedule: build check
./bin/schedule
./artifacts/bin/schedule

.PHONY: run-server
run-server: build check
./bin/service
./artifacts/bin/service

.PHONY: run-rpc-server
run-rpc-server: build check
./bin/rpc_server
./artifacts/bin/rpc_server

.PHONY: run-background
run-background: build check
Expand All @@ -78,13 +89,6 @@ run-background: build check
run-all: build check
make -j run-server run-schedule

.PHONY: help
help:
@echo "Usage: make [target]"
@echo ""
@echo "Targets:"
@echo " build - Build the application"
@echo " clear-db - Clear the database"
@echo " setup-db - Setup the database"
@echo " check - Run go vet"
@echo " test - Run tests"
.PHONY: build-docker
build-docker:
docker build -f docker/oj-lab-platform.dockerfile -t oj-lab-platform:latest .
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Alternatively, you can use `make run-all` to run all the processes in one termin

Use `make get-front` to get the frontend dist codes.
You should also set `service.serve_front` to `true` in config file
(see [override.example.toml](config/override.example.toml) for more information)
(see [override.example.toml](configs/override.example.toml) for more information)

### WARNING

Expand Down
27 changes: 0 additions & 27 deletions application/server/handler/frontend.go

This file was deleted.

14 changes: 0 additions & 14 deletions core/agent/redis/client.go

This file was deleted.

35 changes: 15 additions & 20 deletions docker/oj-lab-platform.dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
FROM golang:latest as build

COPY application/ /usr/src/application/
COPY core/ /usr/src/core/
COPY service/ /usr/src/service/
COPY go.mod /usr/src/go.mod
COPY go.sum /usr/src/go.sum
COPY script/ /usr/src/script/
COPY Makefile /usr/src/Makefile
WORKDIR /oj-lab-platform-build

WORKDIR /usr/src
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

RUN apt update && apt install -y make
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/

RUN apt update && apt install -y make zip curl
RUN make build
RUN make get-front


FROM ubuntu:latest

COPY --from=build /usr/src/bin/service /usr/local/bin/oj-lab-service
COPY --from=build /usr/src/bin/asynq_worker /usr/local/bin/asynq_worker

RUN apt update && apt install -y make zip curl

RUN mkdir /workspace
COPY config/production.toml /workspace/config/production.toml
WORKDIR /workspace

COPY Makefile /workspace/Makefile
COPY script/ /workspace/script/
RUN make get-front
RUN apt install -y strace
COPY --from=build /oj-lab-platform-build/artifacts/bin/service /usr/local/bin/oj-lab-service
COPY --from=build /oj-lab-platform-build/artifacts/oj-lab-front /workspace/artifacts/oj-lab-front

COPY environment/configs/production.toml /workspace/environment/configs/production.toml

ENV OJ_LAB_SERVICE_ENV=production
ENV OJ_LAB_PROJECT_ROOT=workspace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ port = ":8080"
cookie.age = "24h"
mode = "debug"
swagger_on = true
serve_front = false
frontend_dist = "artifacts/oj-lab-front/dist"

[rpc-server]
port = 50051
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ level = "debug"
dsn = "user=postgres password=development host=host.docker.internal port=5432 dbname=oj_lab sslmode=disable TimeZone=Asia/Shanghai"

[redis]
host = "localhost:6379"
host = "host.docker.internal:6379"

[jwt]
secret = "example_secret"
Expand All @@ -17,7 +17,7 @@ port = ":8080"
cookie.age = "24h"
mode = "debug"
swagger_on = true
serve_front = true
frontend_dist = "artifacts/oj-lab-front/dist"

[rpc-server]
port = 50051
Expand Down
File renamed without changes.
Loading

0 comments on commit 5fdf3f6

Please sign in to comment.