41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { Application, Router } from "@oak/oak";
|
|
import { getNotes, getNotesByDid } from "./src/data/db.ts";
|
|
|
|
const router = new Router();
|
|
|
|
router.get("/", (ctx) => {
|
|
ctx.response.body = "Hello world";
|
|
});
|
|
|
|
router.get("/notes", (ctx) => {
|
|
const cursor = ctx.request.url.searchParams.get("cursor") ?? undefined;
|
|
const limit = Number(ctx.request.url.searchParams.get("limit")) || 20;
|
|
ctx.response.body = getNotes(cursor, limit);
|
|
});
|
|
|
|
router.get("/:did/notes", (ctx) => {
|
|
const { did } = ctx.params;
|
|
const cursor = ctx.request.url.searchParams.get("cursor") ?? undefined;
|
|
const limit = Number(ctx.request.url.searchParams.get("limit")) || 20;
|
|
ctx.response.body = getNotesByDid(did, cursor, limit);
|
|
});
|
|
|
|
const app = new Application();
|
|
|
|
app.use(async (ctx, next) => {
|
|
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
|
|
ctx.response.headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
|
ctx.response.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
if (ctx.request.method === "OPTIONS") {
|
|
ctx.response.status = 204;
|
|
return;
|
|
}
|
|
await next();
|
|
});
|
|
|
|
app.use(router.routes());
|
|
app.use(router.allowedMethods());
|
|
|
|
console.log("[server] listening on port 8080");
|
|
app.listen({ port: 8080 });
|