substr/server.ts

71 lines
1.8 KiB
TypeScript

import { Application, Context, 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";
import notFoundHandler from "./handlers/not-found.ts";
const router = new Router();
router.get("/:path", async (ctx: Context) => {
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 {
notFoundHandler(ctx);
}
});
router.get("/:user/:kind.atom", async (ctx: Context) => {
const { user, kind } = ctx.params;
if (
kind === "articles" &&
(user.startsWith("@") || user.startsWith("~"))
) {
await userAtomFeedHandler(ctx);
} else {
notFoundHandler(ctx);
}
});
router.get("/:user/:identifier", async (ctx: Context) => {
const { user } = ctx.params;
if (user.startsWith("@") || user.startsWith("~")) {
await userEventHandler(ctx);
} else {
notFoundHandler(ctx);
}
});
router.get("/assets/:path*", async (ctx) => {
try {
const filePath = ctx.params.path || '';
await send(ctx, filePath, {
root: `${Deno.cwd()}/assets`,
});
} catch (_e) {
notFoundHandler(ctx);
}
});
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}`);