mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-11 07:55:52 +01:00
Add a timeout of 2 seconds for the ping module
This commit is contained in:
@@ -4,19 +4,24 @@ import { NextApiRequest, NextApiResponse } from 'next';
|
|||||||
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||||
// Parse req.body as a AppItem
|
// Parse req.body as a AppItem
|
||||||
const { url } = req.query;
|
const { url } = req.query;
|
||||||
// Parse url as URL object
|
const agent = new https.Agent({ rejectUnauthorized: false });
|
||||||
const parsedUrl = new URL(url as string);
|
await axios
|
||||||
// Ping the URL
|
.get(url as string, { httpsAgent: agent, timeout: 2000 })
|
||||||
const response = await ping.promise.probe(parsedUrl.hostname, {
|
.then((response) => {
|
||||||
timeout: 1,
|
res.status(response.status).json(response.statusText);
|
||||||
});
|
})
|
||||||
|
.catch((error) => {
|
||||||
// Return 200 if the alive property is true
|
if (error.response) {
|
||||||
if (response.alive) {
|
res.status(error.response.status).json(error.response.statusText);
|
||||||
return res.status(200).json({ alive: true });
|
} else if (error.code === 'ECONNABORTED') {
|
||||||
}
|
res.status(408).json('Request Timeout');
|
||||||
// Return 404 if the alive property is false
|
} else {
|
||||||
return res.status(404).json({ alive: false });
|
res.status(500).json('Server Error');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// // Make a request to the URL
|
||||||
|
// const response = await axios.get(url);
|
||||||
|
// // Return the response
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user