feat: add http endpoint to trigger backfill script remotely
All checks were successful
Build and Deploy / build-and-push (push) Successful in 59s

This commit is contained in:
Cauê Faleiros
2026-05-07 17:17:51 -03:00
parent 7dfb3d4a03
commit d317a5041c

View File

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