Skip to content
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
17 changes: 17 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
**/node_modules
out

# Storage
**/storage/

**/.git

**/generated

node_modules

**/dist

**/.nuxt

**/.output
39 changes: 39 additions & 0 deletions .github/workflows/build-frontend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Build the Web Frontend Service

on:
push:
branches:
- main
paths:
- 'apps/frontend/**'
- 'packages/**'
- '.github/workflows/**'

pull_request:
branches:
- main
- dev
paths:
- 'apps/frontend/**'
- 'packages/**'
- '.github/workflows/**'

jobs:
setup-and-build:
runs-on: ubuntu-latest
steps:
- name: check out the code
uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9.0.0
run_install: false
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install Package & Setup
run: pnpm install --frozen-lockfile --filter=frontend... -w
- name: Try to build
run: pnpm dlx turbo build --filter=frontend...
39 changes: 39 additions & 0 deletions .github/workflows/build-heartbeat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Build the Heartbeat Service

on:
push:
branches:
- main
paths:
- 'apps/heartbeat/**'
- 'packages/domain/**'
- '.github/workflows/**'

pull_request:
branches:
- main
- dev
paths:
- 'apps/heartbeat/**'
- 'packages/domain/**'
- '.github/workflows/**'

jobs:
setup-and-build:
runs-on: ubuntu-latest
steps:
- name: check out the code
uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9.0.0
run_install: false
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install Package & Setup
run: pnpm install --frozen-lockfile --filter=heartbeat... -w
- name: Try to build
run: pnpm dlx turbo build --filter=heartbeat...
43 changes: 43 additions & 0 deletions .github/workflows/build_test-backend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Build and Testing Backend Service

on:
push:
branches:
- main
paths:
- 'apps/backend/**'
- 'packages/**'
- '.github/workflows/**'

pull_request:
branches:
- main
- dev
paths:
- 'apps/backend/**'
- 'packages/**'
- '.github/workflows/**'

jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Set up Docker
uses: docker/setup-docker-action@v4
- name: check out the code
uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9.0.0
run_install: false
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install Package & Setup
run: pnpm install --frozen-lockfile --filter=backend... -w
- name: Try to build
run: pnpm dlx turbo build --filter=backend...
- name: RUN E2E Test
run: pnpm test:e2e
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
link-workspace-packages=true
8 changes: 8 additions & 0 deletions apps/backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**/node_modules
out

**/.git

**/generated

**/dist
4 changes: 2 additions & 2 deletions apps/backend/.env.development
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
NODE_ENV=development
NODE_PORT=3001
NODE_PORT=8000
NODE_HOST=0.0.0.0
DB_TYPE=postgres
DB_HOST=db
DB_HOST=127.0.0.1
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
Expand Down
53 changes: 53 additions & 0 deletions apps/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#syntax=docker/dockerfile:1.7-labs
# 支援exclude文法

###############
# BUILD STAGE # This stage is used to build application before production (Typescript => Javascript)
###############
FROM node:20-alpine AS base
# Create and move into /app folder
WORKDIR /app
# Activate corepack to use pnpm
RUN npm install -g corepack@latest turbo
RUN corepack use pnpm@9.0.0

FROM base AS builder
WORKDIR /app
COPY . .
RUN turbo prune backend --docker

# 安裝階段:安裝依賴並構建項目
FROM base AS installer
WORKDIR /app
COPY --from=builder /app/out/json/ .
COPY pnpm-*.yaml ./
COPY .npmrc ./
# 安裝依賴,因為還包含了devDependencies,所以會比較大
RUN pnpm i --frozen-lockfile
COPY --from=builder /app/out/full/ .
RUN turbo build --filter=backend...

# 運行階段:設定最終的運行環境
FROM base AS runner
WORKDIR /app
COPY --exclude=**/node_modules --from=installer /app .
RUN pnpm i --frozen-lockfile --prod
## 1.暫時不知道為啥這裏不生效,換上麵方法
# COPY --from=installer /app .
# RUN pnpm prune --prod
## 2.方法2
# RUN rm -rf ./node_modules && rm -rf ./**/*/node_modules
# RUN pnpm i --prod

# 拷貝部署
FROM base AS deploy
LABEL org.opencontainers.image.source="https://github.com/NTD-Driven-Development/lobby"
WORKDIR /app
# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 fastifyapp
USER fastifyapp
COPY --from=runner --chown=fastifyapp:nodejs /app .

