-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (46 loc) · 1.82 KB
/
Copy pathserver.js
File metadata and controls
62 lines (46 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const express = require('express');
const { exec } = require('child_process');
const fs = require('fs').promises;
const path = require('path');
const crypto = require('crypto');
const app = express();
const PORT = 5000;
app.use(express.json());
// Ensure the test_files directory exists
const testDir = path.join(__dirname, 'test_files');
fs.mkdir(testDir, { recursive: true }).catch(console.error);
// Serve the frontend UI
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'templates', 'index.html'));
});
// Handle code execution
app.post('/run', async (req, res) => {
const { code, mode } = req.body;
if (!code || !code.trim()) {
return res.json({ output: "Error: No code provided." });
}
const filename = `web_${crypto.randomUUID().replace(/-/g, '')}.cpp`;
const filepath = path.join(testDir, filename);
// Default to --run if no mode is selected
const cliFlag = mode || '--run';
try {
await fs.writeFile(filepath, code);
exec(`./sandboxcc ${filepath} ${cliFlag}`, { timeout: 35000 }, async (error, stdout, stderr) => {
let output = stdout || stderr || "";
if (error && error.killed) {
output += "\n[!] Error: Execution timed out (exceeded 35 seconds).";
} else if (error && !output) {
output += `\n[!] Process Error: ${error.message}`;
}
try { await fs.unlink(filepath); } catch (e) {}
console.log(`\n--- Execution Request ---`);
console.log(output.trim());
res.json({ output: output.trim() });
});
} catch (err) {
res.json({ output: `[!] Server Error: ${err.message}` });
}
});
app.listen(PORT, () => {
console.log(`🚀 Advanced Dashboard running on http://127.0.0.1:${PORT}`);
});