Cronitor monitoring implementation

This commit is contained in:
Xevion
2023-02-23 22:11:42 -06:00
parent f1147d92af
commit 88614ed453
2 changed files with 98 additions and 12 deletions

View File

@@ -2,29 +2,39 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { getDistance } from "@/location";
import { env } from "@/env/server.mjs";
import monitorAsync from "@/monitor";
type ResponseData = {
name: string;
diff: number;
inRange: boolean;
};
const center = { latitude: env.CENTER_LATITUDE, longitude: env.CENTER_LONGITUDE };
type StatusData = { status: string };
const center = {
latitude: env.CENTER_LATITUDE,
longitude: env.CENTER_LONGITUDE,
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
res: NextApiResponse<ResponseData & StatusData | StatusData>
) {
if (req.query.key != env.API_KEY) {
// auth failed
res.status(401).json({ name: "Unauthorized" });
res.status(401).json({ status: "Unauthorized" });
return;
}
const diff = await getDistance();
// auth passed
res.setHeader('Cache-Control', `max-age=0, s-maxage=${env.EDGE_CACHE_TIME_SECONDS}, stale-while-revalidate`);
// @ts-ignore
res.status(200).json({ diff, inRange: diff < env.MAX_DISTANCE });
await monitorAsync(async function () {
const diff = await getDistance();
// auth passed
res.setHeader(
"Cache-Control",
`max-age=0, s-maxage=${env.EDGE_CACHE_TIME_SECONDS}, stale-while-revalidate`
);
res
.status(200)
.json({ diff, inRange: diff < env.MAX_DISTANCE, status: "Authorized" });
});
}