import { bold, cyan, green, yellow,} from "https://deno.land/std@0.152.0/fmt/colors.ts";
import { Application, Context, Router, Status } from "../mod.ts";
function notFound(context: Context) { context.response.status = Status.NotFound; context.response.body = `<html><body><h1>404 - Not Found</h1><p>Path <code>${context.request.url}</code> not found.`;}
const controller = new AbortController();
const router = new Router();router .get("/", (context) => { context.response.body = "Hello world!"; }) .get("/close", (context) => { context.response.body = "Bye!"; controller.abort(); });
const app = new Application();
app.use(async (context, next) => { await next(); const rt = context.response.headers.get("X-Response-Time"); console.log( `${green(context.request.method)} ${cyan(context.request.url.pathname)} - ${ bold( String(rt), ) }`, );});
app.use(async (context, next) => { const start = Date.now(); await next(); const ms = Date.now() - start; context.response.headers.set("X-Response-Time", `${ms}ms`);});
app.use(router.routes());app.use(router.allowedMethods());
app.use(notFound);
app.addEventListener("listen", ({ hostname, port, serverType }) => { console.log( bold("Start listening on ") + yellow(`${hostname}:${port}`), ); console.log(bold(" using HTTP server: " + yellow(serverType)));});
const { signal } = controller;await app.listen({ hostname: "127.0.0.1", port: 8000, signal });console.log(bold("Finished."));