PWA Banner entfernt und Laden verbessert
This commit is contained in:
1 parent
534b5a35fc
commit
fe66a5abe2
7 files changed
+186
-106
No files matched your search
@@ -57,20 +57,43 @@ const PUBLIC_BASE_URL = process.env.PUBLIC_BASE_URL || "http://localhost:3000";
|
||||
const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL || "https://discord.com/api/webhooks/1450872860607381515/zRbiXOykKPUfLJ_H87wD2OxQJPLdro6yQzU2T48Pg4fxSNaXEqBFgxdHGTiIDmmKGHIF";
|
||||
|
||||
// --------------------
|
||||
// State Handling
|
||||
// State Handling - MIT PERSISTENT lastReason
|
||||
// --------------------
|
||||
function getState() {
|
||||
if (!fs.existsSync(stateFile)) {
|
||||
const initialState = {
|
||||
current: "A",
|
||||
lastReason: null
|
||||
};
|
||||
fs.writeFileSync(
|
||||
stateFile,
|
||||
JSON.stringify({ current: "A", lastReason: null }, null, 2)
|
||||
JSON.stringify(initialState, null, 2)
|
||||
);
|
||||
console.log("✅ Neue state.json erstellt");
|
||||
return initialState;
|
||||
}
|
||||
|
||||
try {
|
||||
const state = JSON.parse(fs.readFileSync(stateFile, "utf-8"));
|
||||
// Ensure lastReason exists in state
|
||||
if (!state.hasOwnProperty('lastReason')) {
|
||||
state.lastReason = null;
|
||||
}
|
||||
console.log(`📖 State geladen: ${state.current}, Letzter Grund: ${state.lastReason || 'keiner'}`);
|
||||
return state;
|
||||
} catch (err) {
|
||||
console.error("❌ Fehler beim Lesen von state.json:", err);
|
||||
return { current: "A", lastReason: null };
|
||||
}
|
||||
return JSON.parse(fs.readFileSync(stateFile, "utf-8"));
|
||||
}
|
||||
|
||||
function setState(newState) {
|
||||
fs.writeFileSync(stateFile, JSON.stringify(newState, null, 2));
|
||||
try {
|
||||
fs.writeFileSync(stateFile, JSON.stringify(newState, null, 2));
|
||||
console.log(`💾 State gespeichert: ${newState.current}, Grund: ${newState.lastReason || 'keiner'}`);
|
||||
} catch (err) {
|
||||
console.error("❌ Fehler beim Speichern von state.json:", err);
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------
|
||||
@@ -133,7 +156,8 @@ async function sendDiscordEmbed(previousKey, currentKey, reason) {
|
||||
// --------------------
|
||||
|
||||
app.get("/status", (req, res) => {
|
||||
res.json(getState());
|
||||
const state = getState();
|
||||
res.json(state);
|
||||
});
|
||||
|
||||
app.post("/toggle", async (req, res) => {
|
||||
@@ -141,8 +165,13 @@ app.post("/toggle", async (req, res) => {
|
||||
const previousKey = state.current;
|
||||
const reason = req.body?.reason;
|
||||
|
||||
// Wechseln
|
||||
state.current = state.current === "A" ? "B" : "A";
|
||||
|
||||
// Grund speichern (oder null wenn keiner angegeben)
|
||||
state.lastReason = reason || null;
|
||||
|
||||
// State persistent speichern
|
||||
setState(state);
|
||||
|
||||
const logMessage = reason
|
||||
@@ -151,6 +180,7 @@ app.post("/toggle", async (req, res) => {
|
||||
|
||||
console.log(logMessage);
|
||||
|
||||
// Discord Benachrichtigung
|
||||
await sendDiscordEmbed(previousKey, state.current, reason);
|
||||
|
||||
res.json({
|
||||
@@ -167,27 +197,22 @@ app.get("/health", (req, res) => {
|
||||
// --------------------
|
||||
// SPA Fallback - NO CRASH
|
||||
// --------------------
|
||||
// Only serve index.html for navigation requests, not for missing static files
|
||||
app.use((req, res, next) => {
|
||||
// If request already handled by static middleware, skip
|
||||
if (res.headersSent) return;
|
||||
|
||||
// If it's an API route that wasn't matched, 404
|
||||
if (req.url.startsWith('/api/')) {
|
||||
return res.status(404).json({ error: 'Not found' });
|
||||
}
|
||||
|
||||
// If Accept header indicates browser navigation (HTML), serve index.html
|
||||
const acceptsHtml = req.accepts('html');
|
||||
if (acceptsHtml) {
|
||||
return res.sendFile(path.join(__dirname, "public/index.html"));
|
||||
}
|
||||
|
||||
// Otherwise 404
|
||||
res.status(404).send('Not found');
|
||||
});
|
||||
|
||||
// Error handler to prevent crashes
|
||||
// Error handler
|
||||
app.use((err, req, res, next) => {
|
||||
console.error('Server error:', err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
@@ -202,6 +227,12 @@ app.listen(PORT, "0.0.0.0", () => {
|
||||
console.log(`📁 Static files directory: ${path.join(__dirname, "public")}`);
|
||||
console.log(`🔒 Trust proxy: enabled (for reverse proxy support)`);
|
||||
|
||||
// Initial state laden und anzeigen
|
||||
const initialState = getState();
|
||||
console.log(`\n🎲 Aktueller Status:`);
|
||||
console.log(` 👤 Wer zahlt: ${CONFIG[initialState.current].name}`);
|
||||
console.log(` 📝 Letzter Grund: ${initialState.lastReason || 'Keiner gespeichert'}`);
|
||||
|
||||
// Verify critical files exist
|
||||
const criticalFiles = [
|
||||
'public/index.html',
|
||||
@@ -216,7 +247,7 @@ app.listen(PORT, "0.0.0.0", () => {
|
||||
console.log(` ${exists ? '✅' : '❌'} ${file}`);
|
||||
});
|
||||
|
||||
console.log('\n📝 Test URLs (adjust domain for your setup):');
|
||||
console.log('\n📝 Test URLs:');
|
||||
console.log(` http://localhost:${PORT}/`);
|
||||
console.log(` http://localhost:${PORT}/service-worker.js`);
|
||||
console.log(` http://localhost:${PORT}/manifest.json\n`);
|
||||
|
||||
Reference in new issue
Block a user