78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import 'dotenv/config';
|
|
import express from 'express';
|
|
import cors from 'cors';
|
|
import path from 'path';
|
|
import { initDatabase } from './config/database';
|
|
|
|
// Import routes
|
|
import authRoutes from './routes/auth';
|
|
import invoiceRoutes from './routes/invoices';
|
|
import supplierRoutes from './routes/suppliers';
|
|
import purchaseOrderRoutes from './routes/purchaseOrders';
|
|
import exportRoutes from './routes/exports';
|
|
import dashboardRoutes from './routes/dashboard';
|
|
import notificationRoutes from './routes/notifications';
|
|
|
|
const app = express();
|
|
const PORT = parseInt(process.env.PORT || '3001', 10);
|
|
|
|
// Middleware
|
|
app.use(cors({
|
|
origin: process.env.FRONTEND_URL || '*',
|
|
credentials: true,
|
|
}));
|
|
app.use(express.json({ limit: '50mb' }));
|
|
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
|
|
|
|
// Serve uploaded files
|
|
const uploadDir = process.env.UPLOAD_DIR || './uploads';
|
|
app.use('/uploads', express.static(path.resolve(uploadDir)));
|
|
|
|
// API Routes
|
|
app.use('/api/auth', authRoutes);
|
|
app.use('/api/invoices', invoiceRoutes);
|
|
app.use('/api/suppliers', supplierRoutes);
|
|
app.use('/api/purchase-orders', purchaseOrderRoutes);
|
|
app.use('/api/exports', exportRoutes);
|
|
app.use('/api/dashboard', dashboardRoutes);
|
|
app.use('/api/notifications', notificationRoutes);
|
|
|
|
// Health check
|
|
app.get('/api/health', (req, res) => {
|
|
res.json({ status: 'ok', timestamp: new Date().toISOString(), version: '1.0.0' });
|
|
});
|
|
|
|
// Serve frontend in production
|
|
if (process.env.NODE_ENV === 'production') {
|
|
const frontendPath = path.join(__dirname, '../../frontend/dist');
|
|
app.use(express.static(frontendPath));
|
|
app.get('*', (req, res) => {
|
|
if (!req.path.startsWith('/api')) {
|
|
res.sendFile(path.join(frontendPath, 'index.html'));
|
|
}
|
|
});
|
|
}
|
|
|
|
// Error handling middleware
|
|
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
console.error('Erreur non gérée:', err);
|
|
res.status(500).json({ error: 'Erreur interne du serveur' });
|
|
});
|
|
|
|
// Start server
|
|
async function start() {
|
|
try {
|
|
await initDatabase();
|
|
|
|
app.listen(PORT, '0.0.0.0', () => {
|
|
console.log(`🚀 Serveur Facturation SANTINOVA démarré sur le port ${PORT}`);
|
|
console.log(`📊 API disponible sur http://localhost:${PORT}/api`);
|
|
});
|
|
} catch (error) {
|
|
console.error('❌ Erreur au démarrage:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
start();
|