Skip to content

Commit e9712d8

Browse files
Merge pull request #286 from boostcampwm-2024/feature-be-#283
로컬 개발환경 재구성 및 https & wss 적용
2 parents cfcb162 + 6333eb1 commit e9712d8

File tree

11 files changed

+366
-24
lines changed

11 files changed

+366
-24
lines changed

.dockerignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# compiled output
3+
*/dist
4+
*/node_modules
5+
dist
6+
node_modules
7+
dist-ssr
8+
9+
# Logs
10+
logs
11+
*.log
12+
npm-debug.log*
13+
pnpm-debug.log*
14+
yarn-debug.log*
15+
yarn-error.log*
16+
lerna-debug.log*
17+
18+
# OS
19+
.DS_Store
20+
21+
# Tests
22+
/coverage
23+
/.nyc_output
24+
25+
# IDEs and editors
26+
/.idea
27+
.idea
28+
.project
29+
.classpath
30+
.c9/
31+
*.launch
32+
.settings/
33+
*.sublime-workspace
34+
*.suo
35+
*.ntvs*
36+
*.njsproj
37+
*.sln
38+
*.sw?
39+
40+
# IDE - VSCode
41+
.vscode/*
42+
!.vscode/settings.json
43+
!.vscode/tasks.json
44+
!.vscode/launch.json
45+
!.vscode/extensions.json
46+
db.sqlite

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
.env*
44
*.local
55
.turbo
6+
.crt
7+
.key
8+
!Dockerfile.local
69

710
# compiled output
811
*/dist

apps/backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"passport-kakao": "^1.0.1",
4343
"passport-naver": "^1.0.6",
4444
"path": "^0.12.7",
45+
"pg": "^8.13.1",
4546
"reflect-metadata": "^0.1.13",
4647
"rxjs": "^7.8.1",
4748
"socket.io": "^4.8.1",

