Files
tk-factory-services/system2-report/api/index.js
Hyungi Ahn 550633b89d feat: 3-System 분리 프로젝트 초기 코드 작성
TK-FB(공장관리+신고)와 M-Project(부적합관리)를 3개 독립 시스템으로
분리하기 위한 전체 코드 구조 작성.
- SSO 인증 서비스 (bcrypt + pbkdf2 이중 해시 지원)
- System 1: 공장관리 (TK-FB 기반, 신고 코드 제거)
- System 2: 신고 (TK-FB에서 workIssue 코드 추출)
- System 3: 부적합관리 (M-Project 기반)
- Gateway 포털 (path-based 라우팅)
- 통합 docker-compose.yml 및 배포 스크립트

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-09 14:40:11 +09:00

100 lines
2.7 KiB
JavaScript

/**
* System 2 - 신고 시스템 API Server
*
* TK-FB-Project에서 추출한 workIssue(신고) 전용 서비스
*/
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const path = require('path');
const logger = require('./utils/logger');
const { AuthenticationError, ForbiddenError } = require('./utils/errors');
const app = express();
const PORT = process.env.PORT || 3005;
app.set('trust proxy', 1);
// CORS
app.use(cors({
origin: true,
credentials: true
}));
// Body parser
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true }));
// Static files (uploads)
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Rate limiter
const apiLimiter = rateLimit({
windowMs: 1 * 60 * 1000,
max: 500,
message: 'API 요청 한도를 초과했습니다.',
standardHeaders: true,
legacyHeaders: false
});
app.use('/api/', apiLimiter);
// Health check
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', service: 'system2-report', timestamp: new Date().toISOString() });
});
// JWT Auth middleware (SSO 공유 시크릿)
const jwt = require('jsonwebtoken');
const requireAuth = (req, res, next) => {
try {
const authHeader = req.headers['authorization'];
if (!authHeader) {
throw new AuthenticationError('Authorization 헤더가 필요합니다');
}
const token = authHeader.split(' ')[1];
if (!token) {
throw new AuthenticationError('Bearer 토큰이 필요합니다');
}
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = decoded;
next();
} catch (err) {
if (err.name === 'JsonWebTokenError' || err.name === 'TokenExpiredError') {
return res.status(401).json({ success: false, error: '유효하지 않은 토큰입니다' });
}
if (err instanceof AuthenticationError) {
return res.status(401).json({ success: false, error: err.message });
}
next(err);
}
};
// Routes
const workIssueRoutes = require('./routes/workIssueRoutes');
// 인증이 필요한 API
app.use('/api/work-issues', requireAuth, workIssueRoutes);
// Error handler
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
logger.error('API Error:', { error: err.message, path: req.path });
res.status(statusCode).json({
success: false,
error: err.message || 'Internal Server Error'
});
});
// 404
app.use((req, res) => {
res.status(404).json({ success: false, error: 'Not Found', path: req.originalUrl });
});
app.listen(PORT, () => {
console.log(`System 2 (신고) API running on port ${PORT}`);
});
module.exports = app;