Some checks failed
Deploy / deploy (push) Has been cancelled
Full-stack web application for Telegram management - Frontend: Vue 3 + Vben Admin - Backend: NestJS - Features: User management, group broadcast, statistics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
92 lines
2.2 KiB
JavaScript
92 lines
2.2 KiB
JavaScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import helmet from 'helmet';
|
|
import dotenv from 'dotenv';
|
|
import { connectDatabase } from './config/database.js';
|
|
import { logger } from './utils/logger.js';
|
|
import jobProcessor from './services/jobProcessor.js';
|
|
import campaignsRouter from './routes/campaigns.js';
|
|
import jobsRouter from './routes/jobs.js';
|
|
|
|
// Load environment variables
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3013;
|
|
|
|
// Middleware
|
|
app.use(helmet());
|
|
app.use(cors());
|
|
app.use(express.json({ limit: '10mb' }));
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// Request logging
|
|
app.use((req, res, next) => {
|
|
logger.info(`${req.method} ${req.path}`, {
|
|
ip: req.ip,
|
|
userAgent: req.get('user-agent')
|
|
});
|
|
next();
|
|
});
|
|
|
|
// Health check
|
|
app.get('/health', (req, res) => {
|
|
res.json({
|
|
status: 'healthy',
|
|
service: 'scheduler',
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
});
|
|
|
|
// Routes
|
|
app.use('/api/scheduled-campaigns', campaignsRouter);
|
|
app.use('/api/jobs', jobsRouter);
|
|
|
|
// Error handling
|
|
app.use((err, req, res, next) => {
|
|
logger.error('Unhandled error', err);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: 'Internal server error'
|
|
});
|
|
});
|
|
|
|
// 404 handler
|
|
app.use((req, res) => {
|
|
res.status(404).json({
|
|
success: false,
|
|
error: 'Route not found'
|
|
});
|
|
});
|
|
|
|
// Start server
|
|
async function start() {
|
|
try {
|
|
// Connect to database
|
|
await connectDatabase();
|
|
|
|
// Start job processor
|
|
await jobProcessor.start();
|
|
|
|
// Start server
|
|
app.listen(PORT, () => {
|
|
logger.info(`Scheduler service running on port ${PORT}`);
|
|
});
|
|
|
|
// Graceful shutdown
|
|
process.on('SIGTERM', async () => {
|
|
logger.info('SIGTERM received, shutting down gracefully');
|
|
process.exit(0);
|
|
});
|
|
|
|
process.on('SIGINT', async () => {
|
|
logger.info('SIGINT received, shutting down gracefully');
|
|
process.exit(0);
|
|
});
|
|
} catch (error) {
|
|
logger.error('Failed to start scheduler service', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
start(); |