-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
78 lines (65 loc) · 1.99 KB
/
server.js
File metadata and controls
78 lines (65 loc) · 1.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const rateLimit = require('express-rate-limit');
require('dotenv').config();
const db = require('./database');
const bookingRoutes = require('./routes/bookings');
// Initialize Telegram Bot
try {
require('./bot');
console.log('Telegram bot initialized');
} catch (error) {
console.error('Failed to initialize Telegram bot:', error);
}
const app = express();
const PORT = process.env.PORT || 3000;
// --- THE FIX FOR CANCELLING RESPONSIVENESS ---
// Tell Express to trust Railway's proxy headers so rateLimit works correctly
app.set('trust proxy', 1);
// Rate limiting configuration
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100,
message: 'Too many requests from this IP, please try again later.',
standardHeaders: true,
legacyHeaders: false,
});
// Middleware
const corsOptions = {
origin: process.env.NODE_ENV === 'production'
? [process.env.WEB_APP_URL]
: '*',
optionsSuccessStatus: 200
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Apply rate limiting to API routes
app.use('/api/', apiLimiter);
// Serve static files (frontend)
app.use(express.static(path.join(__dirname, 'public')));
// API Routes
app.use('/api/bookings', bookingRoutes);
// Root endpoint
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// Initialize database and start server
db.initialize()
.then(() => {
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
});
})
.catch(err => {
console.error('Failed to initialize database:', err);
process.exit(1);
});
module.exports = app;