apps/backend/src/app.module.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ import { UserModule } from './user/user.module';
2424
}),
2525
ConfigModule.forRoot({
2626
isGlobal: true,
27-
envFilePath: path.join(__dirname, '..', '.env'), // * nest 디렉터리 기준
27+
envFilePath: path.join(__dirname, '..', '..', '..', '.env'), // * nest 디렉터리 기준
2828
}),
2929
TypeOrmModule.forRootAsync({
3030
imports: [ConfigModule],
3131
inject: [ConfigService],
3232
useFactory: (configService: ConfigService) => ({
33-
type: 'sqlite',
33+
type: 'postgres',
34+
host: configService.get('DB_HOST'),
35+
port: configService.get('DB_PORT'),
36+
username: configService.get('DB_USER'),
37+
password: configService.get('DB_PASSWORD'),
3438
database: configService.get('DB_NAME'),
3539
entities: [Node, Page, Edge, User],
3640
logging: true,

compose.local.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
version: "3.8"
2+
3+
services:
4+
postgres:
5+
image: postgres:16-alpine
6+
environment:
7+
POSTGRES_USER: ${DB_USER}
8+
POSTGRES_PASSWORD: ${DB_PASSWORD}
9+
POSTGRES_DB: ${DB_NAME}
10+
volumes:
11+
- postgres_data:/var/lib/postgresql/data
12+
networks:
13+
- backend
14+
healthcheck:
15+
test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}"]
16+
interval: 10s
17+
timeout: 5s
18+
retries: 5
19+
20+
backend:
21+
build:
22+
context: .
23+
dockerfile: ./services/backend/Dockerfile.local
24+
image: backend:latest
25+
env_file:
26+
- .env
27+
depends_on:
28+
postgres:
29+
condition: service_healthy
30+
networks:
31+
- backend
32+
- frontend
33+
34+
nginx:
35+
build:
36+
context: .
37+
dockerfile: ./services/nginx/Dockerfile.local
38+
ports:
39+
- "80:80"
40+
- "443:443"
41+
depends_on:
42+
- backend
43+
networks:
44+
- frontend
45+
volumes:
46+
- type: bind
47+
source: ./services/nginx/ssl
48+
target: /etc/nginx/ssl
49+
bind:
50+
create_host_path: true
51+
propagation: rprivate
52+
53+
networks:
54+
backend:
55+
internal: true
56+
frontend:
57+
58+
volumes:
59+
postgres_data:

package.json

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
{
2-
"name": "octodocs",
3-
"version": "1.0.0",
4-
"main": "index.js",
5-
"repository": "https://github.com/boostcampwm-2024/web15-OctoDocs.git",
6-
"author": "ez <[email protected]>",
7-
"license": "MIT",
8-
"scripts": {
9-
"dev": "turbo run dev",
10-
"build": "turbo run build",
11-
"start": "node apps/backend/dist/main.js",
12-
"lint": "turbo run lint",
13-
"test": "turbo run test"
14-
},
15-
"dependencies": {
16-
"turbo": "^2.3.0"
17-
},
18-
"private": true,
19-
"workspaces": [
20-
"apps/*"
21-
],
22-
"packageManager": "[email protected]"
23-
}
2+
"name": "octodocs",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"repository": "https://github.com/boostcampwm-2024/web15-OctoDocs.git",
6+
"author": "ez <[email protected]>",
7+
"license": "MIT",
8+
"scripts": {
9+
"dev": "turbo run dev",
10+
"build": "turbo run build",
11+
"start": "node apps/backend/dist/main.js",
12+
"lint": "turbo run lint",
13+
"test": "turbo run test",
14+
"docker:dev": "docker compose -f compose.local.yml up",
15+
"docker:dev:down": "docker compose -f compose.local.yml down",
16+
"docker:dev:clean": "docker compose -v -f compose.local.yml down",
17+
"docker:dev:fclean": "docker compose -v -f compose.local.yml down --rmi all",
18+
"ssl:generate": "cd services/nginx/ssl && bash ./generate-cert.sh"
19+
},
20+
"dependencies": {
21+
"turbo": "^2.3.0"
22+
},
23+
"private": true,
24+
"workspaces": [
25+
"apps/*"
26+
],
27+
"packageManager": "[email protected]"
28+
}

services/backend/Dockerfile.local

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
FROM node:20-alpine AS builder
2+
3+
WORKDIR /
4+
5+
# 모노레포 관련 파일 복사
6+
COPY package.json yarn.lock ./
7+
COPY turbo.json ./
8+
COPY apps/backend/package.json ./apps/backend/
9+
COPY apps/frontend/package.json ./apps/frontend/
10+
11+
# 의존성 설치
12+
RUN yarn install --frozen-lockfile
13+
14+
# 소스 코드 복사
15+
COPY apps/backend/ ./apps/backend/
16+
COPY apps/frontend/ ./apps/frontend/
17+
18+
# 프론트엔드와 백엔드 모두 빌드
19+
RUN yarn build
20+
21+
FROM node:20-alpine AS runner
22+
23+
WORKDIR /app
24+
25+
# 프로덕션 의존성만 설치하기 위한 파일들 복사
26+
COPY --from=builder package.json yarn.lock ./
27+
COPY --from=builder apps/backend/package.json ./apps/backend/
28+
29+
# 프로덕션 의존성만 설치
30+
RUN yarn install --production --frozen-lockfile
31+
32+
# 빌드된 결과물 복사
33+
COPY --from=builder apps/backend/dist ./apps/backend/dist
34+
COPY --from=builder apps/frontend/dist ./public/
35+
36+
EXPOSE 3000 1234
37+
38+
CMD ["yarn", "start"]

services/nginx/Dockerfile.local

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM backend:latest AS backend_build
2+
3+
FROM nginx:alpine
4+
5+
# 필요한 설정 파일 복사
6+
COPY services/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf
7+
8+
# 빌드된 프론트엔드 파일 복사
9+
COPY --from=backend_build /app/public /usr/share/nginx/html
10+
11+
# SSL 설정 및 권한 조정
12+
RUN mkdir -p /etc/nginx/ssl && \
13+
chown -R nginx:nginx /etc/nginx/ssl && \
14+
chmod 700 /etc/nginx/ssl

services/nginx/conf.d/default.conf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
server {
2+
listen 80;
3+
server_name localhost octodocs.local;
4+
return 301 https://$host$request_uri;
5+
}
6+
7+
server {
8+
listen 443 ssl;
9+
server_name localhost octodocs.local;
10+
11+
ssl_certificate /etc/nginx/ssl/localhost.crt;
12+
ssl_certificate_key /etc/nginx/ssl/localhost.key;
13+
14+
# Frontend static files
15+
location / {
16+
root /usr/share/nginx/html;
17+
try_files $uri $uri/ /index.html;
18+
add_header Cache-Control "no-cache";
19+
}
20+
21+
# Backend API
22+
location /api {
23+
proxy_pass http://backend:3000;
24+
proxy_http_version 1.1;
25+
proxy_set_header Upgrade $http_upgrade;
26+
proxy_set_header Connection 'upgrade';
27+
proxy_set_header Host $host;
28+
proxy_cache_bypass $http_upgrade;
29+
}
30+
31+
# WebSocket
32+
location /socket.io {
33+
proxy_pass http://backend:1234;
34+
proxy_http_version 1.1;
35+
proxy_set_header Upgrade $http_upgrade;
36+
proxy_set_header Connection "Upgrade";
37+
proxy_set_header Host $host;
38+
}
39+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
# 스크립트가 위치한 디렉토리로 이동
4+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
5+
cd "$SCRIPT_DIR"
6+
7+
# OpenSSL 설정 파일 생성
8+
cat > openssl.conf << EOF
9+
[req]
10+
default_bits = 2048
11+
default_keyfile = localhost.key
12+
distinguished_name = req_distinguished_name
13+
req_extensions = req_ext
14+
x509_extensions = v3_ca
15+
prompt = no
16+
17+
[req_distinguished_name]
18+
C = KR
19+
ST = Seoul
20+
L = Seoul
21+
O = OctoDocs
22+
OU = Development
23+
CN = localhost
24+
25+
[req_ext]
26+
subjectAltName = @alt_names
27+
28+
[v3_ca]
29+
subjectAltName = @alt_names
30+
31+
[alt_names]
32+
DNS.1 = localhost
33+
DNS.2 = *.localhost
34+
DNS.3 = octodocs.local
35+
DNS.4 = *.octodocs.local
36+
IP.1 = 127.0.0.1
37+
EOF
38+
39+
# 인증서 생성
40+
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.key -out localhost.crt -config openssl.conf
41+
42+
chmod 644 localhost.crt
43+
chmod 644 localhost.key
44+
45+
# 설정 파일 정리
46+
rm openssl.conf
47+
48+
echo "SSL certificate generated successfully!"
49+
echo "Certificate: $SCRIPT_DIR/localhost.crt"
50+
echo "Private key: $SCRIPT_DIR/localhost.key"

0 commit comments

Comments
 (0)