From d317a5041c6736e221c0501eeb1bf83ec045d104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Thu, 7 May 2026 17:17:51 -0300 Subject: [PATCH] feat: add http endpoint to trigger backfill script remotely --- src/index.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/index.ts b/src/index.ts index bb01aa9..bdef486 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,8 @@ import express, { Express, Request, Response } from 'express'; import dotenv from 'dotenv'; import webhookRoutes from './routes/webhook.route'; +import { spawn } from 'child_process'; +import path from 'path'; dotenv.config(); @@ -16,6 +18,28 @@ app.get('/health', (req: Request, res: Response) => { res.status(200).json({ status: 'OK', message: 'Tiny-n8n middleware is running.' }); }); +// Hidden endpoint to trigger the backfill script without terminal access +app.get('/api/trigger-backfill', (req: Request, res: Response) => { + const expectedToken = process.env.TINY_WEBHOOK_SECRET; + if (expectedToken && req.query.token !== expectedToken) { + res.status(401).json({ error: 'Unauthorized' }); + return; + } + + const scriptPath = path.join(__dirname, 'scripts', 'backfill.js'); + + // Spawn as a detached background process so it doesn't block the web server + const child = spawn('node', [scriptPath], { + detached: true, + stdio: 'ignore' + }); + + child.unref(); + + console.log('[server]: Backfill script manually triggered via HTTP endpoint.'); + res.status(200).json({ status: 'STARTED', message: 'Backfill script is now running quietly in the background.' }); +}); + app.listen(port, () => { console.log(`[server]: Middleware server is running at http://localhost:${port}`); });