-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
120 lines (98 loc) · 2.89 KB
/
server.js
File metadata and controls
120 lines (98 loc) · 2.89 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
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import rateLimit from 'express-rate-limit';
import { createServer } from 'http';
import dotenv from 'dotenv';
import swaggerUi from 'swagger-ui-express';
// Load environment variables
dotenv.config();
// Connect database
import { connectDatabase } from './app/config/database.js';
// Import routes
import authRoutes from './app/routes/auth.js';
import roomsRoutes from './app/routes/rooms.js';
// Import middleware
import { errorHandler, notFoundHandler } from './app/middleware/errorHandler.js';
// Import Swagger
import { swaggerSpec } from './app/config/swagger.js';
// Import Socket.io
import { initializeSocket } from './app/tools/socket.js';
// Import face recognition models loader
import { loadModels } from './app/utils/faceRecognition.js';
const app = express();
const server = createServer(app);
const PORT = process.env.PORT || 3000;
// Connect to MongoDB
connectDatabase().catch((error) => {
console.error('Failed to connect to MongoDB:', error);
process.exit(1);
});
// Security middleware
app.use(helmet());
app.use(cors({
origin: process.env.CORS_ORIGIN || '*',
credentials: true
}));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
app.use('/api/', limiter);
// Body parsing middleware
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// Health check
app.get('/health', (req, res) => {
res.json({
success: true,
message: 'SafeChat API is running',
timestamp: new Date().toISOString()
});
});
// API Documentation
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, {
customCss: '.swagger-ui .topbar { display: none }',
customSiteTitle: 'SafeChat API Documentation'
}));
// API Routes
app.use('/api/auth', authRoutes);
app.use('/api/rooms', roomsRoutes);
// Root route
app.get('/', (req, res) => {
res.json({
success: true,
message: '🛡️ SafeChat API - Secure Real-Time Messaging',
version: '1.0.0',
documentation: '/api-docs',
health: '/health'
});
});
// Error handling middleware (must be last)
app.use(notFoundHandler);
app.use(errorHandler);
// Initialize Socket.io
const io = initializeSocket(server);
// Load face recognition models
loadModels().catch((error) => {
console.error('Failed to load face recognition models:', error);
});
// Start server
server.listen(PORT, () => {
console.log(`
🚀 SafeChat API Server
📍 Port: ${PORT}
📖 Documentation: http://localhost:${PORT}/api-docs
❤️ Health Check: http://localhost:${PORT}/health
`);
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM signal received: closing HTTP server');
server.close(() => {
console.log('HTTP server closed');
process.exit(0);
});
});
export { app, server, io };