-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
42 lines (33 loc) · 1.64 KB
/
index.ts
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
import express, { Express, Request, Response } from "express";
import cron from "node-cron";
import { queryParamsCheck } from "./utils/middleware";
import { fetchRawAPI as fetchOpenMeteoRawAPI, fetchAPI as fetchOpenMeteoAPI } from "./utils/api/openmeteo";
import { generateCalendar } from "./utils/calendar";
import { reset } from "./utils/cache";
import { extractWindIntervals } from "./utils/geo";
const PORT = process.env.PORT || 3000;
const IS_PRODUCTION = process.env.NODE_ENV === "production";
cron.schedule("0 0 * * *", async () => {
console.info("Reseting cache files");
const deletedFiles = await reset();
if (deletedFiles.length) {
console.info(`${deletedFiles} deleted`);
}
});
const app: Express = express();
app.use(express.static("dist"));
app.get("/json", queryParamsCheck, async (_req: Request, res: Response) => {
res.json(await fetchOpenMeteoAPI(res.locals.coords, res.locals.forceRefresh));
});
app.get("/raw", queryParamsCheck, async (_req: Request, res: Response) => {
res.json(await fetchOpenMeteoRawAPI(res.locals.coords, res.locals.forceRefresh));
});
app.get("/cal", queryParamsCheck, async (req: Request, res: Response) => {
const weatherData = await fetchOpenMeteoAPI(res.locals.coords, res.locals.forceRefresh);
const weatherDataGroups = extractWindIntervals(weatherData, res.locals.coords, res.locals.preferences);
const iCal = generateCalendar(weatherDataGroups, res.locals.coords, res.locals.preferences).toString();
res.set({ "content-type": `text/${IS_PRODUCTION ? "calendar" : "plain"}; charset=utf-8` }).end(iCal);
});
app.listen(PORT, () => {
console.info(`Wind calendar listening on port ${PORT}`);
});