2026-03-07 20:00:57 +01:00
|
|
|
import type { APIRoute } from 'astro';
|
2026-03-07 22:03:01 +01:00
|
|
|
import { createHmac, timingSafeEqual } from 'node:crypto';
|
2026-03-07 20:00:57 +01:00
|
|
|
|
2026-03-07 22:03:01 +01:00
|
|
|
export const POST: APIRoute = async ({ request }) => {
|
|
|
|
|
const webhookSecret = import.meta.env.STORYBLOK_WEBHOOK_SECRET;
|
2026-03-07 20:00:57 +01:00
|
|
|
const token = import.meta.env.CLEVER_TOKEN;
|
|
|
|
|
const appId = import.meta.env.CLEVER_APP_ID_PRODUCTION;
|
|
|
|
|
|
2026-03-07 22:03:01 +01:00
|
|
|
if (!webhookSecret || !token || !appId) {
|
|
|
|
|
return new Response('Missing server configuration', { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const body = await request.text();
|
|
|
|
|
const signature = request.headers.get('webhook-signature') ?? '';
|
|
|
|
|
const expected = createHmac('sha1', webhookSecret).update(body).digest('hex');
|
|
|
|
|
|
|
|
|
|
if (!timingSafeEqual(Buffer.from(signature), Buffer.from(expected))) {
|
|
|
|
|
return new Response('Invalid signature', { status: 401 });
|
2026-03-07 20:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await fetch(
|
2026-03-07 22:11:08 +01:00
|
|
|
`https://api-bridge.clever-cloud.com/v2/self/applications/${appId}/instances`,
|
2026-03-07 20:00:57 +01:00
|
|
|
{
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
2026-03-07 22:03:01 +01:00
|
|
|
const error = await response.text();
|
|
|
|
|
return new Response(`Clever Cloud API error: ${response.status} ${error}`, { status: 502 });
|
2026-03-07 20:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Response('Rebuild triggered', { status: 200 });
|
|
|
|
|
};
|