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}`); });