-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
65 lines (55 loc) · 1.96 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static";
import { html, raw } from "hono/html";
import { parse } from "marked";
import fs from "node:fs";
import { getWeather } from "./api.js";
import { generate } from "./llm.js";
const app = new Hono();
app.use("/img/*", serveStatic({ root: "./" }));
app.use(async function (ctx, next) {
ctx.setRenderer(function (content) {
const template = fs
.readFileSync("./template.html", "utf-8")
.replace("{{content}}", content);
return ctx.html(template);
});
await next();
});
app.get("/health", function (ctx) {
return ctx.text("OK");
});
app.get("/", async function (ctx) {
const location = ctx.req.query("location") || "Jakarta";
const weather = await getWeather(location);
const prompt = `You are an awesome weather reporter. Generate a report for today's weather in ${location} based on data below:
- temperature: ${weather.temp}
- humidity: ${weather.humidity}
- wind speed: ${weather.windspeed}
- maximum temperature: ${weather.maxTemp}
Give a recommendation on what to wear, what to bring, and any activities that are suitable for the weather. Make the report short, funny and engaging.
`;
const comment = await generate(prompt);
return ctx.render(
html`<div class="weathers">
<h1>${location}</h1>
<div id="weather">
<div class="info">
<div class="icon">
<img src="img/${weather.icon}.png" />
</div>
<div class="text">
<ul id="report">
<li>Temperature: <span id="temp">${weather.temp}°C</span></li>
<li>Humidity: <span id="humidity">${weather.humidity}%</span></li>
<li>Wind Speed: <span id="uv">${weather.windspeed}</span></li>
</ul>
</div>
</div>
</div>
<div id="comment">${raw(parse(comment))}</div>
</div>`,
);
});
serve({ fetch: app.fetch, port: 3000 });