2022-12-01 00:54:35 +09:00
|
|
|
import ping from 'ping';
|
2022-05-17 04:02:14 +02:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
|
|
|
|
|
|
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
2022-12-18 22:27:01 +01:00
|
|
|
// Parse req.body as a AppItem
|
2022-05-17 04:02:14 +02:00
|
|
|
const { url } = req.query;
|
2022-12-01 00:54:35 +09:00
|
|
|
// Parse url as URL object
|
|
|
|
|
const parsedUrl = new URL(url as string);
|
|
|
|
|
// Ping the URL
|
|
|
|
|
const response = await ping.promise.probe(parsedUrl.hostname, {
|
|
|
|
|
timeout: 1,
|
|
|
|
|
});
|
2022-12-11 13:58:28 +01:00
|
|
|
|
2022-12-01 00:54:35 +09:00
|
|
|
// Return 200 if the alive property is true
|
|
|
|
|
if (response.alive) {
|
2022-12-10 23:31:22 +09:00
|
|
|
return res.status(200).json({ alive: true });
|
2022-12-01 00:54:35 +09:00
|
|
|
}
|
|
|
|
|
// Return 404 if the alive property is false
|
2022-12-10 23:31:22 +09:00
|
|
|
return res.status(404).json({ alive: false });
|
2022-05-17 04:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
|
// Filter out if the reuqest is a POST or a GET
|
|
|
|
|
if (req.method === 'GET') {
|
|
|
|
|
return Get(req, res);
|
|
|
|
|
}
|
|
|
|
|
return res.status(405).json({
|
|
|
|
|
statusCode: 405,
|
|
|
|
|
message: 'Method not allowed',
|
|
|
|
|
});
|
|
|
|
|
};
|