# 設定容器啓動命令
CMD ["pnpm", "-F", "backend", "start"]
24 changes: 16 additions & 8 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "lobby-backend",
"name": "backend",
"version": "0.0.0",
"description": "",
"main": "src/index.ts",
"scripts": {
"rebuild": "rm -rf ./dist && npm run build",
"build": "cross-env NODE_ENV=production esbuild --bundle --platform=node --target=node18 --outdir=dist src/index.ts",
"start": "cross-env NODE_ENV=production node dist/index",
"build": "cross-env NODE_ENV=production esbuild --bundle --platform=node --target=node20 --outdir=dist src/index.ts",
"start": "node dist/index",
"dev": "cross-env NODE_ENV=development ts-node-dev -r tsconfig-paths/register --project . --respawn src/index.ts",
"format": "prettier --no-error-on-unmatched-pattern --write \"src/**/*.ts\" \"test/**/*.ts\"",
"lint": "eslint --cache --cache-location ./node_modules/.cache/eslint \"{src,apps,libs,test}/**/*.ts\"",
Expand All @@ -25,9 +25,14 @@
"ts-jest": {
"tsconfig": "./tsconfig.json"
},
"SOCKET_URL": "http://localhost:8002"
"SOCKET_URL": "http://localhost:8002",
"CLOSE_TEST_DB": false
},
"globalSetup": "./test/setupGlobal.ts",
"globalSetup": "./test/setup.ts",
"globalTeardown": "./test/teardown.ts",
"setupFilesAfterEnv": [
"jest-extended"
],
"preset": "ts-jest",
"moduleNameMapper": {
"~/routes": "<rootDir>/src/routes",
Expand Down Expand Up @@ -57,11 +62,12 @@
"devDependencies": {
"@eslint/js": "^9.1.1",
"@jest/globals": "^29.7.0",
"@jest/types": "^29.6.3",
"@swc/core": "^1.5.5",
"@swc/jest": "^0.2.36",
"@testcontainers/postgresql": "^10.22.0",
"@tsconfig/node-lts": "^20.1.3",
"@types/jest": "^29.5.0",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"cross-env": "^7.0.3",
Expand All @@ -75,15 +81,17 @@
"jest-extended": "^3.2.4",
"prettier": "^3.2.5",
"socket.io-client": "^4.7.5",
"testcontainers": "^10.22.0",
"ts-jest": "^29.1.0",
"ts-node-dev": "^2.0.0",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.4.5",
"typescript-eslint": "^7.7.1"
},
"dependencies": {
"@packages/domain": "file:../../packages/domain",
"@packages/socket": "file:../../packages/socket",
"@packages/domain": "workspace:*",
"@packages/socket": "workspace:*",
"@types/uuid": "^9.0.8",
"axios": "^1.7.2",
"dotenv": "^16.4.5",
"fastify": "^4.26.2",
Expand Down
10 changes: 8 additions & 2 deletions apps/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import { container } from 'tsyringe'
import { auth0Middleware } from '~/middlewares'
import { AppDataSource } from './data/data-source'
import { GetNewStatusHandler } from './middlewares/get-new-status'
;['SIGINT', 'SIGTERM', 'SIGQUIT'].forEach((signal) =>
process.on(signal, async () => {
/** do your logic */
console.log('Server shutting down...')
await AppDataSource.destroy()
process.exit()
}),
)
;(async () => {
try {
// import { UserRoutes, RoomRoutes, GameRoutes } from '~/routes'
Expand All @@ -27,8 +35,6 @@ import { GetNewStatusHandler } from './middlewares/get-new-status'
app.register(socketIO, { cors: { origin: '*' } })
// prefix api
app.register(RoomRoutes, { prefix: '/api/rooms' })
// app.register(GameRoutes, { prefix: '/api/games' })
// app.register(UserRoutes, { prefix: '/api/users' })
app.ready(async (err) => {
if (err) throw err
container.registerInstance('ServerSocket', app.io)
Expand Down
1 change: 1 addition & 0 deletions apps/backend/test/environment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'jest-extended'
37 changes: 36 additions & 1 deletion apps/backend/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
import 'jest-extended/all'
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-var */
/* eslint-disable @typescript-eslint/no-var-requires */
import 'tsconfig-paths/register'
import { Config } from '@jest/types'
import { PostgreSqlContainer, StartedPostgreSqlContainer } from '@testcontainers/postgresql'

declare global {
var SOCKET_URL: string
var pgContainer: StartedPostgreSqlContainer
}

require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` })

export default async function (globalConfig: Config.GlobalConfig, projectConfig: Config.ProjectConfig) {
if (globalConfig.testPathPattern.includes('/unit')) {
console.log('is unit test, skip setup server')
return
}
// Connect our container
console.log('starting test db...')
const pgsqlContainer = await new PostgreSqlContainer('postgres:16-alpine3.19')
.withUsername(process.env.DB_USER)
.withPassword(process.env.DB_PASSWORD)
.withDatabase(process.env.DB_NAME)
.withExposedPorts({
container: 5432,
host: Number(process.env.DB_PORT),
})
.withReuse()
.start()
globalThis.pgContainer = pgsqlContainer
console.log('test db started...')
console.log('setup test server')
await require('../src/index')
}
18 changes: 0 additions & 18 deletions apps/backend/test/setupGlobal.ts

This file was deleted.

13 changes: 13 additions & 0 deletions apps/backend/test/teardown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Config } from '@jest/types'

export default async function (globalConfig: Config.GlobalConfig, projectConfig: Config.ProjectConfig) {
if (projectConfig.globals.CLOSE_TEST_DB) {
await closeDb()
}
}

async function closeDb() {
console.log('stopping db...')
await globalThis.pgContainer.stop()
console.log('db stopped...')
}
1 change: 0 additions & 1 deletion apps/backend/test/test.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "@tsconfig/node-lts",
"include": ["src", "test"],
"files": ["test/test.d.ts"],
"compilerOptions": {
"declaration": true,
"removeComments": true,
Expand Down
Loading