RaBU1234 commited on
Commit
cdda8ef
Β·
verified Β·
1 Parent(s): 2a4979d

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +101 -11
server.js CHANGED
@@ -1,42 +1,43 @@
1
  const express = require('express');
2
  const cors = require('cors');
3
- const GVisorSandboxManager = require('./gvisor-manager');
4
 
5
  const app = express();
6
  app.use(cors({ origin: '*' }));
7
  app.use(express.json({ limit: '50mb' }));
8
 
9
- const sandboxManager = new GVisorSandboxManager();
10
 
11
- // Initialize on startup
12
  (async () => {
13
  await sandboxManager.initialize();
14
- console.log('πŸ”’ gVisor Sandbox Manager ready');
15
  })();
16
 
 
17
  app.post('/api/sandbox/create', async (req, res) => {
18
  try {
19
- const sandboxId = `sb-${Date.now()}`;
20
  const sandbox = await sandboxManager.createSandbox(sandboxId);
21
 
22
  res.json({
23
  sandboxId,
24
- containerId: sandbox.containerId,
25
  status: 'ready',
26
- runtime: 'gvisor'
27
  });
28
  } catch (error) {
29
- console.error('❌ Sandbox creation error:', error);
30
  res.status(500).json({ error: error.message });
31
  }
32
  });
33
 
 
34
  app.post('/api/sandbox/:id/command', async (req, res) => {
35
  try {
36
  const { id } = req.params;
37
- const { command, args = [] } = req.body;
38
 
39
- const result = await sandboxManager.executeCommand(id, command, args);
40
  res.json(result);
41
  } catch (error) {
42
  console.error('❌ Command error:', error);
@@ -44,6 +45,19 @@ app.post('/api/sandbox/:id/command', async (req, res) => {
44
  }
45
  });
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  app.post('/api/sandbox/:id/files', async (req, res) => {
48
  try {
49
  const { id } = req.params;
@@ -57,16 +71,92 @@ app.post('/api/sandbox/:id/files', async (req, res) => {
57
  }
58
  });
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  app.delete('/api/sandbox/:id', async (req, res) => {
61
  try {
62
  await sandboxManager.destroySandbox(req.params.id);
63
  res.json({ success: true });
64
  } catch (error) {
 
65
  res.status(500).json({ error: error.message });
66
  }
67
  });
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  const PORT = process.env.PORT || 3001;
70
  app.listen(PORT, '0.0.0.0', () => {
71
- console.log(`πŸš€ gVisor Sandbox API on port ${PORT}`);
 
 
 
 
 
 
 
 
 
72
  });
 
1
  const express = require('express');
2
  const cors = require('cors');
3
+ const SimpleSandboxManager = require('./simple-sandbox-manager');
4
 
5
  const app = express();
6
  app.use(cors({ origin: '*' }));
7
  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)}`;
21
  const sandbox = await sandboxManager.createSandbox(sandboxId);
22
 
23
  res.json({
24
  sandboxId,
 
25
  status: 'ready',
26
+ path: sandbox.workspacePath
27
  });
28
  } catch (error) {
29
+ console.error('❌ Create error:', error);
30
  res.status(500).json({ error: error.message });
31
  }
32
  });
33
 
34
+ // Execute command
35
  app.post('/api/sandbox/:id/command', async (req, res) => {
36
  try {
37
  const { id } = req.params;
38
+ const { command, args = [], background = false } = req.body;
39
 
40
+ const result = await sandboxManager.executeCommand(id, command, args, { background });
41
  res.json(result);
42
  } catch (error) {
43
  console.error('❌ Command error:', error);
 
45
  }
46
  });
47
 
48
+ // Stop command
49
+ app.post('/api/sandbox/:id/command/stop', async (req, res) => {
50
+ try {
51
+ const { id } = req.params;
52
+ const result = await sandboxManager.stopCommand(id);
53
+ res.json(result);
54
+ } catch (error) {
55
+ console.error('❌ Stop error:', error);
56
+ res.status(500).json({ error: error.message });
57
+ }
58
+ });
59
+
60
+ // Write files
61
  app.post('/api/sandbox/:id/files', async (req, res) => {
62
  try {
63
  const { id } = req.params;
 
71
  }
72
  });
73
 
74
+ // Read file
75
+ app.get('/api/sandbox/:id/files', async (req, res) => {
76
+ try {
77
+ const { id } = req.params;
78
+ const { path: filePath } = req.query;
79
+
80
+ const content = await sandboxManager.readFile(id, filePath);
81
+ res.send(content);
82
+ } catch (error) {
83
+ console.error('❌ File read error:', error);
84
+ res.status(404).json({ error: 'File not found' });
85
+ }
86
+ });
87
+
88
+ // List files
89
+ app.get('/api/sandbox/:id/files/list', async (req, res) => {
90
+ try {
91
+ const { id } = req.params;
92
+ const { path = '.' } = req.query;
93
+
94
+ const files = await sandboxManager.listFiles(id, path);
95
+ res.json({ files });
96
+ } catch (error) {
97
+ console.error('❌ List files error:', error);
98
+ res.status(500).json({ error: error.message });
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;
106
+ const procInfo = sandboxManager.getProcess(id);
107
+
108
+ if (procInfo) {
109
+ res.json({
110
+ url: `/preview/${id}/`,
111
+ status: 'running',
112
+ commandId: procInfo.cmdId
113
+ });
114
+ } else {
115
+ res.json({
116
+ url: null,
117
+ status: 'no process running'
118
+ });
119
+ }
120
+ } catch (error) {
121
+ res.status(500).json({ error: error.message });
122
+ }
123
+ });
124
+
125
+ // Delete sandbox
126
  app.delete('/api/sandbox/:id', async (req, res) => {
127
  try {
128
  await sandboxManager.destroySandbox(req.params.id);
129
  res.json({ success: true });
130
  } catch (error) {
131
+ console.error('❌ Delete error:', error);
132
  res.status(500).json({ error: error.message });
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); // Every 2 minutes
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()) {
159
+ await sandboxManager.destroySandbox(id);
160
+ }
161
+ process.exit(0);
162
  });