-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
110 lines (94 loc) · 2.99 KB
/
Copy pathserver.ts
File metadata and controls
110 lines (94 loc) · 2.99 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { createServer } from 'node:http';
import { readFileSync, existsSync } from 'fs';
import { join, dirname, extname } from 'path';
import { fileURLToPath } from 'url';
import {
CopilotRuntime,
ExperimentalEmptyAdapter,
copilotRuntimeNodeHttpEndpoint,
} from '@copilotkit/runtime';
// Get __dirname equivalent for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// MIME type mapping
const mimeTypes: Record<string, string> = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.webp': 'image/webp',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.eot': 'font/eot'
};
const serviceAdapter = new ExperimentalEmptyAdapter();
const runtime = new CopilotRuntime({
remoteEndpoints: [
{ url: "http://localhost:8000/copilotkit" },
],
});
const handler = copilotRuntimeNodeHttpEndpoint({
endpoint: '/copilotkit',
runtime,
serviceAdapter,
});
const server = createServer((req, res) => {
// Add CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// Handle preflight requests
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
// Handle CopilotKit requests
if (req.url?.startsWith('/copilotkit')) {
return handler(req, res);
}
// Serve static files from React build
const url = req.url || '/';
let requestPath = url === '/' ? '/index.html' : url;
// Remove query parameters for file path resolution
requestPath = requestPath.split('?')[0];
const filePath = join(__dirname, 'client', 'dist', requestPath);
if (existsSync(filePath)) {
try {
const content = readFileSync(filePath);
const ext = extname(filePath);
const mimeType = mimeTypes[ext] || 'application/octet-stream';
res.setHeader('Content-Type', mimeType);
res.writeHead(200);
res.end(content);
} catch (error) {
console.error('Error reading file:', error);
res.writeHead(500);
res.end('Internal Server Error');
}
} else {
// For SPA routing, serve index.html for any non-API routes
try {
const fallback = readFileSync(join(__dirname, 'client', 'dist', 'index.html'));
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(fallback);
} catch (error) {
console.error('Error reading index.html:', error);
res.writeHead(404);
res.end('Not Found');
}
}
});
const PORT = process.env.PORT || 4000;
server.listen(PORT, () => {
console.log(`🚀 Server running on http://localhost:${PORT}`);
console.log(`📁 Serving static files from client/dist`);
console.log(`🤖 CopilotKit endpoint available at /copilotkit`);
});