File size: 3,237 Bytes
1a81036 15bd029 1a81036 | 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import 'dotenv/config';
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import rateLimit from 'express-rate-limit';
import path from 'path';
import { fileURLToPath } from 'url';
import connectDB from './config/db.js';
import { seedAdmin } from './config/seed.js';
// Routes
import authRoutes from './routes/auth.js';
import productRoutes from './routes/products.js';
import orderRoutes from './routes/orders.js';
import customerRoutes from './routes/customers.js';
import staffRoutes from './routes/staff.js';
import categoryRoutes from './routes/categories.js';
import uploadRoutes from './routes/upload.js';
import analyticsRoutes from './routes/analytics.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 7860;
app.set('trust proxy', 1);
// Connect to MongoDB
await connectDB();
await seedAdmin();
// Security middleware
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'",
"cdn.jsdelivr.net", "unpkg.com", "cdnjs.cloudflare.com"],
styleSrc: ["'self'", "'unsafe-inline'", "fonts.googleapis.com",
"cdn.jsdelivr.net", "cdnjs.cloudflare.com"],
fontSrc: ["'self'", "fonts.gstatic.com", "cdn.jsdelivr.net"],
imgSrc: ["'self'", "data:", "blob:", "*.r2.dev", "*.cloudflare.com"],
connectSrc: ["'self'", "*.r2.dev"],
mediaSrc: ["'self'", "blob:"],
workerSrc: ["'self'", "blob:"],
},
},
}));
app.use(cors({
origin: process.env.NODE_ENV === 'production' ? false : '*',
credentials: true,
}));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 200,
standardHeaders: true,
legacyHeaders: false,
});
app.use('/api/', limiter);
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// Serve static files
app.use(express.static(path.join(__dirname, '..', 'public')));
// API Routes
app.use('/api/auth', authRoutes);
app.use('/api/products', productRoutes);
app.use('/api/orders', orderRoutes);
app.use('/api/customers', customerRoutes);
app.use('/api/staff', staffRoutes);
app.use('/api/categories', categoryRoutes);
app.use('/api/upload', uploadRoutes);
app.use('/api/analytics', analyticsRoutes);
// Health check
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
store: process.env.STORE_NAME || 'Woodlands Supermarket',
timestamp: new Date().toISOString()
});
});
// SPA fallback - serve index.html for all non-API routes
app.get('*', (req, res) => {
if (!req.path.startsWith('/api/')) {
res.sendFile(path.join(__dirname, '..', 'public', 'index.html'));
}
});
// Global error handler
app.use((err, req, res, next) => {
console.error('Server Error:', err.stack);
res.status(err.status || 500).json({
success: false,
message: err.message || 'Internal server error',
});
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`🌿 Woodlands Supermarket server running on port ${PORT}`);
console.log(`📦 Environment: ${process.env.NODE_ENV || 'development'}`);
});
|