97 lines
2.7 KiB
JavaScript
97 lines
2.7 KiB
JavaScript
import express from "express";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import fs from "fs/promises";
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3001;
|
|
const CACHE_TTL_MS = Number(process.env.CACHE_TTL_MS || 10 * 60 * 1000);
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const rootDir = path.resolve(__dirname, "..");
|
|
const distDir = path.join(rootDir, "dist");
|
|
const reposFile = path.join(__dirname, "repos.json");
|
|
|
|
const cache = new Map();
|
|
|
|
async function readRepos() {
|
|
const content = await fs.readFile(reposFile, "utf-8");
|
|
const parsed = JSON.parse(content);
|
|
return Array.isArray(parsed) ? parsed : [];
|
|
}
|
|
|
|
function getCached(key) {
|
|
const entry = cache.get(key);
|
|
if (!entry) return null;
|
|
if (Date.now() > entry.expiresAt) {
|
|
cache.delete(key);
|
|
return null;
|
|
}
|
|
return entry.value;
|
|
}
|
|
|
|
function setCached(key, value) {
|
|
cache.set(key, { value, expiresAt: Date.now() + CACHE_TTL_MS });
|
|
}
|
|
|
|
async function fetchRepo(ownerRepo) {
|
|
const cached = getCached(ownerRepo);
|
|
if (cached) return cached;
|
|
|
|
const response = await fetch(`https://api.github.com/repos/${ownerRepo}`);
|
|
if (!response.ok) {
|
|
throw new Error(`GitHub request failed for ${ownerRepo}`);
|
|
}
|
|
const data = await response.json();
|
|
const mapped = {
|
|
fullName: data.full_name,
|
|
name: data.name,
|
|
description: data.description,
|
|
repoUrl: data.html_url,
|
|
stars: data.stargazers_count,
|
|
forks: data.forks_count,
|
|
issues: data.open_issues_count,
|
|
updatedAt: data.updated_at,
|
|
topics: data.topics || []
|
|
};
|
|
setCached(ownerRepo, mapped);
|
|
return mapped;
|
|
}
|
|
|
|
app.get("/api/plugins", async (_req, res) => {
|
|
try {
|
|
const repos = await readRepos();
|
|
const results = await Promise.all(
|
|
repos.map((repo) => fetchRepo(repo).catch(() => ({
|
|
fullName: repo,
|
|
name: repo.split("/")[1] || repo,
|
|
description: "Kon gegevens niet ophalen.",
|
|
repoUrl: `https://github.com/${repo}`,
|
|
stars: 0,
|
|
forks: 0,
|
|
issues: 0,
|
|
updatedAt: null,
|
|
topics: []
|
|
})))
|
|
);
|
|
|
|
res.json({
|
|
count: results.length,
|
|
updatedAt: new Date().toISOString(),
|
|
items: results
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Kon plugins niet laden." });
|
|
}
|
|
});
|
|
|
|
app.use(express.static(distDir));
|
|
app.get("*", (_req, res) => {
|
|
res.sendFile(path.join(distDir, "index.html"));
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Server draait op http://localhost:${PORT}`);
|
|
});
|