Skip to content

Commit cb97f23

Browse files
jammy0903claude
andcommitted
refactor(backend): Migrate from Express to Fastify
- Replace Express 5.x with Fastify 5.x framework - Create Fastify plugins (auth, rateLimit, swagger) - Convert all 16 route files to Fastify plugin format - Implement auth decorators (requireAuth, requireDbUser, optionalAuth, requireAdmin) - Add SSE streaming support for AI chat routes - Remove old Express middleware folder - Add backend README documentation BREAKING CHANGE: All routes now use Fastify patterns Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 105d683 commit cb97f23

30 files changed

Lines changed: 4806 additions & 4221 deletions

File tree

packages/backend/README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# CodeInsight Backend
2+
3+
Fastify 기반 백엔드 API 서버
4+
5+
## Tech Stack
6+
7+
- **Framework**: Fastify 5.x (Express에서 마이그레이션됨, 2026-02)
8+
- **Language**: TypeScript
9+
- **Database**: PostgreSQL (Prisma ORM)
10+
- **Auth**: Firebase Authentication
11+
- **Documentation**: Swagger/OpenAPI (@fastify/swagger)
12+
13+
## Project Structure
14+
15+
```
16+
src/
17+
├── app.ts # Fastify 앱 진입점
18+
├── config/ # 환경 설정
19+
│ ├── database.ts # Prisma 클라이언트
20+
│ ├── env.ts # 환경 변수
21+
│ ├── firebase.ts # Firebase Admin SDK
22+
│ └── logger.ts # Winston 로거
23+
├── plugins/ # Fastify 플러그인
24+
│ ├── auth.ts # 인증 데코레이터
25+
│ ├── rateLimit.ts # Rate limiting
26+
│ └── swagger.ts # API 문서화
27+
├── modules/ # 도메인별 모듈
28+
│ ├── ai/ # AI 채팅 (SSE 스트리밍)
29+
│ ├── admin/ # 관리자 API
30+
│ ├── analytics/ # 학습 분석
31+
│ ├── courses/ # 코스/레슨 관리
32+
│ ├── gamification/ # 게이미피케이션
33+
│ ├── notes/ # 사용자 노트
34+
│ ├── problems/ # 문제 관리
35+
│ ├── simulators/ # 코드 시뮬레이터
36+
│ │ ├── c/
37+
│ │ ├── java/
38+
│ │ ├── javascript/
39+
│ │ └── python/
40+
│ ├── standalone-quizzes/
41+
│ ├── submissions/ # 제출 기록
42+
│ ├── subscription/ # 구독 관리
43+
│ └── users/ # 사용자 관리
44+
├── services/ # 공유 서비스
45+
└── utils/ # 유틸리티
46+
```
47+
48+
## Authentication
49+
50+
Fastify 데코레이터를 통한 인증:
51+
52+
```typescript
53+
// 토큰만 검증 (빠름)
54+
fastify.get('/route', { preHandler: [fastify.requireAuth] }, handler);
55+
56+
// 토큰 + DB 사용자 조회 (권한 확인 가능)
57+
fastify.get('/route', { preHandler: [fastify.requireDbUser] }, handler);
58+
59+
// 선택적 인증
60+
fastify.get('/route', { preHandler: [fastify.optionalAuth] }, handler);
61+
62+
// Admin 전용
63+
fastify.get('/admin/route', { preHandler: [fastify.requireAdmin] }, handler);
64+
```
65+
66+
## Rate Limiting
67+
68+
프리셋 기반 Rate Limit:
69+
70+
| 프리셋 | 제한 | 용도 |
71+
|--------|------|------|
72+
| `standard` | 100/분 | 일반 API |
73+
| `auth` | 10/분 | 인증 엔드포인트 |
74+
| `ai` | 50/분 | AI 요청 |
75+
| `execute` | 100/분 | 코드 실행 |
76+
77+
## Development
78+
79+
```bash
80+
# 개발 서버 시작
81+
pnpm dev
82+
83+
# 빌드
84+
pnpm build
85+
86+
# 프로덕션 실행
87+
pnpm start
88+
```
89+
90+
## API Documentation
91+
92+
개발 서버 실행 후 `/api-docs`에서 Swagger UI 확인 가능
93+
94+
## Environment Variables
95+
96+
```env
97+
# Database
98+
DATABASE_URL=postgresql://...
99+
100+
# Firebase
101+
FIREBASE_PROJECT_ID=...
102+
FIREBASE_CLIENT_EMAIL=...
103+
FIREBASE_PRIVATE_KEY=...
104+
105+
# Admin
106+
ADMIN_FIREBASE_UID=...
107+
108+
# AI Provider
109+
DEEPSEEK_API_KEY=...
110+
OLLAMA_API_URL=http://localhost:11434
111+
```

packages/backend/package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
"@babel/traverse": "^7.24.8",
3030
"@babel/types": "^7.28.6",
3131
"@codeinsight/shared": "workspace:*",
32+
"@fastify/cors": "^11.2.0",
33+
"@fastify/rate-limit": "^10.3.0",
34+
"@fastify/swagger": "^9.6.1",
35+
"@fastify/swagger-ui": "^5.2.5",
3236
"@google/generative-ai": "^0.24.1",
3337
"@libsql/client": "^0.15.15",
3438
"@notionhq/client": "^5.6.0",
@@ -43,13 +47,14 @@
4347
"cors": "^2.8.5",
4448
"dotenv": "^17.2.3",
4549
"express": "^5.2.1",
50+
"fastify": "^5.7.4",
51+
"fastify-plugin": "^5.1.0",
4652
"firebase-admin": "^13.6.0",
4753
"pg": "^8.16.3",
48-
"swagger-jsdoc": "^6.2.8",
49-
"swagger-ui-express": "^5.0.1",
5054
"winston": "^3.19.0",
5155
"winston-daily-rotate-file": "^5.0.0",
52-
"zod": "^4.2.1"
56+
"zod": "^4.2.1",
57+
"zod-to-json-schema": "^3.25.1"
5358
},
5459
"devDependencies": {
5560
"@types/babel__traverse": "^7.20.6",

0 commit comments

Comments
 (0)