-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (44 loc) · 1.45 KB
/
app.js
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
const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
const morgan = require('morgan');
const config = require('./config/default');
const logger = require('./utils/logger');
const rateLimiter = require('./middleware/rateLimiter');
const errorHandler = require('./middleware/errorHandler');
const { ensureDirectoryExists } = require('./utils/helpers');
const whatsappRoutes = require('./routes/whatsapp');
// Create required directories
ensureDirectoryExists('logs');
ensureDirectoryExists(config.storage.mediaDir);
ensureDirectoryExists(config.storage.sessionsDir);
const app = express();
// Security middleware
app.use(helmet());
app.use(cors());
// Request parsing
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Logging
app.use(morgan('combined', { stream: { write: message => logger.info(message.trim()) } }));
// Rate limiting
app.use(rateLimiter);
// Routes
app.use('/api/whatsapp', whatsappRoutes);
// Error handling
app.use(errorHandler);
// Start server
app.listen(config.app.port, () => {
logger.info(`Server running on port ${config.app.port}`);
});
// Handle uncaught exceptions
process.on('uncaughtException', (error) => {
logger.error('Uncaught Exception:', error);
process.exit(1);
});
// Handle unhandled promise rejections
process.on('unhandledRejection', (error) => {
logger.error('Unhandled Rejection:', error);
process.exit(1);
});
module.exports = app;