feat(waas): add catch-all route, page manager and draft-publish pipeline
This commit is contained in:
53
src/pages/api/admin/pages/create.ts
Normal file
53
src/pages/api/admin/pages/create.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
export const POST: APIRoute = async ({ request }) => {
|
||||
try {
|
||||
const { title, slug } = await request.json();
|
||||
if (!title || !slug) {
|
||||
return new Response(JSON.stringify({ error: 'Titel und Slug erforderlich' }), { status: 400 });
|
||||
}
|
||||
|
||||
const dataDir = process.env.DATA_DIR || path.join(process.cwd(), 'app', 'data');
|
||||
const configPath = path.join(dataDir, 'site.config.json');
|
||||
|
||||
let config: any = {};
|
||||
try {
|
||||
const rawData = await fs.readFile(configPath, 'utf-8');
|
||||
config = JSON.parse(rawData);
|
||||
} catch {
|
||||
config = {};
|
||||
}
|
||||
|
||||
const formattedSlug = slug.startsWith('/') ? slug : `/${slug}`;
|
||||
const newPageId = `page_${Date.now()}`;
|
||||
|
||||
const newPage = {
|
||||
id: newPageId,
|
||||
slug: formattedSlug,
|
||||
title: title,
|
||||
is_published: true,
|
||||
seo: { description: `${title} - N&D IT Solutions` },
|
||||
sections: [
|
||||
{
|
||||
id: `sec_hero_${Date.now()}`,
|
||||
type: 'HeroSection',
|
||||
settings: { title: title, subtitle: 'Willkommen auf dieser Unterseite.' }
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
config.pages = config.pages || [];
|
||||
config.pages.push(newPage);
|
||||
|
||||
await fs.mkdir(dataDir, { recursive: true });
|
||||
await fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf-8');
|
||||
|
||||
return new Response(JSON.stringify({ success: true, page: newPage }), { status: 201 });
|
||||
} catch (error) {
|
||||
return new Response(JSON.stringify({ error: 'Fehler beim Erstellen der Seite' }), { status: 500 });
|
||||
}
|
||||
};
|
||||
34
src/pages/api/admin/pages/set-homepage.ts
Normal file
34
src/pages/api/admin/pages/set-homepage.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
export const POST: APIRoute = async ({ request }) => {
|
||||
try {
|
||||
const { page_id } = await request.json();
|
||||
if (!page_id) return new Response(JSON.stringify({ error: 'page_id gefordert' }), { status: 400 });
|
||||
|
||||
const dataDir = process.env.DATA_DIR || path.join(process.cwd(), 'app', 'data');
|
||||
const configPath = path.join(dataDir, 'site.config.json');
|
||||
|
||||
let config: any = {};
|
||||
try {
|
||||
const rawData = await fs.readFile(configPath, 'utf-8');
|
||||
config = JSON.parse(rawData);
|
||||
} catch {
|
||||
config = {};
|
||||
}
|
||||
|
||||
// Update homepage_id
|
||||
config.site_info = config.site_info || {};
|
||||
config.site_info.homepage_id = page_id;
|
||||
|
||||
await fs.mkdir(dataDir, { recursive: true });
|
||||
await fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf-8');
|
||||
|
||||
return new Response(JSON.stringify({ success: true, homepage_id: page_id }), { status: 200 });
|
||||
} catch (error) {
|
||||
return new Response(JSON.stringify({ error: 'Fehler beim Speichern' }), { status: 500 });
|
||||
}
|
||||
};
|
||||
28
src/pages/api/editor/publish.ts
Normal file
28
src/pages/api/editor/publish.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
const DATA_DIR = process.env.DATA_DIR || path.join(process.cwd(), 'app', 'data');
|
||||
|
||||
export const POST: APIRoute = async () => {
|
||||
try {
|
||||
const draftPath = path.join(DATA_DIR, 'site.config.draft.json');
|
||||
const livePath = path.join(DATA_DIR, 'site.config.json');
|
||||
|
||||
// Prüfe ob ein Entwurf existiert
|
||||
try {
|
||||
await fs.access(draftPath);
|
||||
} catch {
|
||||
return new Response(JSON.stringify({ error: 'Kein gespeicherter Entwurf vorhanden' }), { status: 404 });
|
||||
}
|
||||
|
||||
// Atomares Kopieren des Entwurfs auf die Live-Konfiguration
|
||||
await fs.copyFile(draftPath, livePath);
|
||||
|
||||
return new Response(JSON.stringify({ success: true, message: 'Website erfolgreich veröffentlicht!' }), { status: 200 });
|
||||
} catch (error) {
|
||||
return new Response(JSON.stringify({ error: 'Fehler beim Veröffentlichen' }), { status: 500 });
|
||||
}
|
||||
};
|
||||
25
src/pages/api/editor/save-draft.ts
Normal file
25
src/pages/api/editor/save-draft.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
const DATA_DIR = process.env.DATA_DIR || path.join(process.cwd(), 'app', 'data');
|
||||
|
||||
export const POST: APIRoute = async ({ request }) => {
|
||||
try {
|
||||
const draftConfig = await request.json();
|
||||
if (!draftConfig || typeof draftConfig !== 'object') {
|
||||
return new Response(JSON.stringify({ error: 'Ungültige Konfigurationsdaten' }), { status: 400 });
|
||||
}
|
||||
|
||||
const draftPath = path.join(DATA_DIR, 'site.config.draft.json');
|
||||
|
||||
await fs.mkdir(DATA_DIR, { recursive: true });
|
||||
await fs.writeFile(draftPath, JSON.stringify(draftConfig, null, 2), 'utf-8');
|
||||
|
||||
return new Response(JSON.stringify({ success: true, message: 'Entwurf erfolgreich gespeichert' }), { status: 200 });
|
||||
} catch (error) {
|
||||
return new Response(JSON.stringify({ error: 'Fehler beim Speichern des Entwurfs' }), { status: 500 });
|
||||
}
|
||||
};
|
||||
39
src/pages/api/editor/update.ts
Normal file
39
src/pages/api/editor/update.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
// Paths inside Docker volume /app/data
|
||||
const draftPath = path.resolve(process.cwd(), 'app', 'data', 'site.config.draft.json');
|
||||
const livePath = path.resolve(process.cwd(), 'app', 'data', 'site.config.json');
|
||||
|
||||
/** GET current draft (for editor init) */
|
||||
export const GET: APIRoute = async () => {
|
||||
let draft = {};
|
||||
if (fs.existsSync(draftPath)) {
|
||||
try {
|
||||
draft = JSON.parse(fs.readFileSync(draftPath, 'utf8'));
|
||||
} catch (e) {
|
||||
console.error('Failed to parse draft JSON', e);
|
||||
}
|
||||
}
|
||||
return new Response(JSON.stringify({ draft }), { status: 200, headers: { 'Content-Type': 'application/json' } });
|
||||
};
|
||||
|
||||
/** POST to save draft or publish */
|
||||
export const POST: APIRoute = async ({ request }) => {
|
||||
const body = await request.json();
|
||||
const { draft, action } = body as { draft: Record<string, any>; action?: string };
|
||||
|
||||
// Ensure directory exists
|
||||
fs.mkdirSync(path.dirname(draftPath), { recursive: true });
|
||||
// Save draft
|
||||
fs.writeFileSync(draftPath, JSON.stringify(draft, null, 2), 'utf8');
|
||||
|
||||
// Publish if requested
|
||||
if (action === 'publish') {
|
||||
fs.mkdirSync(path.dirname(livePath), { recursive: true });
|
||||
fs.writeFileSync(livePath, JSON.stringify(draft, null, 2), 'utf8');
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify({ success: true }), { status: 200, headers: { 'Content-Type': 'application/json' } });
|
||||
};
|
||||
Reference in New Issue
Block a user