CORS 에러 해결 및 Event API Mock 구현

- next.config.js: Event API 프록시 설정 추가 (8080 포트)
- eventApi.ts: 개발 환경에서 상대 경로 사용하도록 수정
- Mock API 추가: /api/v1/events/objectives (백엔드 준비 전 임시)
This commit is contained in:
merrycoral 2025-10-29 17:49:50 +09:00
parent f414e1e1dd
commit ddc7bc143f
3 changed files with 78 additions and 1 deletions

View File

@ -19,6 +19,11 @@ const nextConfig = {
source: '/api/proxy/:path*', source: '/api/proxy/:path*',
destination: 'http://localhost:8084/api/:path*', destination: 'http://localhost:8084/api/:path*',
}, },
// Event Service API Proxy (8080 포트)
{
source: '/api/v1/events/:path*',
destination: 'http://localhost:8080/api/v1/events/:path*',
},
] ]
}, },
} }

View File

@ -0,0 +1,66 @@
import { NextRequest, NextResponse } from 'next/server';
/**
* Mock API: 이벤트 (Step 1)
* API가 Mock API
*
* POST /api/v1/events/objectives
*/
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { objective } = body;
// 백엔드 API 호출 시도
const backendUrl = 'http://localhost:8080/api/v1/events/objectives';
try {
const backendResponse = await fetch(backendUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': request.headers.get('Authorization') || '',
},
body: JSON.stringify(body),
});
// 백엔드가 정상 응답하면 그대로 반환
if (backendResponse.ok) {
const data = await backendResponse.json();
return NextResponse.json(data, { status: backendResponse.status });
}
} catch (backendError) {
console.warn('⚠️ 백엔드 API 호출 실패, Mock 데이터 반환:', backendError);
}
// 백엔드 실패 시 Mock 데이터 반환
const mockEventId = `evt_${Date.now()}_${Math.random().toString(36).substring(7)}`;
const mockResponse = {
success: true,
data: {
eventId: mockEventId,
objective,
status: 'DRAFT',
createdAt: new Date().toISOString(),
},
message: '이벤트가 생성되었습니다 (Mock)',
};
console.log('🎭 Mock API Response:', mockResponse);
return NextResponse.json(mockResponse, { status: 201 });
} catch (error) {
console.error('❌ Mock API Error:', error);
return NextResponse.json(
{
success: false,
errorCode: 'MOCK_ERROR',
message: 'Mock API 오류가 발생했습니다',
details: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
);
}
}

View File

@ -29,11 +29,17 @@ const EVENT_HOST = process.env.NEXT_PUBLIC_EVENT_HOST || 'http://localhost:8080'
/** /**
* Event Service용 API * Event Service용 API
* Event Service는 (8080) * Event Service는 (8080)
*
* 환경: Next.js rewrites (CORS )
* 환경: 환경
*/ */
import axios from 'axios'; import axios from 'axios';
const isProduction = process.env.NODE_ENV === 'production';
const API_BASE_URL = isProduction ? EVENT_HOST : ''; // 개발 환경에서는 상대 경로 사용
const eventApiClient = axios.create({ const eventApiClient = axios.create({
baseURL: EVENT_HOST, baseURL: API_BASE_URL,
timeout: 30000, timeout: 30000,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',