Add Node.js backend API with mock providers for all 15 vendors
Express app on port 3000 with /api/status and /api/health endpoints. Polls all providers every 2 minutes and caches results in memory. Each vendor is a self-contained ESM module with 10s timeout and graceful failure handling. Mock data matches existing frontend. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
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(),
|
||||
}));
|
||||
}
|
||||
|
||||
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}`);
|
||||
});
|
||||
Reference in New Issue
Block a user