import { h } from "https://deno.land/x/corfu@v2.0.1/mod.ts"; import { Application, Router } from "https://deno.land/x/oak@v11.1.0/mod.ts"; import config from "./config.ts"; import watt from "./wattpad.ts"; const app = new Application(); //print info on listen app.addEventListener("listen", (event) => { console.log( `voltpad listening on http://${event.hostname.replace( "::1", "localhost" )}:${event.port}` ); }); app.use(async (ctx, next) => { if (ctx.request.url.pathname === "/") ctx.request.url.pathname = "/index"; let mod; try { mod = await import( "./routes" + ctx.request.url.pathname + ".tsx" + `?update=${Date.now()}` ); } catch { try { return await ctx.send({ root: `./static`, index: `${ctx.request.url.pathname}` }); } catch { try { if ( ctx.request.url.pathname === "/api/getStory" && ctx.request.url.searchParams.get("id") ) { ctx.response.body = await watt.getStory( ctx.request.url.searchParams.get("id") as string, false ); ctx.response.headers.set( "Content-Type", "application/json" ); return; } else { throw "L bozo"; } } catch { mod = await import("./routes/404.tsx"); ctx.response.status = 404; } } } let rizz; try { rizz = await mod.default(ctx.request.url.searchParams, ctx); } catch (e) { console.error(e); mod = await import("./routes/500.tsx"); rizz = await mod.default(); ctx.response.status = 500; } ctx.response.body = rizz; await next(); }); //(attempt to) set mimetype app.use(async (ctx, next) => { await next(); if (ctx.request.url.searchParams.get("feedtype")) { switch (ctx.request.url.searchParams.get("feedtype")) { case "rss": ctx.response.headers.set("Content-Type", "application/rss+xml"); return; case "atom": ctx.response.headers.set( "Content-Type", "application/atom+xml" ); return; case "json": ctx.response.headers.set( "Content-Type", "application/feed+json" ); return; } } if (!ctx.response.headers.get("Content-Type")) { switch (ctx.request.url.pathname.split(".").at(-1)) { case "json": ctx.response.headers.set("Content-Type", "application/json"); break; default: ctx.response.body = "" + (ctx.response.body as string); ctx.response.headers.set("Content-Type", "text/html"); } } }); app.use(async (ctx, next) => { await next(); console.log( `${ctx.response.status} ${ctx.request.method} ${ctx.request.url}` ); }); await app.listen({ hostname: config.serve.address, port: config.serve.port });