@dhruvdobariya/notification-provider
Version:
Multi-provider notification service with failover and scheduling
85 lines (69 loc) • 2.25 kB
JavaScript
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const notificationRoutes = require('./routes/notificationRoutes');
const logger = require('./utils/logger');
const errorHandler = require('./middleware/errorHandler');
const SchedulerService = require('./services/SchedulerService');
const app = express();
const PORT = process.env.PORT || 3000;
// Create logs directory if it doesn't exist
const logsDir = path.join(__dirname, '../logs');
if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir, { recursive: true });
}
// Security middleware
app.use(helmet());
app.use(cors());
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: 'Too many requests from this IP, please try again later.'
});
app.use(limiter);
// Body parsing middleware
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true }));
// Initialize scheduler service
const schedulerService = new SchedulerService();
// Routes
app.use('/api/notifications', notificationRoutes);
// Health check
app.get('/health', (req, res) => {
res.status(200).json({
status: 'OK',
timestamp: new Date().toISOString(),
service: 'Notification Service',
scheduler: {
active: schedulerService.getActiveJobs().length,
running: schedulerService.isRunning()
}
});
});
// Error handling
app.use(errorHandler);
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({ error: 'Route not found' });
});
// Graceful shutdown
process.on('SIGTERM', () => {
logger.info('SIGTERM received, shutting down gracefully');
schedulerService.stop();
process.exit(0);
});
process.on('SIGINT', () => {
logger.info('SIGINT received, shutting down gracefully');
schedulerService.stop();
process.exit(0);
});
app.listen(PORT, () => {
logger.info(`Notification Service running on port ${PORT}`);
schedulerService.start();
});
module.exports = app;