From 9b1aaf28028fce056dc296808b50cde43f8a8673 Mon Sep 17 00:00:00 2001 From: consultoria-as Date: Tue, 17 Feb 2026 06:36:05 +0000 Subject: [PATCH] Wire all routes, services, and middleware into index.js Import and mount all API route handlers (facturas, articulos, catalogo, config, print), initialize the SQLite database, start the XML file watcher, and serve static files with SPA fallback. Add placeholder index.html for the frontend shell. Use Express 5 '*path' wildcard syntax for the catch-all route. Co-Authored-By: Claude Opus 4.6 --- src/index.js | 35 ++++++++++++++++++++++++++++++- src/public/index.html | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/public/index.html diff --git a/src/index.js b/src/index.js index 344dc12..f991750 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,47 @@ const express = require('express'); const path = require('path'); +const { createDatabase } = require('./services/database'); +const { startWatcher } = require('./services/watcher'); +const { facturasRouter } = require('./routes/facturas'); +const { articulosRouter } = require('./routes/articulos'); +const { catalogoRouter } = require('./routes/catalogo'); +const { configRouter } = require('./routes/config'); +const { printRouter } = require('./routes/print'); const app = express(); const PORT = process.env.PORT || 3000; +const DB_PATH = process.env.DB_PATH || path.join(__dirname, '..', 'data', 'refaccionaria.db'); +// Initialize database +const db = createDatabase(DB_PATH); + +// Middleware app.use(express.json()); app.use(express.static(path.join(__dirname, 'public'))); +// API Routes +app.use('/api/facturas', facturasRouter(db)); +app.use('/api/articulos', articulosRouter(db)); +app.use('/api/catalogo', catalogoRouter(db)); +app.use('/api/config', configRouter(db)); +app.use('/api/print', printRouter(db)); + +// SPA fallback — serve index.html for non-API routes +app.get('*path', (req, res) => { + if (!req.path.startsWith('/api')) { + res.sendFile(path.join(__dirname, 'public', 'index.html')); + } +}); + +// Start file watcher +const xmlFolder = db.prepare("SELECT valor FROM configuracion WHERE clave = 'xml_folder'").get(); +if (xmlFolder) { + const folderPath = path.resolve(xmlFolder.valor); + startWatcher(db, folderPath); +} + app.listen(PORT, '0.0.0.0', () => { - console.log(`Portal running at http://0.0.0.0:${PORT}`); + console.log(`Portal Refaccionaria running at http://0.0.0.0:${PORT}`); }); module.exports = app; diff --git a/src/public/index.html b/src/public/index.html new file mode 100644 index 0000000..9fb4e7b --- /dev/null +++ b/src/public/index.html @@ -0,0 +1,48 @@ + + + + + + Portal Refaccionaria + + + + + +
+
+
+

Cargando...

+
+
+ +
+ + + +