UNPKG

catsys

Version:

Category-theoretic system design framework for scalable, modular systems using mathematical foundations

106 lines (105 loc) 4.24 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const http = __importStar(require("http")); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const demo_service_1 = require("./demo-service"); // Create service instance const service = (0, demo_service_1.createCounterService)(); let currentState = { count: 0 }; // Simple HTTP server to serve demo.html and handle API requests const server = http.createServer(async (req, res) => { // CORS headers res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); if (req.method === 'OPTIONS') { res.writeHead(200); res.end(); return; } try { if (req.url === '/') { fs.readFile(path.join(__dirname, 'demo.html'), (err, content) => { if (err) { res.writeHead(500); res.end('Error loading demo.html'); return; } res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(content); }); } else if (req.url === '/api/increment' && req.method === 'POST') { let body = ''; req.on('data', chunk => { body += chunk.toString(); }); req.on('end', async () => { try { const { amount } = JSON.parse(body); currentState = await service.handle(currentState, { kind: 'Increment', amount: Number(amount) }); const view = await service.query(currentState); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ state: currentState, view })); } catch (error) { res.writeHead(400); res.end(JSON.stringify({ error: error instanceof Error ? error.message : 'Unknown error' })); } }); } else if (req.url === '/api/health' && req.method === 'GET') { const health = await service.healthCheck(); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(health)); } else { res.writeHead(404); res.end('Not found'); } } catch (error) { res.writeHead(500); res.end(JSON.stringify({ error: error instanceof Error ? error.message : 'Unknown error' })); } }); const PORT = process.env.PORT || 3000; server.listen(PORT, () => { console.log(`🚀 Demo server running at http://localhost:${PORT}`); console.log('Press Ctrl+C to stop\n'); });