mirror of
https://github.com/wahyd4/heygo.git
synced 2026-08-08 21:05:48 +10:00
- Terraform configs for D1 databases, KV namespaces, Worker custom domains - wrangler.dev.jsonc and wrangler.prod.jsonc for environment-specific deployments - Worker code refactored to use env vars for host checking (PUBLIC_HOST, PRIVATE_HOST) - Configurable app URLs and cookie domain via env vars - Deploy and migrate npm scripts for dev/prod - Updated all tests with new env fixtures - Deployment guide in README
59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
import type { Env } from './env';
|
|
import { withPrivateNoStoreHeaders } from './lib/responses';
|
|
import { handleLinksApi } from './routes/api.links';
|
|
import { handlePromotionsApi } from './routes/api.promotions';
|
|
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,
|
|
},
|
|
});
|
|
}
|
|
|
|
function withPrivateHostNoStoreHeaders(url: URL, response: Response, privateHost: string): Response {
|
|
return isHeygoPrivateHost(url.hostname, privateHost) ? withPrivateNoStoreHeaders(response) : response;
|
|
}
|
|
|
|
export default {
|
|
async fetch(request, env, ctx): Promise<Response> {
|
|
const url = new URL(request.url);
|
|
|
|
if (url.pathname === '/api/health') {
|
|
return withPrivateHostNoStoreHeaders(url, json({ ok: true, service: 'heygo-worker' }), env.PRIVATE_HOST);
|
|
}
|
|
|
|
if (url.pathname.startsWith('/api/')) {
|
|
const apiResponse = await handleLinksApi(request, env);
|
|
if (apiResponse) {
|
|
return withPrivateHostNoStoreHeaders(url, apiResponse, env.PRIVATE_HOST);
|
|
}
|
|
const promotionsResponse = await handlePromotionsApi(request, env);
|
|
if (promotionsResponse) {
|
|
return withPrivateHostNoStoreHeaders(url, promotionsResponse, env.PRIVATE_HOST);
|
|
}
|
|
return withPrivateHostNoStoreHeaders(url, json({ error: 'Not found' }, { status: 404 }), env.PRIVATE_HOST);
|
|
}
|
|
|
|
if (isHeygoPublicHost(url.hostname, env.PUBLIC_HOST) && !isReservedPublicPath(url.pathname)) {
|
|
return handlePublicShortlink(request, env, ctx);
|
|
}
|
|
|
|
if (isHeygoPrivateHost(url.hostname, env.PRIVATE_HOST) && !isReservedPublicPath(url.pathname)) {
|
|
return handlePrivateShortlink(request, env, ctx);
|
|
}
|
|
|
|
return withPrivateHostNoStoreHeaders(url, json({ error: 'Not found' }, { status: 404 }), env.PRIVATE_HOST);
|
|
},
|
|
} satisfies ExportedHandler<Env>;
|