-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserve.js
More file actions
83 lines (73 loc) · 2.73 KB
/
serve.js
File metadata and controls
83 lines (73 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
/**
* Simple HTTP server for AgentFlow Gemini Adapter demo viewer
* Serves the sandbox directory at http://127.0.0.1:5050
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 5050;
const HOST = '127.0.0.1';
const SANDBOX_DIR = path.join(__dirname, 'sandbox');
const MIME_TYPES = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.yaml': 'text/yaml',
'.yml': 'text/yaml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
const server = http.createServer((req, res) => {
let filePath = req.url === '/' ? '/viewer-standalone.html' : req.url;
// Remove query string
filePath = filePath.split('?')[0];
// Sanitize path
filePath = path.normalize(filePath).replace(/^(\.\.[\/\\])+/, '');
filePath = path.join(SANDBOX_DIR, filePath);
const extname = String(path.extname(filePath)).toLowerCase();
const contentType = MIME_TYPES[extname] || 'application/octet-stream';
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 - File Not Found</h1>', 'utf-8');
} else {
res.writeHead(500);
res.end(`Server Error: ${error.code}`, 'utf-8');
}
} else {
res.writeHead(200, {
'Content-Type': contentType,
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-cache'
});
res.end(content, 'utf-8');
}
});
});
server.listen(PORT, HOST, () => {
console.log('╔═══════════════════════════════════════════════════════════╗');
console.log('║ 🚀 AgentFlow Gemini Adapter - Live Server ║');
console.log('╚═══════════════════════════════════════════════════════════╝');
console.log('');
console.log(` 📂 Serving: ${SANDBOX_DIR}`);
console.log(` 🌐 URL: http://${HOST}:${PORT}`);
console.log('');
console.log(` ✨ Server is running! Open http://${HOST}:${PORT} in your browser`);
console.log(' ⛔ Press Ctrl+C to stop the server');
console.log('');
console.log('─'.repeat(61));
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`\n❌ Port ${PORT} is already in use. Try a different port or stop the other server.\n`);
} else {
console.error(`\n❌ Server error: ${err.message}\n`);
}
process.exit(1);
});