This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
109 lines (94 loc) · 2.9 KB
/
app.js
File metadata and controls
109 lines (94 loc) · 2.9 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import express from "express";
import fs from "fs/promises";
import { fileURLToPath } from "url";
import path from "path";
const app = express();
const PORT = process.env.PORT || 4000;
const defaultState = { cards: [], dependencies: [] };
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const DATA_PATH = path.join(__dirname, "data", "boardState.json");
await fs.mkdir(path.dirname(DATA_PATH), { recursive: true });
app.use(express.json());
// Statische Dateien mit Caching
app.use(
express.static(path.join(__dirname, "public"), {
maxAge: "1d",
immutable: true,
})
);
// Datenordner-Initialisierung
const initDataDir = async () => {
try {
await fs.access(path.dirname(DATA_PATH));
} catch {
await fs.mkdir(path.dirname(DATA_PATH), { recursive: true });
await fs.writeFile(DATA_PATH, JSON.stringify(defaultState));
}
};
// API-Endpunkte
app.get("/load-board", async (req, res) => {
try {
const data = await fs.readFile(DATA_PATH, "utf-8");
const parsed = JSON.parse(data);
res.status(200).json({
cards: Array.isArray(parsed.cards) ? parsed.cards : [],
dependencies: Array.isArray(parsed.dependencies)
? parsed.dependencies
: [],
});
} catch (error) {
res.status(200).json(defaultState);
}
});
app.post("/save-board", async (req, res) => {
if (!req.body || typeof req.body !== "object") {
return res.status(400).json("Ungültige Anfragedaten");
}
const toSave = {
cards: Array.isArray(req.body.cards) ? req.body.cards : [],
dependencies: Array.isArray(req.body.dependencies)
? req.body.dependencies
: [],
};
try {
await fs.writeFile(DATA_PATH, JSON.stringify(toSave, null, 2));
res.status(200).json({ message: "Board-Zustand gespeichert" });
} catch (error) {
res.status(500).json("Speichervorgang fehlgeschlagen");
}
});
app.post("/reset-board", async (req, res) => {
try {
await fs.writeFile(DATA_PATH, JSON.stringify(defaultState, null, 2));
res.status(200).json({ message: "Board zurückgesetzt" });
} catch (error) {
res.status(400).json("Zurücksetzen fehlgeschlagen");
}
});
app.use(function (req, res, next) {
if (req.originalUrl && req.originalUrl.split("/").pop() === "favicon.ico") {
return res.status(204).end();
}
next();
});
// Client-Side Routing
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "public", req.path));
});
// Serverstart
const server = app.listen(PORT, async () => {
await initDataDir();
console.log(`Server läuft auf Port ${PORT}`);
});
// Graceful Shutdown
const gracefulShutdown = (signal) => {
console.log(`${signal} empfangen, Server wird beendet`);
server.close(() => process.exit(0));
};
["SIGINT", "SIGTERM"].forEach((signal) => {
process.on(signal, () => gracefulShutdown(signal));
});