Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
104 lines (86 loc) · 3.55 KB
/
Copy pathserver.js
File metadata and controls
104 lines (86 loc) · 3.55 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
import 'dotenv/config';
import express from 'express';
import http from 'http';
import { Server as SocketIO } from 'socket.io';
import helmet from 'helmet';
import cors from 'cors';
import { rateLimit } from 'express-rate-limit';
import mongoose from 'mongoose';
import { connectDB } from './config/db.js';
import { errorHandler } from './middleware/errorHandler.js';
import { todayIST } from './utils/time.js';
import sessionRoutes from './routes/session.js';
import memberRoutes from './routes/members.js';
import menuRoutes from './routes/menu.js';
import orderRoutes from './routes/orders.js';
const app = express();
const server = http.createServer(app);
// Comma-separated list of allowed frontend origins (shared by REST CORS and Socket.IO)
const allowedOrigins = (process.env.CLIENT_ORIGIN || 'http://localhost:5173')
.split(',')
.map((o) => o.trim())
.filter(Boolean);
// ── Socket.IO ──────────────────────────────────────────────────────────────
const io = new SocketIO(server, {
cors: {
origin: allowedOrigins,
methods: ['GET', 'POST'],
},
});
app.set('io', io);
io.on('connection', (socket) => {
console.log(`🔌 Client connected: ${socket.id}`);
socket.on('disconnect', () => console.log(`❌ Client disconnected: ${socket.id}`));
});
// ── Security / parsing ─────────────────────────────────────────────────────
app.use(
helmet({
crossOriginResourcePolicy: { policy: 'cross-origin' },
})
);
app.use(
cors({
origin: (origin, cb) => {
if (!origin || allowedOrigins.includes(origin)) return cb(null, true);
cb(new Error('CORS: origin not allowed'));
},
credentials: true,
})
);
app.use(express.json());
app.use(
rateLimit({
windowMs: 60_000,
max: 120,
standardHeaders: true,
legacyHeaders: false,
})
);
// ── Routes ─────────────────────────────────────────────────────────────────
app.use('/api/session', sessionRoutes);
app.use('/api/members', memberRoutes);
app.use('/api/menu', menuRoutes);
app.use('/api/orders', orderRoutes);
// Uptime monitor target: keeps the Render instance awake and the Mongo connection warm.
// GET and HEAD both work; 503 tells the monitor the database is unreachable.
app.get('/api/health', async (_req, res) => {
try {
await mongoose.connection.db.admin().ping();
res.json({ ok: true, db: 'up', uptime: Math.round(process.uptime()) });
} catch {
res.status(503).json({ ok: false, db: 'down' });
}
});
// ── Error handler ──────────────────────────────────────────────────────────
app.use(errorHandler);
// ── Start ──────────────────────────────────────────────────────────────────
const PORT = process.env.PORT || 4000;
connectDB().then(() => {
server.listen(PORT, () => {
console.log(`🚀 Chai Time API running on http://localhost:${PORT}`);
console.log(
`🕒 server now ${new Date().toISOString()} | IST date ${todayIST()} | CUTOFF_TIME=${process.env.CUTOFF_TIME ?? '(unset)'} (IST)`
);
});
});
export { app, server, io };