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 <noreply@anthropic.com>
This commit is contained in:
35
src/index.js
35
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;
|
||||
|
||||
48
src/public/index.html
Normal file
48
src/public/index.html
Normal file
@@ -0,0 +1,48 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Portal Refaccionaria</title>
|
||||
<link rel="stylesheet" href="/css/styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<aside class="sidebar" id="sidebar">
|
||||
<div class="sidebar-header">
|
||||
<h1 class="sidebar-title">Portal Refaccionaria</h1>
|
||||
</div>
|
||||
<nav class="sidebar-nav">
|
||||
<a href="#facturas" class="nav-link" data-route="facturas">
|
||||
<span class="nav-icon">📄</span>
|
||||
<span class="nav-text">Facturas</span>
|
||||
</a>
|
||||
<a href="#articulos" class="nav-link" data-route="articulos">
|
||||
<span class="nav-icon">🔍</span>
|
||||
<span class="nav-text">Artículos</span>
|
||||
</a>
|
||||
<a href="#catalogo" class="nav-link" data-route="catalogo">
|
||||
<span class="nav-icon">📋</span>
|
||||
<span class="nav-text">Catálogo</span>
|
||||
</a>
|
||||
<a href="#config" class="nav-link" data-route="config">
|
||||
<span class="nav-icon">⚙️</span>
|
||||
<span class="nav-text">Configuración</span>
|
||||
</a>
|
||||
</nav>
|
||||
<div class="sidebar-footer">
|
||||
<small>v1.0 — Refaccionaria</small>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<main class="content" id="content">
|
||||
<div class="loading-screen">
|
||||
<div class="spinner"></div>
|
||||
<p>Cargando...</p>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<div class="toast-container" id="toastContainer"></div>
|
||||
|
||||
<script src="/js/app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user