html frontend

This commit is contained in:
Ruidy 2021-02-22 20:56:24 +01:00
parent 0f54f17f18
commit b42c701f20
2 changed files with 32 additions and 19 deletions

View file

@ -6,3 +6,4 @@ export {
} from "https://deno.land/std@0.87.0/testing/asserts.ts";
export { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts";
export { Application, Router } from "https://deno.land/x/oak/mod.ts";
export type { RouterContext } from "https://deno.land/x/oak/mod.ts";

View file

@ -1,29 +1,41 @@
import { Application, Router } from "./deps.ts";
import { Application, Router, RouterContext } from "./deps.ts";
import { ID, sayHello, sayRandomHello } from "./src/index.ts";
const port = 8000;
const router = new Router();
const htmlBody = (content: string) =>
`<!DOCTYPE html><html><body><pre>${content}</pre></body></html>`;
router.get("/", (ctx: any) => {
ctx.response.body = JSON.stringify(sayRandomHello());
}).get<{ id: string }>("/:id", (ctx: any) => {
type AppOpts = { port: number };
const get_application = ({ port }: AppOpts): Application => {
const router = new Router();
router.get("/", (ctx: RouterContext) => {
ctx.response.body = htmlBody(JSON.stringify(sayRandomHello(), null, 2));
}).get("/:id", (ctx: RouterContext) => {
try {
const value = ctx.params.id;
const id = new ID(value);
ctx.response.body = JSON.stringify(sayHello(id));
if (!!value) {
const id = new ID(parseInt(value, 10));
ctx.response.body = htmlBody(JSON.stringify(sayHello(id), null, 2));
}
} catch (error) {
console.error(error);
ctx.response.body = error.message;
}
});
});
const app = new Application();
app.use(router.routes());
const app = new Application();
app.addEventListener("listen", ({ port }: any) => {
app.use(router.routes());
app.addEventListener("listen", ({ port }) => {
console.log(`Server listening on http://localhost:${port}/`);
});
});
return app;
};
const app = get_application({ port });
await app.listen({ port });
console.log(`Finished`);