import { listen } from "./mod.ts";

listen((ctx) => {
  console.log(ctx.method, ctx.path);

  const router = ctx.route();

  router.get("/", async (ctx) => {
    return await ctx.respond(
      await Deno.open("example.html"),
      "text/html",
    );
  }).get("/sse", (ctx) => {
    const sse = ctx.sse();
    const timer = setInterval(() => {
      sse.ping();
    }, 1000);
    sse.addEventListener("close", () => clearInterval(timer));
  }).get("/user/:id", (ctx, param) => {
    console.log(param);
    ctx.respond((param as { id: string }).id);
  });

  router.post("/post", async (ctx) => {
    const body = await ctx.body();
    console.log(body);
    ctx.empty(204);
  });

  router.post("/postjson", async (ctx) => {
    const body = await ctx.json<{ query: string }>();
    console.log(body);
    ctx.respond({ hello: 123 });
  });

  router.exec();
}, 3000);
