Update server.js
Browse files
server.js
CHANGED
|
@@ -8,13 +8,11 @@ app.use(express.json({ limit: '50mb' }));
|
|
| 8 |
|
| 9 |
const sandboxManager = new SimpleSandboxManager();
|
| 10 |
|
| 11 |
-
// Initialize
|
| 12 |
(async () => {
|
| 13 |
await sandboxManager.initialize();
|
| 14 |
console.log('π Sandbox Manager ready');
|
| 15 |
})();
|
| 16 |
|
| 17 |
-
// Create sandbox
|
| 18 |
app.post('/api/sandbox/create', async (req, res) => {
|
| 19 |
try {
|
| 20 |
const sandboxId = `sb-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
@@ -23,7 +21,8 @@ app.post('/api/sandbox/create', async (req, res) => {
|
|
| 23 |
res.json({
|
| 24 |
sandboxId,
|
| 25 |
status: 'ready',
|
| 26 |
-
path: sandbox.workspacePath
|
|
|
|
| 27 |
});
|
| 28 |
} catch (error) {
|
| 29 |
console.error('β Create error:', error);
|
|
@@ -31,7 +30,6 @@ app.post('/api/sandbox/create', async (req, res) => {
|
|
| 31 |
}
|
| 32 |
});
|
| 33 |
|
| 34 |
-
// Execute command
|
| 35 |
app.post('/api/sandbox/:id/command', async (req, res) => {
|
| 36 |
try {
|
| 37 |
const { id } = req.params;
|
|
@@ -45,7 +43,6 @@ app.post('/api/sandbox/:id/command', async (req, res) => {
|
|
| 45 |
}
|
| 46 |
});
|
| 47 |
|
| 48 |
-
// Stop command
|
| 49 |
app.post('/api/sandbox/:id/command/stop', async (req, res) => {
|
| 50 |
try {
|
| 51 |
const { id } = req.params;
|
|
@@ -57,7 +54,6 @@ app.post('/api/sandbox/:id/command/stop', async (req, res) => {
|
|
| 57 |
}
|
| 58 |
});
|
| 59 |
|
| 60 |
-
// Write files
|
| 61 |
app.post('/api/sandbox/:id/files', async (req, res) => {
|
| 62 |
try {
|
| 63 |
const { id } = req.params;
|
|
@@ -71,7 +67,6 @@ app.post('/api/sandbox/:id/files', async (req, res) => {
|
|
| 71 |
}
|
| 72 |
});
|
| 73 |
|
| 74 |
-
// Read file
|
| 75 |
app.get('/api/sandbox/:id/files', async (req, res) => {
|
| 76 |
try {
|
| 77 |
const { id } = req.params;
|
|
@@ -85,7 +80,6 @@ app.get('/api/sandbox/:id/files', async (req, res) => {
|
|
| 85 |
}
|
| 86 |
});
|
| 87 |
|
| 88 |
-
// List files
|
| 89 |
app.get('/api/sandbox/:id/files/list', async (req, res) => {
|
| 90 |
try {
|
| 91 |
const { id } = req.params;
|
|
@@ -99,7 +93,6 @@ app.get('/api/sandbox/:id/files/list', async (req, res) => {
|
|
| 99 |
}
|
| 100 |
});
|
| 101 |
|
| 102 |
-
// Get sandbox URL (for dev servers)
|
| 103 |
app.get('/api/sandbox/:id/url', async (req, res) => {
|
| 104 |
try {
|
| 105 |
const { id } = req.params;
|
|
@@ -107,9 +100,10 @@ app.get('/api/sandbox/:id/url', async (req, res) => {
|
|
| 107 |
|
| 108 |
if (procInfo) {
|
| 109 |
res.json({
|
| 110 |
-
url: `/preview/${
|
| 111 |
status: 'running',
|
| 112 |
-
commandId: procInfo.cmdId
|
|
|
|
| 113 |
});
|
| 114 |
} else {
|
| 115 |
res.json({
|
|
@@ -122,7 +116,6 @@ app.get('/api/sandbox/:id/url', async (req, res) => {
|
|
| 122 |
}
|
| 123 |
});
|
| 124 |
|
| 125 |
-
// Delete sandbox
|
| 126 |
app.delete('/api/sandbox/:id', async (req, res) => {
|
| 127 |
try {
|
| 128 |
await sandboxManager.destroySandbox(req.params.id);
|
|
@@ -133,26 +126,24 @@ app.delete('/api/sandbox/:id', async (req, res) => {
|
|
| 133 |
}
|
| 134 |
});
|
| 135 |
|
| 136 |
-
// Health check
|
| 137 |
app.get('/', (req, res) => {
|
| 138 |
res.json({
|
| 139 |
status: 'running',
|
| 140 |
sandboxes: sandboxManager.sandboxes.size,
|
| 141 |
-
processes: sandboxManager.processes.size
|
|
|
|
| 142 |
});
|
| 143 |
});
|
| 144 |
|
| 145 |
-
// Auto cleanup
|
| 146 |
setInterval(() => {
|
| 147 |
sandboxManager.cleanupInactive();
|
| 148 |
-
}, 2 * 60 * 1000);
|
| 149 |
|
| 150 |
const PORT = process.env.PORT || 3001;
|
| 151 |
app.listen(PORT, '0.0.0.0', () => {
|
| 152 |
console.log(`π Sandbox API running on port ${PORT}`);
|
| 153 |
});
|
| 154 |
|
| 155 |
-
// Graceful shutdown
|
| 156 |
process.on('SIGTERM', async () => {
|
| 157 |
console.log('β οΈ Shutting down...');
|
| 158 |
for (const [id] of sandboxManager.sandboxes.entries()) {
|
|
|
|
| 8 |
|
| 9 |
const sandboxManager = new SimpleSandboxManager();
|
| 10 |
|
|
|
|
| 11 |
(async () => {
|
| 12 |
await sandboxManager.initialize();
|
| 13 |
console.log('π Sandbox Manager ready');
|
| 14 |
})();
|
| 15 |
|
|
|
|
| 16 |
app.post('/api/sandbox/create', async (req, res) => {
|
| 17 |
try {
|
| 18 |
const sandboxId = `sb-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
|
|
| 21 |
res.json({
|
| 22 |
sandboxId,
|
| 23 |
status: 'ready',
|
| 24 |
+
path: sandbox.workspacePath,
|
| 25 |
+
port: sandbox.port
|
| 26 |
});
|
| 27 |
} catch (error) {
|
| 28 |
console.error('β Create error:', error);
|
|
|
|
| 30 |
}
|
| 31 |
});
|
| 32 |
|
|
|
|
| 33 |
app.post('/api/sandbox/:id/command', async (req, res) => {
|
| 34 |
try {
|
| 35 |
const { id } = req.params;
|
|
|
|
| 43 |
}
|
| 44 |
});
|
| 45 |
|
|
|
|
| 46 |
app.post('/api/sandbox/:id/command/stop', async (req, res) => {
|
| 47 |
try {
|
| 48 |
const { id } = req.params;
|
|
|
|
| 54 |
}
|
| 55 |
});
|
| 56 |
|
|
|
|
| 57 |
app.post('/api/sandbox/:id/files', async (req, res) => {
|
| 58 |
try {
|
| 59 |
const { id } = req.params;
|
|
|
|
| 67 |
}
|
| 68 |
});
|
| 69 |
|
|
|
|
| 70 |
app.get('/api/sandbox/:id/files', async (req, res) => {
|
| 71 |
try {
|
| 72 |
const { id } = req.params;
|
|
|
|
| 80 |
}
|
| 81 |
});
|
| 82 |
|
|
|
|
| 83 |
app.get('/api/sandbox/:id/files/list', async (req, res) => {
|
| 84 |
try {
|
| 85 |
const { id } = req.params;
|
|
|
|
| 93 |
}
|
| 94 |
});
|
| 95 |
|
|
|
|
| 96 |
app.get('/api/sandbox/:id/url', async (req, res) => {
|
| 97 |
try {
|
| 98 |
const { id } = req.params;
|
|
|
|
| 100 |
|
| 101 |
if (procInfo) {
|
| 102 |
res.json({
|
| 103 |
+
url: `/preview/${procInfo.port}/`,
|
| 104 |
status: 'running',
|
| 105 |
+
commandId: procInfo.cmdId,
|
| 106 |
+
port: procInfo.port
|
| 107 |
});
|
| 108 |
} else {
|
| 109 |
res.json({
|
|
|
|
| 116 |
}
|
| 117 |
});
|
| 118 |
|
|
|
|
| 119 |
app.delete('/api/sandbox/:id', async (req, res) => {
|
| 120 |
try {
|
| 121 |
await sandboxManager.destroySandbox(req.params.id);
|
|
|
|
| 126 |
}
|
| 127 |
});
|
| 128 |
|
|
|
|
| 129 |
app.get('/', (req, res) => {
|
| 130 |
res.json({
|
| 131 |
status: 'running',
|
| 132 |
sandboxes: sandboxManager.sandboxes.size,
|
| 133 |
+
processes: sandboxManager.processes.size,
|
| 134 |
+
message: 'π Simple Sandbox API'
|
| 135 |
});
|
| 136 |
});
|
| 137 |
|
|
|
|
| 138 |
setInterval(() => {
|
| 139 |
sandboxManager.cleanupInactive();
|
| 140 |
+
}, 2 * 60 * 1000);
|
| 141 |
|
| 142 |
const PORT = process.env.PORT || 3001;
|
| 143 |
app.listen(PORT, '0.0.0.0', () => {
|
| 144 |
console.log(`π Sandbox API running on port ${PORT}`);
|
| 145 |
});
|
| 146 |
|
|
|
|
| 147 |
process.on('SIGTERM', async () => {
|
| 148 |
console.log('β οΈ Shutting down...');
|
| 149 |
for (const [id] of sandboxManager.sandboxes.entries()) {
|