100 lines
2.6 KiB
JavaScript
100 lines
2.6 KiB
JavaScript
|
|
require('dotenv').config();
|
||
|
|
const express = require('express');
|
||
|
|
const session = require('express-session');
|
||
|
|
const helmet = require('helmet');
|
||
|
|
const cors = require('cors');
|
||
|
|
const logger = require('./config/logger');
|
||
|
|
const sessionConfig = require('./config/session');
|
||
|
|
const { testConnection } = require('./config/database');
|
||
|
|
const errorHandler = require('./middlewares/errorHandler');
|
||
|
|
const { apiLimiter } = require('./middlewares/rateLimiter');
|
||
|
|
|
||
|
|
const app = express();
|
||
|
|
const PORT = process.env.PORT || 3000;
|
||
|
|
|
||
|
|
// Security middleware
|
||
|
|
app.use(helmet());
|
||
|
|
app.use(cors({
|
||
|
|
origin: process.env.FRONTEND_URL || 'http://localhost:3001',
|
||
|
|
credentials: true,
|
||
|
|
}));
|
||
|
|
|
||
|
|
// Body parsing middleware
|
||
|
|
app.use(express.json());
|
||
|
|
app.use(express.urlencoded({ extended: true }));
|
||
|
|
|
||
|
|
// Serve static files (landing page)
|
||
|
|
app.use(express.static('src/public'));
|
||
|
|
|
||
|
|
// Session middleware
|
||
|
|
app.use(session(sessionConfig));
|
||
|
|
|
||
|
|
// Rate limiting
|
||
|
|
app.use('/api', apiLimiter);
|
||
|
|
|
||
|
|
// 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({
|
||
|
|
success: true,
|
||
|
|
message: 'Server is running',
|
||
|
|
timestamp: new Date().toISOString(),
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
// API Routes
|
||
|
|
app.use('/api/auth', require('./routes/auth.routes'));
|
||
|
|
app.use('/api/companies', require('./routes/company.routes'));
|
||
|
|
app.use('/api/tokens', require('./routes/token.routes'));
|
||
|
|
app.use('/api/templates', require('./routes/template.routes'));
|
||
|
|
app.use('/api/settings', require('./routes/settings.routes'));
|
||
|
|
app.use('/api/stats', require('./routes/stats.routes'));
|
||
|
|
|
||
|
|
// Public tracking route (no rate limit on this specific route)
|
||
|
|
app.use('/t', require('./routes/tracking.routes'));
|
||
|
|
|
||
|
|
// 404 handler
|
||
|
|
app.use((req, res) => {
|
||
|
|
res.status(404).json({
|
||
|
|
success: false,
|
||
|
|
error: 'Endpoint not found',
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
// Error handler (must be last)
|
||
|
|
app.use(errorHandler);
|
||
|
|
|
||
|
|
// Start server
|
||
|
|
const startServer = async () => {
|
||
|
|
try {
|
||
|
|
// Test database connection
|
||
|
|
await testConnection();
|
||
|
|
|
||
|
|
// Start listening
|
||
|
|
app.listen(PORT, () => {
|
||
|
|
logger.info(`🚀 Server is running on port ${PORT}`);
|
||
|
|
logger.info(`📊 Environment: ${process.env.NODE_ENV || 'development'}`);
|
||
|
|
logger.info(`🔗 Health check: http://localhost:${PORT}/health`);
|
||
|
|
console.log(`\n✨ Oltalama Backend Server Started!`);
|
||
|
|
console.log(`🌐 API: http://localhost:${PORT}/api`);
|
||
|
|
console.log(`🎯 Tracking: http://localhost:${PORT}/t/:token\n`);
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
logger.error('Failed to start server:', error);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
startServer();
|
||
|
|
|
||
|
|
module.exports = app;
|
||
|
|
|