Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const cors = require('cors');
|
| 3 |
+
const { exec, spawn } = require('child_process');
|
| 4 |
+
const fs = require('fs').promises;
|
| 5 |
+
const path = require('path');
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
app.use(cors());
|
| 9 |
+
app.use(express.json({ limit: '50mb' }));
|
| 10 |
+
|
| 11 |
+
const sandboxes = new Map();
|
| 12 |
+
const devServers = new Map();
|
| 13 |
+
|
| 14 |
+
// Health check
|
| 15 |
+
app.get('/', (req, res) => {
|
| 16 |
+
res.json({
|
| 17 |
+
status: 'Sandbox server running',
|
| 18 |
+
sandboxes: sandboxes.size,
|
| 19 |
+
timestamp: new Date().toISOString()
|
| 20 |
+
});
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
// Create sandbox
|
| 24 |
+
app.post('/api/sandbox/create', async (req, res) => {
|
| 25 |
+
try {
|
| 26 |
+
const sandboxId = `sandbox-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
| 27 |
+
const sandboxPath = path.join('/tmp', sandboxId);
|
| 28 |
+
|
| 29 |
+
await fs.mkdir(sandboxPath, { recursive: true });
|
| 30 |
+
sandboxes.set(sandboxId, {
|
| 31 |
+
path: sandboxPath,
|
| 32 |
+
createdAt: Date.now()
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
console.log(`✅ Sandbox created: ${sandboxId}`);
|
| 36 |
+
res.json({
|
| 37 |
+
sandboxId,
|
| 38 |
+
status: 'ready',
|
| 39 |
+
path: sandboxPath
|
| 40 |
+
});
|
| 41 |
+
} catch (error) {
|
| 42 |
+
console.error('❌ Error:', error);
|
| 43 |
+
res.status(500).json({ error: error.message });
|
| 44 |
+
}
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
// Run command
|
| 48 |
+
app.post('/api/sandbox/:id/command', async (req, res) => {
|
| 49 |
+
try {
|
| 50 |
+
const { id } = req.params;
|
| 51 |
+
const { command, args = [] } = req.body;
|
| 52 |
+
const sandbox = sandboxes.get(id);
|
| 53 |
+
|
| 54 |
+
if (!sandbox) {
|
| 55 |
+
return res.status(404).json({ error: 'Sandbox not found' });
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
const fullCommand = `cd ${sandbox.path} && ${command} ${args.join(' ')}`;
|
| 59 |
+
console.log(`⚡ Executing: ${fullCommand}`);
|
| 60 |
+
|
| 61 |
+
exec(fullCommand, {
|
| 62 |
+
timeout: 300000,
|
| 63 |
+
maxBuffer: 10 * 1024 * 1024
|
| 64 |
+
}, (error, stdout, stderr) => {
|
| 65 |
+
res.json({
|
| 66 |
+
exitCode: error ? (error.code || 1) : 0,
|
| 67 |
+
stdout: stdout.toString(),
|
| 68 |
+
stderr: stderr.toString()
|
| 69 |
+
});
|
| 70 |
+
});
|
| 71 |
+
} catch (error) {
|
| 72 |
+
res.status(500).json({ error: error.message });
|
| 73 |
+
}
|
| 74 |
+
});
|
| 75 |
+
|
| 76 |
+
// Write files
|
| 77 |
+
app.post('/api/sandbox/:id/files', async (req, res) => {
|
| 78 |
+
try {
|
| 79 |
+
const { id } = req.params;
|
| 80 |
+
const { files } = req.body;
|
| 81 |
+
const sandbox = sandboxes.get(id);
|
| 82 |
+
|
| 83 |
+
if (!sandbox) {
|
| 84 |
+
return res.status(404).json({ error: 'Sandbox not found' });
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
for (const file of files) {
|
| 88 |
+
const fullPath = path.join(sandbox.path, file.path);
|
| 89 |
+
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
| 90 |
+
await fs.writeFile(fullPath, file.content, 'utf-8');
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
res.json({ success: true, filesWritten: files.length });
|
| 94 |
+
} catch (error) {
|
| 95 |
+
res.status(500).json({ error: error.message });
|
| 96 |
+
}
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
// Start server on port 7860
|
| 100 |
+
const PORT = process.env.PORT || 7860;
|
| 101 |
+
app.listen(PORT, '0.0.0.0', () => {
|
| 102 |
+
console.log(`🚀 Server running on port ${PORT}`);
|
| 103 |
+
});
|