Skip to content

Commit

Permalink
Typescript - Add ui route
Browse files Browse the repository at this point in the history
  • Loading branch information
julioomoura committed Jun 8, 2024
1 parent 487cbfd commit 870a1b2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"author": "Júlio Moura",
"license": "ISC",
"devDependencies": {
"@types/ejs": "^3.1.5",
"@types/node": "^20.14.0",
"eslint": "^8.56.0",
"jest": "^29.7.0",
Expand All @@ -36,4 +37,4 @@
"redis": "^4.6.13",
"zod": "^3.23.8"
}
}
}
15 changes: 15 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Fastify from "fastify";
import { fastifyView } from "@fastify/view";
import ejs from "ejs";
import { z } from "zod";
import { CacheClient, createRedisClient } from "./redis.js";
import RandomDadJokesAPI from "./random-dad-jokes.api.js";
Expand All @@ -11,6 +13,12 @@ const cacheClient = new CacheClient(redisClient).getRedisClient();
const randomDadJokesAPI = new RandomDadJokesAPI();
const messageService = new MessageService(cacheClient, randomDadJokesAPI);

fastify.register(fastifyView, {
engine: {
ejs,
},
});

fastify.get("/", async (request, reply) => {
return { hello: "world" };
});
Expand All @@ -20,6 +28,13 @@ fastify.get("/messages", async (request, reply) => {
reply.code(200).send({ message });
});

fastify.get("/ui/messages", async (request, reply) => {
const message = await messageService.getMessage();
return reply.viewAsync("./src/ui/message.ejs", {
message,
});
});

fastify.post("/messages", async (request, reply) => {
const message = z.object({
message: z.string(),
Expand Down

0 comments on commit 870a1b2

Please sign in to comment.