- new Error() → cb(null, false): 500 에러 대신 CORS 헤더 미포함으로 거부 - *.technicalkorea.net 와일드카드 추가: 서브도메인 간 통신 보장 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
67 lines
2.0 KiB
JavaScript
67 lines
2.0 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const vacationRoutes = require('./routes/vacationRoutes');
|
|
const companyHolidayRoutes = require('./routes/companyHolidayRoutes');
|
|
const vacationDashboardRoutes = require('./routes/vacationDashboardRoutes');
|
|
const vacationRequestModel = require('./models/vacationRequestModel');
|
|
const { requireAuth } = require('./middleware/auth');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
const allowedOrigins = [
|
|
'https://tkfb.technicalkorea.net',
|
|
'https://tkreport.technicalkorea.net',
|
|
'https://tkqc.technicalkorea.net',
|
|
'https://tkuser.technicalkorea.net',
|
|
'https://tkpurchase.technicalkorea.net',
|
|
'https://tksafety.technicalkorea.net',
|
|
'https://tksupport.technicalkorea.net',
|
|
];
|
|
if (process.env.NODE_ENV === 'development') {
|
|
allowedOrigins.push('http://localhost:30680');
|
|
}
|
|
app.use(cors({
|
|
origin: function(origin, cb) {
|
|
if (!origin || allowedOrigins.includes(origin) || /^https?:\/\/[a-z0-9-]+\.technicalkorea\.net$/.test(origin) || /^http:\/\/192\.168\.\d+\.\d+(:\d+)?$/.test(origin)) return cb(null, true);
|
|
cb(null, false);
|
|
},
|
|
credentials: true
|
|
}));
|
|
app.use(express.json());
|
|
|
|
// Health check
|
|
app.get('/health', (req, res) => {
|
|
res.json({ status: 'ok', service: 'tksupport-api', timestamp: new Date().toISOString() });
|
|
});
|
|
|
|
// Routes
|
|
app.use('/api/vacation/company', companyHolidayRoutes);
|
|
app.use('/api/vacation/dashboard', vacationDashboardRoutes);
|
|
app.use('/api/vacation', vacationRoutes);
|
|
|
|
// 404
|
|
app.use((req, res) => {
|
|
res.status(404).json({ success: false, error: 'Not Found' });
|
|
});
|
|
|
|
// Error handler
|
|
app.use((err, req, res, next) => {
|
|
console.error('tksupport-api Error:', err.message);
|
|
res.status(err.status || 500).json({
|
|
success: false,
|
|
error: '서버 오류가 발생했습니다'
|
|
});
|
|
});
|
|
|
|
app.listen(PORT, async () => {
|
|
console.log(`tksupport-api running on port ${PORT}`);
|
|
try {
|
|
await vacationRequestModel.runMigration();
|
|
} catch (err) {
|
|
console.error('Migration error:', err.message);
|
|
}
|
|
});
|
|
|
|
module.exports = app;
|