93 lines
2.1 KiB
JavaScript
93 lines
2.1 KiB
JavaScript
/**
|
|
* Steam API Handler für Vite Dev Server
|
|
* Fungiert als Proxy um CORS-Probleme zu vermeiden
|
|
*/
|
|
|
|
import { fetchSteamGames } from "./steam-backend.mjs";
|
|
|
|
export async function handleSteamRefresh(req, res) {
|
|
if (req.method !== "POST") {
|
|
res.statusCode = 405;
|
|
res.end("Method Not Allowed");
|
|
return;
|
|
}
|
|
|
|
let body = "";
|
|
req.on("data", (chunk) => {
|
|
body += chunk.toString();
|
|
});
|
|
|
|
req.on("end", async () => {
|
|
try {
|
|
let payload;
|
|
try {
|
|
payload = JSON.parse(body || "{}");
|
|
} catch (error) {
|
|
res.statusCode = 400;
|
|
res.end(
|
|
JSON.stringify({
|
|
error: "Ungültiges JSON im Request-Body",
|
|
}),
|
|
);
|
|
return;
|
|
}
|
|
|
|
const { apiKey, steamId } = payload;
|
|
|
|
if (!apiKey || !steamId) {
|
|
res.statusCode = 400;
|
|
res.end(JSON.stringify({ error: "apiKey und steamId erforderlich" }));
|
|
return;
|
|
}
|
|
|
|
const { games, count } = await fetchSteamGames(apiKey, steamId);
|
|
|
|
res.statusCode = 200;
|
|
res.setHeader("Content-Type", "application/json");
|
|
res.end(JSON.stringify({ games, count }));
|
|
} catch (error) {
|
|
res.statusCode = 500;
|
|
res.end(
|
|
JSON.stringify({
|
|
error: error instanceof Error ? error.message : String(error),
|
|
}),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Config Loader - lädt config.local.json für Test-Modus
|
|
*/
|
|
export async function handleConfigLoad(req, res) {
|
|
if (req.method !== "GET") {
|
|
res.statusCode = 405;
|
|
res.end("Method Not Allowed");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const { readFile } = await import("node:fs/promises");
|
|
const { fileURLToPath } = await import("node:url");
|
|
const { dirname, join } = await import("node:path");
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
const configPath = join(__dirname, "..", "config.local.json");
|
|
|
|
const configData = await readFile(configPath, "utf-8");
|
|
const config = JSON.parse(configData);
|
|
|
|
res.statusCode = 200;
|
|
res.setHeader("Content-Type", "application/json");
|
|
res.end(JSON.stringify(config));
|
|
} catch (error) {
|
|
res.statusCode = 404;
|
|
res.end(
|
|
JSON.stringify({
|
|
error: "config.local.json nicht gefunden",
|
|
}),
|
|
);
|
|
}
|
|
}
|