RaBU1234 commited on
Commit
161aa64
Β·
verified Β·
1 Parent(s): 4bbcfdc

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +16 -49
server.js CHANGED
@@ -1,85 +1,55 @@
1
  const express = require('express');
2
  const cors = require('cors');
3
- const FirecrackerManager = require('./firecracker-manager');
4
 
5
  const app = express();
6
  app.use(cors({ origin: '*' }));
7
  app.use(express.json({ limit: '50mb' }));
8
 
9
- // Initialize Firecracker
10
- const fcManager = new FirecrackerManager();
11
 
12
- // Startup
13
  (async () => {
14
- await fcManager.initialize();
15
- console.log('πŸ”₯ Firecracker Manager initialized');
16
  })();
17
 
18
- // Create sandbox with Firecracker VM
19
  app.post('/api/sandbox/create', async (req, res) => {
20
  try {
21
- const sandboxId = `sandbox-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
 
22
 
23
- console.log(`πŸš€ Creating Firecracker VM for ${sandboxId}...`);
24
- const vm = await fcManager.createVM(sandboxId);
25
-
26
- res.json({
27
  sandboxId,
28
- vmId: vm.vmId,
29
  status: 'ready',
30
- bootTime: Date.now() - vm.createdAt
31
  });
32
  } catch (error) {
33
- console.error('❌ VM creation error:', error);
34
  res.status(500).json({ error: error.message });
35
  }
36
  });
37
 
38
- // Execute command in VM
39
  app.post('/api/sandbox/:id/command', async (req, res) => {
40
  try {
41
  const { id } = req.params;
42
  const { command, args = [] } = req.body;
43
 
44
- const vm = fcManager.getVM(id);
45
- if (!vm) {
46
- return res.status(404).json({ error: 'Sandbox not found' });
47
- }
48
-
49
- console.log(`⚑ Executing in VM ${vm.vmId}: ${command}`);
50
-
51
- // Execute via Firecracker
52
- const result = await fcManager.executeInVM(id, `${command} ${args.join(' ')}`);
53
-
54
- res.json({
55
- exitCode: 0,
56
- stdout: result,
57
- stderr: '',
58
- vmId: vm.vmId
59
- });
60
  } catch (error) {
61
  console.error('❌ Command error:', error);
62
  res.status(500).json({ error: error.message });
63
  }
64
  });
65
 
66
- // Write files to VM
67
  app.post('/api/sandbox/:id/files', async (req, res) => {
68
  try {
69
  const { id } = req.params;
70
  const { files } = req.body;
71
 
72
- const vm = fcManager.getVM(id);
73
- if (!vm) {
74
- return res.status(404).json({ error: 'Sandbox not found' });
75
- }
76
-
77
- console.log(`πŸ“ Writing ${files.length} files to VM ${vm.vmId}`);
78
-
79
- for (const file of files) {
80
- await fcManager.writeFile(id, file.path, file.content);
81
- }
82
-
83
  res.json({ success: true, filesWritten: files.length });
84
  } catch (error) {
85
  console.error('❌ File write error:', error);
@@ -87,19 +57,16 @@ app.post('/api/sandbox/:id/files', async (req, res) => {
87
  }
88
  });
89
 
90
- // Delete sandbox
91
  app.delete('/api/sandbox/:id', async (req, res) => {
92
  try {
93
- const { id } = req.params;
94
- await fcManager.destroyVM(id);
95
  res.json({ success: true });
96
  } catch (error) {
97
- console.error('❌ Delete error:', error);
98
  res.status(500).json({ error: error.message });
99
  }
100
  });
101
 
102
  const PORT = process.env.PORT || 3001;
103
  app.listen(PORT, '0.0.0.0', () => {
104
- console.log(`πŸš€ Firecracker Sandbox API running on port ${PORT}`);
105
  });
 
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);
43
  res.status(500).json({ error: error.message });
44
  }
45
  });
46
 
 
47
  app.post('/api/sandbox/:id/files', async (req, res) => {
48
  try {
49
  const { id } = req.params;
50
  const { files } = req.body;
51
 
52
+ await sandboxManager.writeFiles(id, files);
 
 
 
 
 
 
 
 
 
 
53
  res.json({ success: true, filesWritten: files.length });
54
  } catch (error) {
55
  console.error('❌ File write error:', error);
 
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
  });