Skip to content

Commit 093386b

Browse files
committed
Adds CDN proxy to address CORS
Proxies requests to the CDN, enabling resource retrieval without cross-origin issues. Sets necessary headers to allow frontend access.
1 parent 908e5bc commit 093386b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

server/src/webhook.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,40 @@ export function registerWebhookRoutes(app: express.Application) {
443443
const keys = Array.from(eventStore.keys());
444444
return res.json({ keys, count: keys.length });
445445
});
446+
447+
// CDN Proxy: Forward requests to cdn.trackmangolf.com to avoid CORS issues
448+
app.get('/cdn-proxy/*', async (req: Request, res: Response) => {
449+
try {
450+
const cdnPath = req.params[0]; // Everything after /cdn-proxy/
451+
const cdnUrl = `https://cdn.trackmangolf.com/${cdnPath}`;
452+
453+
console.log(`[CDN Proxy] Fetching: ${cdnUrl}`);
454+
455+
const response = await fetch(cdnUrl);
456+
457+
if (!response.ok) {
458+
console.error(`[CDN Proxy] Failed to fetch ${cdnUrl}: ${response.status}`);
459+
return res.status(response.status).send(`Failed to fetch from CDN: ${response.statusText}`);
460+
}
461+
462+
// Forward the content type from the CDN
463+
const contentType = response.headers.get('content-type');
464+
if (contentType) {
465+
res.setHeader('Content-Type', contentType);
466+
}
467+
468+
// Set CORS headers to allow frontend access
469+
res.setHeader('Access-Control-Allow-Origin', '*');
470+
res.setHeader('Access-Control-Allow-Methods', 'GET');
471+
472+
// Stream the response body
473+
const buffer = await response.arrayBuffer();
474+
res.send(Buffer.from(buffer));
475+
} catch (err) {
476+
console.error('[CDN Proxy] Error:', err);
477+
res.status(500).send('Failed to proxy CDN request');
478+
}
479+
});
446480
}
447481

448482
export { eventStore, lastWebhookDiagnostics, sseClients };

0 commit comments

Comments
 (0)