-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
139 lines (116 loc) · 4.35 KB
/
Copy pathserver.js
File metadata and controls
139 lines (116 loc) · 4.35 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// server.js - Main entry point for OmanX Express server
import './config/env.js';
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import chatHandler from './api/chat.js';
import healthHandler from './api/health.js';
import readyHandler from './api/ready.js';
import metricsHandler from './api/metrics.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PORT = Number(process.env.PORT || 3000);
const app = express();
const PUBLIC_DIR = path.join(__dirname, 'public');
const ROUTE_METHODS = new Map([
['/', new Set(['GET'])],
['/workspace', new Set(['GET'])],
['/chat', new Set(['GET'])],
['/system', new Set(['GET'])],
['/method', new Set(['GET'])],
['/vision', new Set(['GET'])],
['/contact', new Set(['GET'])],
['/examples', new Set(['GET'])],
['/trust', new Set(['GET'])],
['/settings', new Set(['GET'])],
['/collaboration', new Set(['GET'])],
['/api/chat', new Set(['POST'])],
['/api/health', new Set(['GET'])],
['/api/ready', new Set(['GET'])],
['/api/metrics', new Set(['GET'])],
]);
// Middleware
app.use(express.json({ limit: '1mb' }));
app.use((req, res, next) => {
const allowedMethods = ROUTE_METHODS.get(req.path);
if (!allowedMethods || allowedMethods.has(req.method)) {
return next();
}
if (req.path.startsWith('/api/')) {
return res.status(405).json({ error: 'Method not allowed' });
}
return res.status(405).sendFile(path.join(PUBLIC_DIR, '405.html'));
});
// API Routes
app.post('/api/chat', chatHandler);
app.get('/api/health', healthHandler);
app.get('/api/ready', readyHandler);
app.get('/api/metrics', metricsHandler);
// Canonical redirects from file-based routes
app.get('/index.html', (req, res) => res.redirect(301, '/'));
app.get('/chat.html', (req, res) => res.redirect(301, '/workspace'));
app.get('/system.html', (req, res) => res.redirect(301, '/system'));
app.get('/method.html', (req, res) => res.redirect(301, '/method'));
app.get('/vision.html', (req, res) => res.redirect(301, '/vision'));
app.get('/contact.html', (req, res) => res.redirect(301, '/contact'));
app.get('/examples.html', (req, res) => res.redirect(301, '/examples'));
app.get('/settings.html', (req, res) => res.redirect(301, '/settings'));
app.get('/collaboration.html', (req, res) => res.redirect(301, '/collaboration'));
app.get('/trust.html', (req, res) => res.redirect(301, '/trust'));
// Page routes
app.get('/', (req, res) => {
res.sendFile(path.join(PUBLIC_DIR, 'index.html'));
});
app.get(['/workspace', '/chat'], (req, res) => {
res.sendFile(path.join(PUBLIC_DIR, 'chat.html'));
});
app.get('/system', (req, res) => {
res.sendFile(path.join(PUBLIC_DIR, 'system.html'));
});
app.get('/method', (req, res) => {
res.sendFile(path.join(PUBLIC_DIR, 'method.html'));
});
app.get('/vision', (req, res) => {
res.sendFile(path.join(PUBLIC_DIR, 'vision.html'));
});
app.get('/contact', (req, res) => {
res.sendFile(path.join(PUBLIC_DIR, 'contact.html'));
});
app.get('/examples', (req, res) => {
res.sendFile(path.join(PUBLIC_DIR, 'examples.html'));
});
app.get('/trust', (req, res) => {
res.sendFile(path.join(PUBLIC_DIR, 'trust.html'));
});
app.get('/settings', (req, res) => {
res.sendFile(path.join(PUBLIC_DIR, 'settings.html'));
});
app.get('/collaboration', (req, res) => {
res.sendFile(path.join(PUBLIC_DIR, 'collaboration.html'));
});
// Legacy redirects
app.get('/info', (req, res) => res.redirect(301, '/system'));
// Serve static files from public directory
app.use(express.static(PUBLIC_DIR));
// Fallback
app.use((req, res) => {
if (req.path.startsWith('/api/')) {
return res.status(404).json({ error: 'API endpoint not found' });
}
return res.status(404).sendFile(path.join(PUBLIC_DIR, '404.html'));
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error('Server error:', err);
if (req.path.startsWith('/api/')) {
return res.status(500).json({ error: 'Internal server error' });
}
return res.status(500).sendFile(path.join(PUBLIC_DIR, '500.html'));
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`OmanX running at http://localhost:${PORT}`);
console.log('API endpoints available at:');
console.log(` - http://localhost:${PORT}/api/chat`);
console.log(` - http://localhost:${PORT}/api/health`);
console.log(` - http://localhost:${PORT}/api/ready`);
console.log(` - http://localhost:${PORT}/api/metrics`);
});