fix
This commit is contained in:
parent
5bf02b47d5
commit
102f78b523
|
|
@ -40,15 +40,17 @@ function isAuthorised(page: { id: string; password_hash: string | null }, req: R
|
|||
return verifyAuthCookie(req.headers.get("cookie"), page.id);
|
||||
}
|
||||
|
||||
const app = new Elysia()
|
||||
.get("/", () => new Response("PingQL status service", {
|
||||
headers: { "content-type": "text/plain" },
|
||||
}))
|
||||
// Memoirist (Elysia's router) treats `:slug.json` as a single parameter named
|
||||
// "slug.json" and refuses to coexist with `:slug`. Instead, use one `:slug`
|
||||
// route and dispatch on the suffix in the handler.
|
||||
function splitSlugAndFormat(raw: string): { slug: string; format: "html" | "json" | "rss" } {
|
||||
if (raw.endsWith(".json")) return { slug: raw.slice(0, -5), format: "json" };
|
||||
if (raw.endsWith(".rss")) return { slug: raw.slice(0, -4), format: "rss" };
|
||||
return { slug: raw, format: "html" };
|
||||
}
|
||||
|
||||
// Public HTML page
|
||||
.get("/:slug", async ({ params, request, set }) => {
|
||||
if (!allow(params.slug, clientIp(request))) return rateLimited();
|
||||
const page = await cached(`page:${params.slug}`, 60, () => loadStatusPage(params.slug));
|
||||
async function renderHtml(slug: string, request: Request): Promise<Response> {
|
||||
const page = await cached(`page:${slug}`, 60, () => loadStatusPage(slug));
|
||||
if (!page) return notFound();
|
||||
if (!isAuthorised(page, request)) {
|
||||
return new Response(eta.render("password", { title: page.title, slug: page.slug, error: null }), {
|
||||
|
|
@ -56,9 +58,8 @@ const app = new Elysia()
|
|||
headers: { "content-type": "text/html; charset=utf-8" },
|
||||
});
|
||||
}
|
||||
const payload = await cached(`payload:${params.slug}`, 60, () => loadPagePayload(params.slug));
|
||||
const payload = await cached(`payload:${slug}`, 60, () => loadPagePayload(slug));
|
||||
if (!payload) return notFound();
|
||||
|
||||
const html = eta.render("page", payload);
|
||||
const headers: Record<string, string> = {
|
||||
"content-type": "text/html; charset=utf-8",
|
||||
|
|
@ -69,19 +70,15 @@ const app = new Elysia()
|
|||
};
|
||||
if (!page.index_search) headers["x-robots-tag"] = "noindex, nofollow";
|
||||
return new Response(html, { headers });
|
||||
})
|
||||
}
|
||||
|
||||
// Public JSON
|
||||
.get("/:slug.json", async ({ params, request, set, query }) => {
|
||||
if (!allow(params.slug, clientIp(request))) return rateLimited();
|
||||
const page = await cached(`page:${params.slug}`, 60, () => loadStatusPage(params.slug));
|
||||
if (!page) { set.status = 404; return { error: "not found" }; }
|
||||
if (!isAuthorised(page, request)) { set.status = 401; return { error: "password required" }; }
|
||||
|
||||
const win = (query as any)?.window as Window | undefined;
|
||||
const cacheKey = `payload:${params.slug}:${win ?? page.default_window}`;
|
||||
const payload = await cached(cacheKey, 60, () => loadPagePayload(params.slug, win));
|
||||
if (!payload) { set.status = 404; return { error: "not found" }; }
|
||||
async function renderJson(slug: string, request: Request, win?: Window): Promise<Response> {
|
||||
const page = await cached(`page:${slug}`, 60, () => loadStatusPage(slug));
|
||||
if (!page) return new Response(JSON.stringify({ error: "not found" }), { status: 404, headers: { "content-type": "application/json" } });
|
||||
if (!isAuthorised(page, request)) return new Response(JSON.stringify({ error: "password required" }), { status: 401, headers: { "content-type": "application/json" } });
|
||||
const cacheKey = `payload:${slug}:${win ?? page.default_window}`;
|
||||
const payload = await cached(cacheKey, 60, () => loadPagePayload(slug, win));
|
||||
if (!payload) return new Response(JSON.stringify({ error: "not found" }), { status: 404, headers: { "content-type": "application/json" } });
|
||||
return new Response(JSON.stringify(payload), {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
|
|
@ -89,20 +86,32 @@ const app = new Elysia()
|
|||
...(page.index_search ? {} : { "x-robots-tag": "noindex, nofollow" }),
|
||||
},
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
// Public RSS
|
||||
.get("/:slug.rss", async ({ params, request }) => {
|
||||
if (!allow(params.slug, clientIp(request))) return rateLimited();
|
||||
const page = await loadStatusPage(params.slug);
|
||||
async function renderRssResp(slug: string): Promise<Response> {
|
||||
const page = await loadStatusPage(slug);
|
||||
if (!page) return notFound();
|
||||
const xml = await cached(`rss:${params.slug}`, 300, () => renderRss(page, PUBLIC_BASE));
|
||||
const xml = await cached(`rss:${slug}`, 300, () => renderRss(page, PUBLIC_BASE));
|
||||
return new Response(xml, {
|
||||
headers: {
|
||||
"content-type": "application/rss+xml; charset=utf-8",
|
||||
"cache-control": "public, max-age=300, s-maxage=300",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const app = new Elysia()
|
||||
.get("/", () => new Response("PingQL status service", {
|
||||
headers: { "content-type": "text/plain" },
|
||||
}))
|
||||
|
||||
// Single public route — dispatches HTML / JSON / RSS by extension on the slug.
|
||||
.get("/:slug", async ({ params, request, query }) => {
|
||||
const { slug, format } = splitSlugAndFormat(params.slug);
|
||||
if (!allow(slug, clientIp(request))) return rateLimited();
|
||||
if (format === "json") return renderJson(slug, request, (query as any)?.window as Window | undefined);
|
||||
if (format === "rss") return renderRssResp(slug);
|
||||
return renderHtml(slug, request);
|
||||
})
|
||||
|
||||
// Public SVG badge
|
||||
|
|
|
|||
Loading…
Reference in New Issue