-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (74 loc) · 2.29 KB
/
server.js
File metadata and controls
85 lines (74 loc) · 2.29 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
import http from 'http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const port = process.env.PORT || 8080;
const distDir = path.join(__dirname, 'dist');
const MIME_TYPES = {
'.html': 'text/html; charset=utf-8',
'.js': 'application/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.webm': 'audio/webm',
'.woff': 'font/woff',
'.woff2': 'font/woff2'
};
const sendJson = (res, statusCode, payload) => {
res.writeHead(statusCode, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify(payload));
};
const server = http.createServer((req, res) => {
const url = req.url || '/';
if (req.method === 'GET' && url.startsWith('/api/health')) {
return sendJson(res, 200, {
status: 'ok',
service: 'anubisai',
geminiClientSide: true,
time: new Date().toISOString()
});
}
const cleanPath = url.split('?')[0];
const relPath = cleanPath === '/' ? '/index.html' : cleanPath;
const filePath = path.join(distDir, relPath);
const safePath = path.normalize(filePath);
if (!safePath.startsWith(distDir)) {
res.writeHead(403);
res.end('Forbidden');
return;
}
fs.readFile(safePath, (error, content) => {
if (!error) {
const extname = path.extname(safePath).toLowerCase();
res.writeHead(200, { 'Content-Type': MIME_TYPES[extname] || 'application/octet-stream' });
res.end(content);
return;
}
if (error.code !== 'ENOENT') {
res.writeHead(500);
res.end('Internal server error');
return;
}
const fallback = path.join(distDir, 'index.html');
fs.readFile(fallback, (fallbackError, fallbackContent) => {
if (fallbackError) {
res.writeHead(404);
res.end('Not found');
return;
}
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(fallbackContent);
});
});
});
server.listen(port, () => {
console.log(`Anubis AI server listening on port ${port}`);
});