mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-10 00:06:41 -06:00
Cronitor monitoring implementation
This commit is contained in:
@@ -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" });
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user