import cors from 'cors'; import express from 'express'; import { fileURLToPath } from 'url'; import path from 'path'; import compilerRouter from './routes/compiler.js'; const app = express(); const PORT = Number(process.env.PORT || 5001); const allowedOrigins = new Set(['http://localhost:5173', 'http://127.0.0.1:5173']); app.use( cors({ origin(origin, callback) { if (!origin || allowedOrigins.has(origin)) { callback(null, true); return; } callback(null, false); }, methods: ['GET', 'POST', 'OPTIONS'], allowedHeaders: ['Content-Type'], }), ); app.use(express.json({ limit: '300kb' })); app.get('/health', (_req, res) => { res.json({ status: 'ok', service: 'ryp-compiler' }); }); app.use('/api/compile', compilerRouter); app.use((req, res) => { res.status(404).json({ output: '', error: `Route not found: ${req.method} ${req.originalUrl}`, executionTime: 0, status: 'not_found', }); }); app.use((err, _req, res, _next) => { res.status(500).json({ output: '', error: err?.message || 'Unexpected compiler server error.', executionTime: 0, status: 'server_error', }); }); const currentFile = fileURLToPath(import.meta.url); const executedFile = process.argv[1] ? path.resolve(process.argv[1]) : ''; if (currentFile === executedFile) { app.listen(PORT, '127.0.0.1', () => { console.log(`RYP compiler server running on http://127.0.0.1:${PORT}`); }); } export default app;