import { Application, Router, send } from "@oak/oak"; import config from "./config.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"; } }); 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"; } }); 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"; } }); router.get("/assets/:path*", async (ctx) => { try { const filePath = ctx.params.path; await send(ctx, filePath, { root: `${Deno.cwd()}/assets`, }); } catch (_e) { ctx.response.status = 404; ctx.response.body = "Not Found"; } }); const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); app.listen({ port: config.port }); console.log(`App listening on http://localhost:${config.port}`);