sometrend-mcp-server
Version:
TM2 기반의 썸트렌드(sometrend) MCP 서버 - 트렌드 분석 및 데이터 처리
77 lines (70 loc) • 1.72 kB
text/typescript
/**
* Rate Limiting 미들웨어
* 프로덕션 환경에서 API 남용 방지
*/
import rateLimit from 'express-rate-limit';
import { Request, Response } from 'express';
/**
* 기본 Rate Limit 설정
*/
export const basicRateLimit = rateLimit({
windowMs: 15 * 60 * 1000, // 15분
max: 100, // 최대 100 요청
message: {
error: 'Too many requests from this IP',
retryAfter: 15 * 60,
limit: 100,
windowMs: 15 * 60 * 1000
},
standardHeaders: true,
legacyHeaders: false,
skip: (req: Request) => {
// 헬스체크는 rate limit 제외
return req.path === '/health' || req.path === '/info';
}
});
/**
* SSE 연결용 엄격한 Rate Limit
*/
export const sseRateLimit = rateLimit({
windowMs: 5 * 60 * 1000, // 5분
max: 10, // 최대 10개 SSE 연결
message: {
error: 'Too many SSE connections from this IP',
retryAfter: 5 * 60,
limit: 10,
windowMs: 5 * 60 * 1000
},
standardHeaders: true,
legacyHeaders: false
});
/**
* MCP 메시지용 Rate Limit
*/
export const messageRateLimit = rateLimit({
windowMs: 1 * 60 * 1000, // 1분
max: 30, // 최대 30개 메시지
message: {
error: 'Too many MCP messages from this IP',
retryAfter: 60,
limit: 30,
windowMs: 60 * 1000
},
standardHeaders: true,
legacyHeaders: false
});
/**
* 프로덕션 환경용 Rate Limit 팩토리
*/
export function createProductionRateLimit() {
const isProduction = process.env.NODE_ENV === 'production';
if (!isProduction) {
// 개발 환경에서는 느슨한 제한
return rateLimit({
windowMs: 1 * 60 * 1000,
max: 1000,
skip: () => true // 개발시에는 스킵
});
}
return basicRateLimit;
}