import { createServer } from "node:http";
|
import { readFile } from "node:fs/promises";
|
import { join, extname, dirname } from "node:path";
|
import { fileURLToPath } from "node:url";
|
import { createBaukasten } from "@dh-software/baukasten";
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
const configurationsDir =
|
process.env.BAUKASTEN_CONFIGURATIONS_DIR || join(here, "fixtures", "configurations");
|
|
// Demo StoreTree: which requesting customers belong to an owner's store group.
|
// In furnplan_web this comes from FurncloudCredential.StoreTree.
|
const demoStoreTree = {
|
"77001": ["77002"],
|
"14243": ["77001", "77002"],
|
};
|
function isStoreMember(ownerCustomerNumber, requestingCustomerNumber) {
|
return (demoStoreTree[ownerCustomerNumber] || []).includes(requestingCustomerNumber);
|
}
|
|
const baukasten = createBaukasten({ configurationsDir, isStoreMember });
|
|
const MIME = {
|
".html": "text/html; charset=utf-8",
|
".mjs": "text/javascript",
|
".js": "text/javascript",
|
".css": "text/css",
|
".map": "application/json",
|
".json": "application/json",
|
};
|
|
function send(res, status, body, type = "application/json") {
|
res.writeHead(status, { "Content-Type": type });
|
res.end(body);
|
}
|
|
async function serveStatic(res, relPath) {
|
try {
|
const filePath = join(here, relPath);
|
const data = await readFile(filePath);
|
send(res, 200, data, MIME[extname(filePath)] || "application/octet-stream");
|
} catch {
|
send(res, 404, "Not found", "text/plain");
|
}
|
}
|
|
async function readJsonBody(req) {
|
const chunks = [];
|
for await (const chunk of req) chunks.push(chunk);
|
return JSON.parse(Buffer.concat(chunks).toString("utf-8") || "null");
|
}
|
|
const server = createServer(async (req, res) => {
|
try {
|
const url = new URL(req.url, "http://localhost");
|
|
if (req.method === "GET" && url.pathname === "/api/catalog") {
|
const customerNo = url.searchParams.get("customerNo") || "";
|
const entries = await baukasten.getSelectableConfigurations(customerNo);
|
return send(res, 200, JSON.stringify(entries));
|
}
|
|
if (req.method === "POST" && url.pathname === "/api/read") {
|
const references = (await readJsonBody(req)) || [];
|
const contents = await baukasten.readConfigurations(references);
|
const result = references.map((reference, index) => ({
|
reference,
|
content: contents[index],
|
}));
|
return send(res, 200, JSON.stringify(result));
|
}
|
|
if (req.method === "GET" && (url.pathname === "/" || url.pathname === "/index.html")) {
|
return serveStatic(res, "index.html");
|
}
|
if (req.method === "GET" && url.pathname.startsWith("/dist/")) {
|
return serveStatic(res, url.pathname.slice(1));
|
}
|
|
send(res, 404, "Not found", "text/plain");
|
} catch (error) {
|
send(res, 500, JSON.stringify({ error: error?.message ?? String(error) }));
|
}
|
});
|
|
const port = Number(process.env.PORT) || 4173;
|
server.listen(port, () => {
|
console.log(`baukasten playground → http://localhost:${port}`);
|
console.log(`configurations dir → ${configurationsDir}`);
|
});
|