mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-06 01:56:40 +00:00
39 lines
1 KiB
TypeScript
39 lines
1 KiB
TypeScript
import { Application, Router, RouterContext } from "./deps.ts";
|
|
import { ID, sayHello, sayRandomHello } from "./src/index.ts";
|
|
import { htmlBody } from "./view.ts";
|
|
|
|
const port = 8000;
|
|
|
|
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;
|
|
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());
|
|
|
|
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`);
|