59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const helmet = require('helmet');
|
|
const compression = require('compression');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// Security middleware
|
|
app.use(helmet({ contentSecurityPolicy: false }));
|
|
app.use(compression());
|
|
app.use(cors({
|
|
origin: process.env.FRONTEND_URL || true,
|
|
credentials: true
|
|
}));
|
|
app.use(express.json({ limit: '10mb' }));
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// Database connection
|
|
const { connectDB } = require('./src/db/connection');
|
|
|
|
// Routes
|
|
const authRoutes = require('./src/routes/auth');
|
|
const craftsmenRoutes = require('./src/routes/craftsmen');
|
|
const productsRoutes = require('./src/routes/products');
|
|
const ordersRoutes = require('./src/routes/orders');
|
|
|
|
app.use('/api/auth', authRoutes);
|
|
app.use('/api/craftsmen', craftsmenRoutes);
|
|
app.use('/api/products', productsRoutes);
|
|
app.use('/api/orders', ordersRoutes);
|
|
|
|
// Health check
|
|
app.get('/api/health', (req, res) => {
|
|
res.json({ status: 'ok', service: 'shokuninmarche-api', timestamp: new Date().toISOString() });
|
|
});
|
|
|
|
// Serve React frontend in production
|
|
app.use(express.static(path.join(__dirname, 'client/dist')));
|
|
app.get('*', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'client/dist', 'index.html'));
|
|
});
|
|
|
|
// Start server
|
|
async function start() {
|
|
try {
|
|
await connectDB();
|
|
app.listen(PORT, () => {
|
|
console.log(`🚀 shokuninmarche server running on port ${PORT}`);
|
|
});
|
|
} catch (err) {
|
|
console.error('Failed to start server:', err);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
start();
|