Files
WebHooker/src/server.ts
T
RhenCloud 81d21ffb08 feat(web): add public landing page and move console to /admin
Serve a public landing page at / with buttons to the docs, GitHub repo,
Terms, Privacy and the admin login. Relocate the Nuxt admin console from
/ to /admin so the root is no longer the private console.

- add home-routes.ts (bilingual landing, auto dark mode, configurable
  DOCS_URL / GITHUB_REPO_URL) mounted at /
- move console to admin/pages/admin.vue; redirect / and unknown SPA
  routes to /admin
- point admin login/logout redirects at /admin
2026-08-02 07:40:14 +08:00

80 lines
2.4 KiB
TypeScript

import { Hono } from "hono";
import type { Env } from "./types";
import { verifySignature, parseEvent } from "./webhook";
import { dispatchEvent } from "./discord";
import { createOAuthRoutes } from "./oauth-routes";
import { createActionRoutes } from "./action-routes";
import { createAdminRoutes } from "./admin-routes";
import { createLegalRoutes } from "./legal-routes";
import { createHomeRoutes } from "./home-routes";
import { log } from "./log";
const MAX_BODY_SIZE = 1024 * 1024;
export function createServer(): Hono<{ Bindings: Env }> {
const app = new Hono<{ Bindings: Env }>();
app.get("/health", (c) => c.json({ status: "ok" }));
app.route("/auth", createOAuthRoutes());
app.route("/", createActionRoutes());
app.route("/", createLegalRoutes());
app.route("/", createHomeRoutes());
app.route("/admin", createAdminRoutes());
app.post("/webhook", async (c) => {
const contentLength = Number(c.req.header("content-length") ?? 0);
if (contentLength > MAX_BODY_SIZE) {
return c.json({ error: "Request too large" }, 413);
}
const body = await c.req.text();
if (body.length > MAX_BODY_SIZE) {
return c.json({ error: "Request too large" }, 413);
}
const headers: Record<string, string> = {};
c.req.raw.headers.forEach((value, key) => {
headers[key] = value;
});
if (
!(await verifySignature(body, headers["x-hub-signature-256"], c.env.GITHUB_WEBHOOK_SECRET))
) {
return c.json({ error: "Invalid signature" }, 401);
}
const event = parseEvent(headers, body);
if (!event) {
return c.json({ error: "Invalid event" }, 400);
}
const delivery = headers["x-github-delivery"];
if (delivery) {
const seen = await c.env.KV.get(`delivery:${delivery}`);
if (seen) {
return c.json({ ok: true, duplicate: true });
}
await c.env.KV.put(`delivery:${delivery}`, "1", { expirationTtl: 300 });
}
const { loadConfig } = await import("./config");
const config = await loadConfig(c.env);
const dispatch = dispatchEvent(config, event, c.env).catch((err) =>
log.error(err, "Dispatch failed"),
);
c.executionCtx.waitUntil(dispatch);
return c.json({ ok: true });
});
app.notFound((c) => {
if (c.env.ASSETS) {
return c.env.ASSETS.fetch(c.req.raw);
}
return c.json({ error: "Not found" }, 404);
});
return app;
}