Run scheduled tasks through application database
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { createHash, timingSafeEqual } from 'node:crypto';
|
||||
import { env } from '$env/dynamic/private';
|
||||
import { runScheduledNotificationTasks } from '$lib/server/scheduled-notifications';
|
||||
import { json } from '@sveltejs/kit';
|
||||
|
||||
function tokenMatches(header: string | null) {
|
||||
const configured = env.SCHEDULER_TOKEN;
|
||||
const provided = header?.startsWith('Bearer ') ? header.slice(7) : '';
|
||||
if (!configured || !provided) return false;
|
||||
const expectedHash = createHash('sha256').update(configured).digest();
|
||||
const providedHash = createHash('sha256').update(provided).digest();
|
||||
return timingSafeEqual(expectedHash, providedHash);
|
||||
}
|
||||
|
||||
export async function POST({ request }) {
|
||||
if (!tokenMatches(request.headers.get('authorization'))) {
|
||||
return json({ error: 'Not found' }, { status: 404 });
|
||||
}
|
||||
return json({ status: 'ok', ...(await runScheduledNotificationTasks()) });
|
||||
}
|
||||
Reference in New Issue
Block a user