Files
homepage/src/pages/api/ping.js
shamoon 97e909ebf4
Some checks failed
Docker CI / Linting Checks (push) Has been cancelled
Docker CI / Docker Build & Push (push) Has been cancelled
Crowdin Action / Crowdin Sync (push) Has been cancelled
Chore: move to eslint (#6270)
2026-02-02 15:18:30 -08:00

43 lines
1.2 KiB
JavaScript

import { promise as ping } from "ping";
import { getServiceItem } from "utils/config/service-helpers";
import createLogger from "utils/logger";
const logger = createLogger("ping");
export default async function handler(req, res) {
const { groupName, serviceName } = req.query;
const serviceItem = await getServiceItem(groupName, serviceName);
if (!serviceItem) {
logger.debug(`No service item found for group ${groupName} named ${serviceName}`);
return res.status(400).send({
error: "Unable to find service, see log for details.",
});
}
const { ping: pingHostOrURL } = serviceItem;
if (!pingHostOrURL) {
logger.debug("No ping host specified");
return res.status(400).send({
error: "No ping host given",
});
}
let hostname = pingHostOrURL;
try {
// maintain backwards compatibility with old ping where may be http://...
hostname = new URL(pingHostOrURL).hostname;
} catch (e) {}
try {
const response = await ping.probe(hostname);
return res.status(200).json(response);
} catch (e) {
logger.debug("Error attempting ping: %s", e);
return res.status(400).send({
error: "Error attempting ping, see logs.",
});
}
}