refactor(backend): 매직 넘버 config 이동
- 이미지 크기/품질 설정 (800x85, 400x80) → config.image - X 기본 사용자명 'realfromis_9' → config.x.defaultUsername - Meilisearch 최소 점수 0.5 → config.meilisearch.minScore Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
46469fd324
commit
c3e504d1e3
7 changed files with 86 additions and 40 deletions
|
|
@ -10,6 +10,13 @@ export default {
|
||||||
port: parseInt(process.env.PORT) || 80,
|
port: parseInt(process.env.PORT) || 80,
|
||||||
host: '0.0.0.0',
|
host: '0.0.0.0',
|
||||||
},
|
},
|
||||||
|
image: {
|
||||||
|
medium: { width: 800, quality: 85 },
|
||||||
|
thumb: { width: 400, quality: 80 },
|
||||||
|
},
|
||||||
|
x: {
|
||||||
|
defaultUsername: 'realfromis_9',
|
||||||
|
},
|
||||||
db: {
|
db: {
|
||||||
host: process.env.DB_HOST || 'mariadb',
|
host: process.env.DB_HOST || 'mariadb',
|
||||||
port: parseInt(process.env.DB_PORT) || 3306,
|
port: parseInt(process.env.DB_PORT) || 3306,
|
||||||
|
|
@ -40,5 +47,6 @@ export default {
|
||||||
meilisearch: {
|
meilisearch: {
|
||||||
host: process.env.MEILI_HOST || 'http://fromis9-meilisearch:7700',
|
host: process.env.MEILI_HOST || 'http://fromis9-meilisearch:7700',
|
||||||
apiKey: process.env.MEILI_MASTER_KEY,
|
apiKey: process.env.MEILI_MASTER_KEY,
|
||||||
|
minScore: 0.5,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import config, { CATEGORY_IDS } from '../../config/index.js';
|
||||||
|
|
||||||
const X_CATEGORY_ID = CATEGORY_IDS.X;
|
const X_CATEGORY_ID = CATEGORY_IDS.X;
|
||||||
const NITTER_URL = config.nitter?.url || process.env.NITTER_URL || 'http://nitter:8080';
|
const NITTER_URL = config.nitter?.url || process.env.NITTER_URL || 'http://nitter:8080';
|
||||||
const DEFAULT_USERNAME = 'realfromis_9';
|
const DEFAULT_USERNAME = config.x.defaultUsername;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* X(Twitter) 관련 관리자 라우트
|
* X(Twitter) 관련 관리자 라우트
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
*/
|
*/
|
||||||
import suggestionsRoutes from './suggestions.js';
|
import suggestionsRoutes from './suggestions.js';
|
||||||
import { searchSchedules, syncAllSchedules } from '../../services/meilisearch/index.js';
|
import { searchSchedules, syncAllSchedules } from '../../services/meilisearch/index.js';
|
||||||
import { CATEGORY_IDS } from '../../config/index.js';
|
import config, { CATEGORY_IDS } from '../../config/index.js';
|
||||||
import { getMonthlySchedules, getUpcomingSchedules } from '../../services/schedule.js';
|
import { getMonthlySchedules, getUpcomingSchedules } from '../../services/schedule.js';
|
||||||
|
|
||||||
export default async function schedulesRoutes(fastify) {
|
export default async function schedulesRoutes(fastify) {
|
||||||
|
|
@ -163,7 +163,7 @@ export default async function schedulesRoutes(fastify) {
|
||||||
: `https://www.youtube.com/watch?v=${s.youtube_video_id}`;
|
: `https://www.youtube.com/watch?v=${s.youtube_video_id}`;
|
||||||
} else if (s.category_id === CATEGORY_IDS.X && s.x_post_id) {
|
} else if (s.category_id === CATEGORY_IDS.X && s.x_post_id) {
|
||||||
// X (Twitter)
|
// X (Twitter)
|
||||||
const username = 'realfromis_9';
|
const username = config.x.defaultUsername;
|
||||||
result.postId = s.x_post_id;
|
result.postId = s.x_post_id;
|
||||||
result.content = s.x_content || null;
|
result.content = s.x_content || null;
|
||||||
result.imageUrls = s.x_image_urls ? JSON.parse(s.x_image_urls) : [];
|
result.imageUrls = s.x_image_urls ? JSON.parse(s.x_image_urls) : [];
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,9 @@ const s3Client = new S3Client({
|
||||||
const BUCKET = config.s3.bucket;
|
const BUCKET = config.s3.bucket;
|
||||||
const PUBLIC_URL = config.s3.publicUrl;
|
const PUBLIC_URL = config.s3.publicUrl;
|
||||||
|
|
||||||
|
// 이미지 처리 설정
|
||||||
|
const { medium, thumb } = config.image;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 이미지를 3가지 해상도로 변환
|
* 이미지를 3가지 해상도로 변환
|
||||||
*/
|
*/
|
||||||
|
|
@ -23,12 +26,12 @@ async function processImage(buffer) {
|
||||||
const [originalBuffer, mediumBuffer, thumbBuffer] = await Promise.all([
|
const [originalBuffer, mediumBuffer, thumbBuffer] = await Promise.all([
|
||||||
sharp(buffer).webp({ lossless: true }).toBuffer(),
|
sharp(buffer).webp({ lossless: true }).toBuffer(),
|
||||||
sharp(buffer)
|
sharp(buffer)
|
||||||
.resize(800, null, { withoutEnlargement: true })
|
.resize(medium.width, null, { withoutEnlargement: true })
|
||||||
.webp({ quality: 85 })
|
.webp({ quality: medium.quality })
|
||||||
.toBuffer(),
|
.toBuffer(),
|
||||||
sharp(buffer)
|
sharp(buffer)
|
||||||
.resize(400, null, { withoutEnlargement: true })
|
.resize(thumb.width, null, { withoutEnlargement: true })
|
||||||
.webp({ quality: 80 })
|
.webp({ quality: thumb.quality })
|
||||||
.toBuffer(),
|
.toBuffer(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,11 @@
|
||||||
* - 일정 동기화
|
* - 일정 동기화
|
||||||
*/
|
*/
|
||||||
import Inko from 'inko';
|
import Inko from 'inko';
|
||||||
|
import config from '../../config/index.js';
|
||||||
|
|
||||||
const inko = new Inko();
|
const inko = new Inko();
|
||||||
const INDEX_NAME = 'schedules';
|
const INDEX_NAME = 'schedules';
|
||||||
|
const MIN_SCORE = config.meilisearch.minScore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 영문 자판으로 입력된 검색어인지 확인
|
* 영문 자판으로 입력된 검색어인지 확인
|
||||||
|
|
@ -86,9 +88,9 @@ export async function searchSchedules(meilisearch, db, query, options = {}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 유사도 0.5 미만 필터링
|
// 유사도 필터링
|
||||||
let filteredHits = Array.from(allHits.values())
|
let filteredHits = Array.from(allHits.values())
|
||||||
.filter(hit => hit._rankingScore >= 0.5);
|
.filter(hit => hit._rankingScore >= MIN_SCORE);
|
||||||
|
|
||||||
// 유사도 순 정렬
|
// 유사도 순 정렬
|
||||||
filteredHits.sort((a, b) => (b._rankingScore || 0) - (a._rankingScore || 0));
|
filteredHits.sort((a, b) => (b._rankingScore || 0) - (a._rankingScore || 0));
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
* 스케줄 서비스
|
* 스케줄 서비스
|
||||||
* 스케줄 관련 비즈니스 로직
|
* 스케줄 관련 비즈니스 로직
|
||||||
*/
|
*/
|
||||||
import { CATEGORY_IDS } from '../config/index.js';
|
import config, { CATEGORY_IDS } from '../config/index.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 월별 일정 조회 (생일 포함)
|
* 월별 일정 조회 (생일 포함)
|
||||||
|
|
@ -113,7 +113,7 @@ export async function getMonthlySchedules(db, year, month) {
|
||||||
} else if (s.category_id === CATEGORY_IDS.X && s.x_post_id) {
|
} else if (s.category_id === CATEGORY_IDS.X && s.x_post_id) {
|
||||||
schedule.source = {
|
schedule.source = {
|
||||||
name: '',
|
name: '',
|
||||||
url: `https://x.com/realfromis_9/status/${s.x_post_id}`,
|
url: `https://x.com/${config.x.defaultUsername}/status/${s.x_post_id}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,10 @@
|
||||||
|
|
||||||
백엔드 코드 품질 개선을 위한 리팩토링 계획서
|
백엔드 코드 품질 개선을 위한 리팩토링 계획서
|
||||||
|
|
||||||
## 작업 목록
|
## 완료된 작업
|
||||||
|
|
||||||
### 1단계: 설정 통합 (config 정리) ✅ 완료
|
### 1단계: 설정 통합 (config 정리) ✅ 완료
|
||||||
- [x] 카테고리 ID 상수 통합 (`CATEGORY_IDS`)
|
- [x] 카테고리 ID 상수 통합 (`CATEGORY_IDS`)
|
||||||
- [ ] 매직 넘버 설정 파일로 이동 (추후)
|
|
||||||
- [ ] JWT 기본값 제거 (추후)
|
|
||||||
|
|
||||||
**수정된 파일:**
|
**수정된 파일:**
|
||||||
- `src/config/index.js` - `CATEGORY_IDS` 상수 추가
|
- `src/config/index.js` - `CATEGORY_IDS` 상수 추가
|
||||||
|
|
@ -23,50 +21,79 @@
|
||||||
|
|
||||||
**수정된 파일:**
|
**수정된 파일:**
|
||||||
- `src/routes/albums/index.js` - GET /api/albums에서 트랙 조회 최적화
|
- `src/routes/albums/index.js` - GET /api/albums에서 트랙 조회 최적화
|
||||||
- 변경 전: 앨범마다 트랙 조회 (N+1 쿼리)
|
|
||||||
- 변경 후: 모든 트랙을 한 번에 조회 후 JavaScript에서 그룹화 (2 쿼리)
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 3단계: 서비스 레이어 분리 ✅ 완료
|
### 3단계: 서비스 레이어 분리 ✅ 완료
|
||||||
- [x] `src/services/album.js` 생성 - 앨범 관련 비즈니스 로직
|
- [x] `src/services/album.js` 생성
|
||||||
- [x] `src/services/schedule.js` 생성 - 스케줄 관련 비즈니스 로직
|
- [x] `src/services/schedule.js` 생성
|
||||||
- [x] 라우트에서 서비스 호출로 변경
|
- [x] 라우트에서 서비스 호출로 변경
|
||||||
|
|
||||||
**생성된 파일:**
|
|
||||||
- `src/services/album.js` - getAlbumDetails, getAlbumsWithTracks
|
|
||||||
- `src/services/schedule.js` - getMonthlySchedules, getUpcomingSchedules
|
|
||||||
|
|
||||||
**수정된 파일:**
|
|
||||||
- `src/routes/albums/index.js` - 서비스 import 및 사용
|
|
||||||
- `src/routes/schedules/index.js` - 서비스 import 및 기존 함수 제거 (240줄 감소)
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 4단계: 에러 처리 통일 ✅ 완료
|
### 4단계: 에러 처리 통일 ✅ 완료
|
||||||
- [x] 에러 응답 유틸리티 생성 (`src/utils/error.js`)
|
- [x] 에러 응답 유틸리티 생성 (`src/utils/error.js`)
|
||||||
- [x] `reply.status()` → `reply.code()` 통일
|
- [x] `reply.status()` → `reply.code()` 통일
|
||||||
- [ ] `console.error` → `fastify.log.error` 변경 (추후)
|
|
||||||
|
|
||||||
**생성된 파일:**
|
|
||||||
- `src/utils/error.js` - sendError, badRequest, unauthorized, notFound, conflict, serverError
|
|
||||||
|
|
||||||
**수정된 파일:**
|
|
||||||
- `src/routes/auth.js` - reply.status → reply.code
|
|
||||||
- `src/routes/stats/index.js` - reply.status → reply.code
|
|
||||||
- `src/routes/members/index.js` - reply.status → reply.code
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 5단계: 중복 코드 제거 ✅ 완료
|
### 5단계: 중복 코드 제거 ✅ 완료
|
||||||
- [x] 스케줄러 상태 업데이트 로직 통합 (handleSyncResult 함수)
|
- [x] 스케줄러 상태 업데이트 로직 통합 (handleSyncResult 함수)
|
||||||
- [x] youtube/index.js 하드코딩된 카테고리 ID → config 사용
|
- [x] youtube/index.js 하드코딩된 카테고리 ID → config 사용
|
||||||
- [ ] 멤버 이름 매핑 유틸리티 (기존 코드가 충분히 분리됨)
|
|
||||||
- [ ] 이미지 업로드 공통 함수 (기존 코드가 충분히 분리됨)
|
---
|
||||||
|
|
||||||
|
## 추가 작업 목록
|
||||||
|
|
||||||
|
### 6단계: 매직 넘버 config 이동 ✅ 완료
|
||||||
|
- [x] 이미지 크기/품질 설정 (`services/image.js`)
|
||||||
|
- [x] X 기본 사용자명 (`routes/admin/x.js`, `routes/schedules/index.js`, `services/schedule.js`)
|
||||||
|
- [x] Meilisearch 최소 점수 (`services/meilisearch/index.js`)
|
||||||
|
|
||||||
**수정된 파일:**
|
**수정된 파일:**
|
||||||
- `src/plugins/scheduler.js` - handleSyncResult 함수 추출로 중복 제거
|
- `src/config/index.js` - `image`, `x`, `meilisearch.minScore` 추가
|
||||||
- `src/services/youtube/index.js` - CATEGORY_IDS.YOUTUBE 사용
|
- `src/services/image.js` - config에서 이미지 크기/품질 참조
|
||||||
|
- `src/services/meilisearch/index.js` - config에서 minScore 참조
|
||||||
|
- `src/routes/admin/x.js` - config에서 defaultUsername 참조
|
||||||
|
- `src/routes/schedules/index.js` - config에서 defaultUsername 참조
|
||||||
|
- `src/services/schedule.js` - config에서 defaultUsername 참조
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 7단계: 순차 쿼리 → 병렬 처리
|
||||||
|
- [ ] `services/album.js` getAlbumDetails - tracks, teasers, photos 병렬 조회
|
||||||
|
- [ ] `routes/albums/photos.js` - 멤버 INSERT 배치 처리
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 8단계: meilisearch 카테고리 ID 상수화
|
||||||
|
- [ ] `services/meilisearch/index.js` - 하드코딩된 2, 3 → CATEGORY_IDS 사용
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 9단계: 응답 형식 통일
|
||||||
|
- [ ] `routes/schedules/suggestions.js` - `{success, message}` → `{error}` 형식으로 통일
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 10단계: 로거 통일
|
||||||
|
- [ ] `src/utils/logger.js` 생성
|
||||||
|
- [ ] 모든 `console.error/log` → logger 사용
|
||||||
|
|
||||||
|
**대상 파일 (30개 이상):**
|
||||||
|
- `services/image.js:61`
|
||||||
|
- `services/meilisearch/index.js:112, 188, 201, 256`
|
||||||
|
- `services/suggestions/index.js:47, 136, 174, 203, 239, 261, 296`
|
||||||
|
- `services/suggestions/morpheme.js:92, 144`
|
||||||
|
- `routes/albums/photos.js:199`
|
||||||
|
- `routes/schedules/index.js:264`
|
||||||
|
- `routes/schedules/suggestions.js:18, 190`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 11단계: 대형 핸들러 분리
|
||||||
|
- [ ] `routes/albums/photos.js` POST (153줄) → `services/album.js`로 이동
|
||||||
|
- [ ] `routes/albums/index.js` POST/PUT → 서비스 함수로 분리
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -79,11 +106,17 @@
|
||||||
| 3단계 | 서비스 레이어 분리 | ✅ 완료 |
|
| 3단계 | 서비스 레이어 분리 | ✅ 완료 |
|
||||||
| 4단계 | 에러 처리 통일 | ✅ 완료 |
|
| 4단계 | 에러 처리 통일 | ✅ 완료 |
|
||||||
| 5단계 | 중복 코드 제거 | ✅ 완료 |
|
| 5단계 | 중복 코드 제거 | ✅ 완료 |
|
||||||
|
| 6단계 | 매직 넘버 config 이동 | ✅ 완료 |
|
||||||
|
| 7단계 | 순차→병렬 쿼리 | 대기 |
|
||||||
|
| 8단계 | meilisearch 카테고리 ID | 대기 |
|
||||||
|
| 9단계 | 응답 형식 통일 | 대기 |
|
||||||
|
| 10단계 | 로거 통일 | 대기 |
|
||||||
|
| 11단계 | 대형 핸들러 분리 | 대기 |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 참고사항
|
## 참고사항
|
||||||
|
|
||||||
- 각 단계별로 테스트 후 다음 단계 진행
|
- 각 단계별로 커밋 후 다음 단계 진행
|
||||||
- 기존 API 응답 형식은 유지
|
- 기존 API 응답 형식은 유지
|
||||||
- 프론트엔드 수정 불필요하도록 진행
|
- 프론트엔드 수정 불필요하도록 진행
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue