09404db559
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
import "dotenv/config";
|
|
import express from "express";
|
|
import cors from "cors";
|
|
import { providers } from "./providers/index.js";
|
|
|
|
const app = express();
|
|
const PORT = 3000;
|
|
const POLL_INTERVAL = 2 * 60 * 1000; // 2 minutes
|
|
const PROVIDER_TIMEOUT = 10 * 1000; // 10 seconds
|
|
|
|
app.use(cors());
|
|
|
|
let cachedStatuses = [];
|
|
|
|
function safeCheck(provider) {
|
|
return Promise.race([
|
|
provider.checkStatus(),
|
|
new Promise((_, reject) =>
|
|
setTimeout(() => reject(new Error("Timeout")), PROVIDER_TIMEOUT)
|
|
),
|
|
]).catch((err) => ({
|
|
name: provider.name,
|
|
status: "unknown",
|
|
message: `Check failed: ${err.message}`,
|
|
lastUpdated: new Date().toISOString(),
|
|
})).then((result) => ({
|
|
url: provider.url ?? null,
|
|
...result,
|
|
}));
|
|
}
|
|
|
|
async function pollAll() {
|
|
const results = await Promise.allSettled(providers.map(safeCheck));
|
|
cachedStatuses = results.map((r) =>
|
|
r.status === "fulfilled"
|
|
? r.value
|
|
: {
|
|
name: "Unknown",
|
|
status: "unknown",
|
|
message: "Provider check failed.",
|
|
lastUpdated: new Date().toISOString(),
|
|
}
|
|
);
|
|
console.log(
|
|
`[${new Date().toISOString()}] Polled ${cachedStatuses.length} providers`
|
|
);
|
|
}
|
|
|
|
app.get("/api/status", (_req, res) => {
|
|
res.json(cachedStatuses);
|
|
});
|
|
|
|
app.get("/api/health", (_req, res) => {
|
|
res.json({ ok: true, providers: providers.length });
|
|
});
|
|
|
|
// Poll immediately, then on interval
|
|
await pollAll();
|
|
setInterval(pollAll, POLL_INTERVAL);
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Backend listening on http://localhost:${PORT}`);
|
|
});
|