Files
heygo/worker/index.ts
T

44 lines
1.2 KiB
TypeScript

import type { Env } from './env';
import { handlePrivateShortlink, isHeygoPrivateHost } from './routes/private-redirect';
import { handlePublicShortlink, isHeygoPublicHost, isReservedPublicPath } from './routes/redirect';
export type { Env } from './env';
const jsonHeaders = {
'content-type': 'application/json; charset=utf-8',
};
function json(body: unknown, init: ResponseInit = {}) {
return Response.json(body, {
...init,
headers: {
...jsonHeaders,
...init.headers,
},
});
}
export default {
async fetch(request, env, ctx): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === '/api/health') {
return json({ ok: true, service: 'heygo-worker' });
}
if (url.pathname.startsWith('/api/')) {
return json({ error: 'Not found' }, { status: 404 });
}
if (isHeygoPublicHost(url) && !isReservedPublicPath(url.pathname)) {
return handlePublicShortlink(request, env, ctx);
}
if (isHeygoPrivateHost(url) && !isReservedPublicPath(url.pathname)) {
return handlePrivateShortlink(request, env, ctx);
}
return json({ error: 'Not found' }, { status: 404 });
},
} satisfies ExportedHandler<Env>;