51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
const logHttp = scoped("http", { color: "magenta" });
|
|
|
|
const app = new Hono();
|
|
|
|
app.notFound(assets.notFound);
|
|
|
|
app.use(trimTrailingSlash());
|
|
app.use(removeDuplicateSlashes);
|
|
app.use(logger((msg) => msg.startsWith("-->") && logHttp(msg.slice(4))));
|
|
app.use(admin.middleware);
|
|
|
|
app.route("", require("./q+a/backend.ts").app);
|
|
app.route("", require("./file-viewer/backend.tsx").app);
|
|
// app.route("", require("./friends/backend.tsx").app);
|
|
|
|
app.use(assets.middleware);
|
|
|
|
if (process.argv.includes("--development")) {
|
|
app.onError((err, c) => {
|
|
if (err instanceof HTTPException) {
|
|
// Get the custom response
|
|
return err.getResponse();
|
|
}
|
|
|
|
return c.text(util.inspect(err), 500);
|
|
});
|
|
}
|
|
|
|
export default app;
|
|
|
|
async function removeDuplicateSlashes(c: Context, next: Next) {
|
|
const path = c.req.path;
|
|
if (/\/\/+/.test(path)) {
|
|
const normalizedPath = path.replace(/\/\/+/g, "/");
|
|
const query = c.req.query();
|
|
const queryString = Object.keys(query).length > 0
|
|
? "?" + new URLSearchParams(query).toString()
|
|
: "";
|
|
return c.redirect(normalizedPath + queryString, 301);
|
|
}
|
|
await next();
|
|
}
|
|
|
|
import { type Context, Hono, type Next } from "#hono";
|
|
import { HTTPException } from "hono/http-exception";
|
|
import { logger } from "hono/logger";
|
|
import { trimTrailingSlash } from "hono/trailing-slash";
|
|
import * as assets from "#sitegen/assets";
|
|
import * as admin from "./admin.ts";
|
|
import { scoped } from "@paperclover/console";
|
|
import * as util from "node:util";
|