Gesamte Mono-Repo des Baukastens (alle apps/clients, examples/tests und packages)
dh_heyart
vor 7 Std. 6c5682091e41d21772f699a702219154bf9f242d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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}`);
});