131 lines
3.5 KiB
JavaScript
131 lines
3.5 KiB
JavaScript
require('dotenv').config();
|
|
// import uploadRoutes from "./routes/upload.routes";
|
|
const uploadRoutes = require("./routes/upload.routes");
|
|
|
|
const express = require('express');
|
|
const cors = require('cors');
|
|
const helmet = require('helmet');
|
|
const morgan = require('morgan');
|
|
const cookieParser = require('cookie-parser');
|
|
|
|
const {
|
|
initializeDatabases,
|
|
closeDatabaseConnections,
|
|
} = require('./config/database');
|
|
const { errorHandler, notFound } = require('./middleware/errorHandler');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// Security middleware
|
|
app.use(
|
|
helmet({
|
|
contentSecurityPolicy: false, // Disable for API
|
|
crossOriginEmbedderPolicy: false,
|
|
})
|
|
);
|
|
|
|
// CORS configuration
|
|
const corsOptions = {
|
|
origin: process.env.CORS_ORIGIN?.split(',') || [
|
|
'http://localhost:3000',
|
|
'http://localhost:3001',
|
|
'http://localhost:5173',
|
|
'http://localhost:5174',
|
|
],
|
|
credentials: true,
|
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
|
|
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
|
|
};
|
|
|
|
app.use(cors(corsOptions));
|
|
|
|
// Body parsing middleware
|
|
app.use(express.json({ limit: '10mb' }));
|
|
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
|
|
app.use(cookieParser());
|
|
|
|
// Logging middleware
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
app.use(morgan('combined'));
|
|
}
|
|
|
|
// Health check endpoint
|
|
app.get('/health', (req, res) => {
|
|
res.status(200).json({
|
|
status: 'OK',
|
|
timestamp: new Date().toISOString(),
|
|
uptime: process.uptime(),
|
|
environment: process.env.NODE_ENV,
|
|
});
|
|
});
|
|
|
|
// API Routes
|
|
app.use('/api/auth', require('./routes/auth'));
|
|
app.use('/api/users', require('./routes/users'));
|
|
app.use('/api/products', require('./routes/products'));
|
|
app.use('/api/orders', require('./routes/orders'));
|
|
app.use('/api/wardrobe', require('./routes/wardrobe'));
|
|
app.use('/api/delivery', require('./routes/deliveryRoutes'));
|
|
app.use('/api/coupons', require('./routes/couponRoutes'));
|
|
app.use('/api/admin', require('./routes/admin'));
|
|
|
|
app.use('/api/admin/reports', require('./routes/reports'));
|
|
app.use('/api/payments', require('./routes/paymentRoutes'));
|
|
// Upload route
|
|
app.use("/api", uploadRoutes);
|
|
|
|
|
|
// Root endpoint
|
|
app.get('/', (req, res) => {
|
|
res.json({
|
|
message: 'Vaishnavi Creation API',
|
|
version: '1.0.0',
|
|
documentation: '/api/docs',
|
|
health: '/health',
|
|
});
|
|
});
|
|
|
|
// Error handling middleware (must be last)
|
|
app.use(notFound);
|
|
app.use(errorHandler);
|
|
|
|
// Graceful shutdown
|
|
process.on('SIGTERM', async () => {
|
|
console.log('SIGTERM received, shutting down gracefully');
|
|
await closeDatabaseConnections();
|
|
process.exit(0);
|
|
});
|
|
|
|
process.on('SIGINT', async () => {
|
|
console.log('SIGINT received, shutting down gracefully');
|
|
await closeDatabaseConnections();
|
|
process.exit(0);
|
|
});
|
|
|
|
// Start server
|
|
const startServer = async () => {
|
|
try {
|
|
// Initialize database connections
|
|
await initializeDatabases();
|
|
|
|
// Start the server
|
|
app.listen(PORT, () => {
|
|
console.log(`🚀 Server running on port ${PORT}`);
|
|
console.log(`📚 API Documentation: http://localhost:${PORT}/api/docs`);
|
|
console.log(`🏥 Health Check: http://localhost:${PORT}/health`);
|
|
console.log(`🌍 Environment: ${process.env.NODE_ENV}`);
|
|
});
|
|
} catch (error) {
|
|
console.error('❌ Failed to start server:', error);
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
// Only start server if this file is run directly
|
|
if (require.main === module) {
|
|
startServer();
|
|
}
|
|
|
|
module.exports = app;
|