164 lines
5.0 KiB
JavaScript
164 lines
5.0 KiB
JavaScript
import dotenv from 'dotenv';
|
|
import express from 'express';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
import { fileURLToPath } from 'url';
|
|
import cors from 'cors';
|
|
import { dbReady } from './db/index.js';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
dotenv.config({ path: path.join(__dirname, '.env') });
|
|
import { getProducts } from './routes/products.js';
|
|
import { getBlockTypes } from './routes/blockTypes.js';
|
|
import { getFinishes } from './routes/finishes.js';
|
|
import { getServiceOptions } from './routes/serviceOptions.js';
|
|
import { getSettings } from './routes/settings.js';
|
|
import { postPrice } from './routes/price.js';
|
|
import { getOrders, postOrder } from './routes/orders.js';
|
|
import {
|
|
triggerFormexSync,
|
|
streamFormexSync,
|
|
getLogs,
|
|
getStatus,
|
|
getFormexMaterials,
|
|
getFormexMaterial,
|
|
createFormexMaterial,
|
|
updateFormexMaterial,
|
|
deleteFormexMaterial,
|
|
getFormexCollections,
|
|
getFormexCategories
|
|
} from './routes/sync.js';
|
|
import {
|
|
uploadMiddleware,
|
|
analyzeRoom,
|
|
saveRoomConfig,
|
|
getRoomConfig,
|
|
checkApiStatus
|
|
} from './routes/room.js';
|
|
import {
|
|
optimizeLayoutRoute,
|
|
quickOptimizeRoute,
|
|
suggestLayoutRoute
|
|
} from './routes/layout.js';
|
|
import {
|
|
getModules,
|
|
getModule,
|
|
getModuleCategories,
|
|
getHardware,
|
|
getHardwareTypes,
|
|
getHandles,
|
|
getHinges
|
|
} from './routes/modules.js';
|
|
import {
|
|
generatePlansRoute,
|
|
generateCuttingListRoute,
|
|
exportCuttingListText,
|
|
exportPDFHtml
|
|
} from './routes/plans.js';
|
|
import {
|
|
designTipsRoute,
|
|
recommendMaterialsRoute,
|
|
renderRoute
|
|
} from './routes/ai.js';
|
|
import { initScheduler } from './services/scheduler.js';
|
|
|
|
const clientDist = path.join(__dirname, '..', 'client', 'dist');
|
|
const hasClientBuild = fs.existsSync(clientDist);
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3001;
|
|
|
|
app.use(cors({ origin: true }));
|
|
app.use(express.json());
|
|
|
|
app.get('/api/products', getProducts);
|
|
app.get('/api/block-types', getBlockTypes);
|
|
app.get('/api/finishes', getFinishes);
|
|
app.get('/api/service-options', getServiceOptions);
|
|
app.get('/api/settings', getSettings);
|
|
app.post('/api/price', postPrice);
|
|
app.get('/api/orders', getOrders);
|
|
app.post('/api/orders', postOrder);
|
|
|
|
// Sync routes (Formex crawler)
|
|
app.post('/api/sync/formex', triggerFormexSync);
|
|
app.get('/api/sync/formex/stream', streamFormexSync);
|
|
app.get('/api/sync/logs', getLogs);
|
|
app.get('/api/sync/status', getStatus);
|
|
|
|
// Formex materials CRUD
|
|
app.get('/api/formex/materials', getFormexMaterials);
|
|
app.get('/api/formex/materials/:id', getFormexMaterial);
|
|
app.post('/api/formex/materials', createFormexMaterial);
|
|
app.put('/api/formex/materials/:id', updateFormexMaterial);
|
|
app.delete('/api/formex/materials/:id', deleteFormexMaterial);
|
|
app.get('/api/formex/collections', getFormexCollections);
|
|
app.get('/api/formex/categories', getFormexCategories);
|
|
|
|
// Room analysis routes (AI-powered)
|
|
app.post('/api/room/analyze', uploadMiddleware, analyzeRoom);
|
|
app.post('/api/room/save', saveRoomConfig);
|
|
app.get('/api/room/config/:orderId', getRoomConfig);
|
|
app.get('/api/room/status', checkApiStatus);
|
|
|
|
// Layout optimization routes (AI-powered)
|
|
app.post('/api/layout/optimize', optimizeLayoutRoute);
|
|
app.post('/api/layout/quick-optimize', quickOptimizeRoute);
|
|
app.get('/api/layout/suggest', suggestLayoutRoute);
|
|
|
|
// Modules catalog routes
|
|
app.get('/api/modules', getModules);
|
|
app.get('/api/modules/categories', getModuleCategories);
|
|
app.get('/api/modules/:id', getModule);
|
|
|
|
// Hardware routes
|
|
app.get('/api/hardware', getHardware);
|
|
app.get('/api/hardware/types', getHardwareTypes);
|
|
app.get('/api/handles', getHandles);
|
|
app.get('/api/hinges', getHinges);
|
|
|
|
// AI design assistant routes
|
|
app.post('/api/ai/design-tips', designTipsRoute);
|
|
app.post('/api/ai/recommend-materials', recommendMaterialsRoute);
|
|
app.post('/api/ai/render', renderRoute);
|
|
|
|
// Plans and cutting list routes
|
|
app.post('/api/plans/generate', generatePlansRoute);
|
|
app.post('/api/plans/cutting-list', generateCuttingListRoute);
|
|
app.post('/api/plans/export-text', exportCuttingListText);
|
|
app.post('/api/plans/export-pdf', exportPDFHtml);
|
|
|
|
if (hasClientBuild) {
|
|
app.use(express.static(clientDist));
|
|
app.get('*', (req, res) => {
|
|
res.sendFile(path.join(clientDist, 'index.html'));
|
|
});
|
|
} else {
|
|
app.get('/', (req, res) => {
|
|
res.send(`
|
|
<!DOCTYPE html>
|
|
<html><head><meta charset="utf-8"><title>Configurateur</title></head>
|
|
<body style="font-family:sans-serif;padding:2rem;">
|
|
<h1>Application non construite</h1>
|
|
<p>Depuis la racine du projet, exécutez :</p>
|
|
<pre>npm run build</pre>
|
|
<p>Puis rechargez cette page. Ou lancez le client en mode dev :</p>
|
|
<pre>npm run client</pre>
|
|
<p>et ouvrez <a href="http://localhost:5173">http://localhost:5173</a>.</p>
|
|
</body></html>
|
|
`);
|
|
});
|
|
}
|
|
|
|
await dbReady;
|
|
|
|
// Initialize the scheduler for automated sync
|
|
initScheduler();
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`API configurateur armoires — http://localhost:${PORT}`);
|
|
if (!hasClientBuild) {
|
|
console.log('(Exécutez "npm run build" pour servir l\'app sur ce port)');
|
|
}
|
|
});
|