Files
mpm/modules/demo/backend/server.js

70 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Demo-Modul: Referenzimplementierung des Modul-API-Vertrags (Phase 6).
* Läuft als eigener Node-Prozess im Management-Container (Port via ENV PORT).
*
* Vertrag (verbindlich für alle Module):
* GET /health → Liveness/Readiness
* GET /api/manifest → Manifest zur Laufzeit
* GET /api/me → Aktueller Benutzer (vom Gateway übergeben)
*
* Die Identität stammt ausschließlich aus den Gateway-Headern
* (siehe platform-module-sdk.js) – niemals aus der URL oder dem Body.
*/
const http = require('node:http');
const {
extractIdentity,
healthResponse,
meResponse,
} = require('./platform-module-sdk');
const PORT = Number(process.env.PORT ?? 41001);
/** Manifest des Moduls (identisch zu module.json). */
const MANIFEST = {
moduleId: 'demo',
version: '1.0.0',
status: 'healthy',
};
const server = http.createServer((request, response) => {
const url = new URL(request.url ?? '/', `http://127.0.0.1:${PORT}`);
if (url.pathname === '/health') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(healthResponse(MANIFEST.moduleId, MANIFEST.version)));
return;
}
if (url.pathname === '/api/manifest') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(MANIFEST));
return;
}
if (url.pathname === '/api/me') {
const identity = extractIdentity(request.headers);
if (!identity) {
// Request kam nicht über den Modul-Gateway → keine Identität.
response.writeHead(401, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ statusCode: 401, message: 'Keine Identität übergeben' }));
return;
}
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(meResponse(identity, MANIFEST.moduleId, ['demo.read'])));
return;
}
if (url.pathname === '/') {
response.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
response.end('Demo-Modul läuft');
return;
}
response.writeHead(404, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ statusCode: 404, message: 'Nicht gefunden' }));
});
server.listen(PORT, '127.0.0.1', () => {
console.log(`Demo-Modul läuft auf Port ${PORT}`);
});