import { Application, Router } from "@oak/oak"; import { log } from "./log.ts"; import naddrHandler from "./handlers/naddr.ts"; import nprofileHandler from "./handlers/nprofile.ts"; import npubHandler from "./handlers/npub.ts"; import userProfileHandler from "./handlers/user-profile.ts"; import userEventHandler from "./handlers/user-event.ts"; import userAtomFeedHandler from "./handlers/user-atom-feed.ts"; const router = new Router(); router.get("/:path", async (ctx: ctx) => { const { path } = ctx.params; if (path.startsWith("naddr")) { await naddrHandler(ctx); } else if (path.startsWith("nprofile")) { await nprofileHandler(ctx); } else if (path.startsWith("npub")) { await npubHandler(ctx); } else if (path.startsWith("@") || path.startsWith("~")) { await userProfileHandler(ctx); } else { ctx.response.status = 404; ctx.response.body = "Not Found"; } log( `${ctx.request.method} ${ctx.request.url} - ${ctx.response.status}`, "gray", ); }); router.get("/:user/:kind.atom", async (ctx: ctx) => { const { user } = ctx.params; if (user.startsWith("@") || user.startsWith("~") || kind === "articles") { await userAtomFeedHandler(ctx); } else { ctx.response.status = 404; ctx.response.body = "Not Found"; } log( `${ctx.request.method} ${ctx.request.url} - ${ctx.response.status}`, "gray", ); }); router.get("/:user/:identifier", async (ctx: ctx) => { const { user } = ctx.params; if (user.startsWith("@") || user.startsWith("~")) { await userEventHandler(ctx); } else { ctx.response.status = 404; ctx.response.body = "Not Found"; } log( `${ctx.request.method} ${ctx.request.url} - ${ctx.response.status}`, "gray", ); }); const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); const PORT = 8000; app.listen({ port: PORT }); console.log(`App listening on http://localhost:${PORT}`);