feat: add http endpoint to trigger backfill script remotely
All checks were successful
Build and Deploy / build-and-push (push) Successful in 59s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 59s
This commit is contained in:
24
src/index.ts
24
src/index.ts
@@ -1,6 +1,8 @@
|
|||||||
import express, { Express, Request, Response } from 'express';
|
import express, { Express, Request, Response } from 'express';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import webhookRoutes from './routes/webhook.route';
|
import webhookRoutes from './routes/webhook.route';
|
||||||
|
import { spawn } from 'child_process';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
dotenv.config();
|
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.' });
|
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, () => {
|
app.listen(port, () => {
|
||||||
console.log(`[server]: Middleware server is running at http://localhost:${port}`);
|
console.log(`[server]: Middleware server is running at http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user