mirror of
https://github.com/wahyd4/heygo.git
synced 2026-08-09 05:15:52 +10:00
- POST /api/auth/dev-login creates/finds user, promotes to admin, sets session cookie - POST /api/auth/dev-logout clears session cookie - Dev login page at #/dev-login in the SPA - Fix: getSessionCookieAttributes omits Secure over HTTP (local dev fix) - Guarded: only works when PUBLIC_HOST=localhost, returns 403 otherwise - Justfile: dev-admin and dev-logout commands - README: local admin login instructions
64 lines
2.4 KiB
TypeScript
64 lines
2.4 KiB
TypeScript
import type { Env } from './env';
|
|
import { withPrivateNoStoreHeaders } from './lib/responses';
|
|
import { handleDevAuth } from './routes/api.dev-auth';
|
|
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 devAuthResponse = await handleDevAuth(request, env);
|
|
if (devAuthResponse) {
|
|
return withPrivateHostNoStoreHeaders(url, devAuthResponse, env.PRIVATE_HOST);
|
|
}
|
|
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>;
|