Update server.js
Browse files
server.js
CHANGED
|
@@ -32,7 +32,7 @@ app.get('/', (req, res) => {
|
|
| 32 |
app.post('/api/sandbox/create', async (req, res) => {
|
| 33 |
try {
|
| 34 |
const { timeout = 600000, ports = [] } = req.body;
|
| 35 |
-
const sandboxId =
|
| 36 |
const sandbox = await sandboxManager.createSandbox(sandboxId, { timeout, ports });
|
| 37 |
|
| 38 |
res.json({
|
|
@@ -138,7 +138,7 @@ app.get('/api/sandbox/:sandboxId/cmds/:cmdId', async (req, res) => {
|
|
| 138 |
app.get('/api/sandbox/:sandboxId/cmds/:cmdId/logs', (req, res) => {
|
| 139 |
const { sandboxId, cmdId } = req.params;
|
| 140 |
|
| 141 |
-
console.log(
|
| 142 |
|
| 143 |
res.setHeader('Content-Type', 'text/event-stream');
|
| 144 |
res.setHeader('Cache-Control', 'no-cache');
|
|
@@ -151,24 +151,26 @@ app.get('/api/sandbox/:sandboxId/cmds/:cmdId/logs', (req, res) => {
|
|
| 151 |
const command = sandboxManager.getCommand(sandboxId, cmdId);
|
| 152 |
|
| 153 |
if (!command) {
|
| 154 |
-
res.write(
|
| 155 |
error: 'Command not found'
|
| 156 |
-
})
|
| 157 |
|
| 158 |
-
|
| 159 |
res.end();
|
| 160 |
return;
|
| 161 |
}
|
| 162 |
-
|
| 163 |
-
command.logs.
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
| 169 |
');
|
| 170 |
-
|
| 171 |
-
}
|
|
|
|
| 172 |
sandboxManager.addLogStream(sandboxId, cmdId, res);
|
| 173 |
|
| 174 |
const keepAlive = setInterval(() => {
|
|
@@ -183,7 +185,7 @@ if (command.logs && command.logs.length > 0) {
|
|
| 183 |
|
| 184 |
req.on('close', () => {
|
| 185 |
clearInterval(keepAlive);
|
| 186 |
-
console.log(
|
| 187 |
res.end();
|
| 188 |
});
|
| 189 |
});
|
|
@@ -253,7 +255,7 @@ app.get('/api/sandbox/:sandboxId/url', async (req, res) => {
|
|
| 253 |
}
|
| 254 |
|
| 255 |
const baseUrl = process.env.SPACE_HOST || 'localhost:7860';
|
| 256 |
-
const url =
|
| 257 |
|
| 258 |
res.json({
|
| 259 |
url,
|
|
@@ -281,7 +283,7 @@ setInterval(() => {
|
|
| 281 |
|
| 282 |
const PORT = process.env.PORT || 7860;
|
| 283 |
app.listen(PORT, '0.0.0.0', () => {
|
| 284 |
-
console.log(
|
| 285 |
});
|
| 286 |
|
| 287 |
process.on('SIGTERM', async () => {
|
|
|
|
| 32 |
app.post('/api/sandbox/create', async (req, res) => {
|
| 33 |
try {
|
| 34 |
const { timeout = 600000, ports = [] } = req.body;
|
| 35 |
+
const sandboxId = 'sbx-' + Date.now() + '-' + Math.random().toString(36).substr(2, 9);
|
| 36 |
const sandbox = await sandboxManager.createSandbox(sandboxId, { timeout, ports });
|
| 37 |
|
| 38 |
res.json({
|
|
|
|
| 138 |
app.get('/api/sandbox/:sandboxId/cmds/:cmdId/logs', (req, res) => {
|
| 139 |
const { sandboxId, cmdId } = req.params;
|
| 140 |
|
| 141 |
+
console.log('📡 SSE logs connection for ' + sandboxId + '/' + cmdId);
|
| 142 |
|
| 143 |
res.setHeader('Content-Type', 'text/event-stream');
|
| 144 |
res.setHeader('Cache-Control', 'no-cache');
|
|
|
|
| 151 |
const command = sandboxManager.getCommand(sandboxId, cmdId);
|
| 152 |
|
| 153 |
if (!command) {
|
| 154 |
+
res.write('data: ' + JSON.stringify({
|
| 155 |
error: 'Command not found'
|
| 156 |
+
}) + '
|
| 157 |
|
| 158 |
+
');
|
| 159 |
res.end();
|
| 160 |
return;
|
| 161 |
}
|
| 162 |
+
|
| 163 |
+
if (command.logs && command.logs.length > 0) {
|
| 164 |
+
command.logs.forEach(log => {
|
| 165 |
+
res.write(JSON.stringify({
|
| 166 |
+
data: log.data,
|
| 167 |
+
stream: log.stream,
|
| 168 |
+
timestamp: log.timestamp
|
| 169 |
+
}) + '
|
| 170 |
');
|
| 171 |
+
});
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
sandboxManager.addLogStream(sandboxId, cmdId, res);
|
| 175 |
|
| 176 |
const keepAlive = setInterval(() => {
|
|
|
|
| 185 |
|
| 186 |
req.on('close', () => {
|
| 187 |
clearInterval(keepAlive);
|
| 188 |
+
console.log('📡 SSE logs connection closed for ' + sandboxId + '/' + cmdId);
|
| 189 |
res.end();
|
| 190 |
});
|
| 191 |
});
|
|
|
|
| 255 |
}
|
| 256 |
|
| 257 |
const baseUrl = process.env.SPACE_HOST || 'localhost:7860';
|
| 258 |
+
const url = 'https://' + baseUrl + '/preview/' + targetPort + '/';
|
| 259 |
|
| 260 |
res.json({
|
| 261 |
url,
|
|
|
|
| 283 |
|
| 284 |
const PORT = process.env.PORT || 7860;
|
| 285 |
app.listen(PORT, '0.0.0.0', () => {
|
| 286 |
+
console.log('🚀 Sandbox API running on port ' + PORT);
|
| 287 |
});
|
| 288 |
|
| 289 |
process.on('SIGTERM', async () => {
|