// main.el — DHARMA CGI Provenance Registry — El implementation. // // Rewrite of the Go/SQLite DHARMA service in the El engram language. // Stores all records as typed nodes in the engram knowledge graph. // // Environment variables: // DHARMA_API_KEY — required for all authenticated routes (X-Dharma-Key header) // ENGRAM_URL — engram-server base URL (default: http://localhost:7750) // ENGRAM_KEY — engram-server API key (optional) // DHARMA_PORT — HTTP port (default: 8765) // // Routes: // GET /health — no auth // POST /principals — create principal // GET /principals/:id — get principal // POST /cgis — register CGI // GET /cgis/:id — get CGI // GET /cgis/:id/seed — get seed metadata // POST /cgis/:id/evaluation — upsert evaluation record // GET /cgis/:id/evaluation — get evaluation record // POST /cgis/:id/accumulation — add accumulation layer // GET /cgis/:id/accumulation — get latest accumulation layer // GET /cgis/:id/accumulation/history — list all accumulation layers // POST /cgis/:id/drift — report drift event // GET /cgis/:id/drift — list drift events // PATCH /cgis/:id/drift/:drift_id — resolve drift event // POST /cgis/:id/kindred — grant kindred access // GET /cgis/:id/kindred — list kindred grants // POST /internal-state — log internal state event // GET /internal-state — list internal state events (query by cgi_id in body) // POST /audit/transmission — log transmission audit entry // GET /audit/transmission — list audit entries import "types.el" import "auth.el" import "crypto.el" import "db.el" import "seed.el" import "handlers.el" // ── Startup ─────────────────────────────────────────────────────────────────── let port_str: String = env("DHARMA_PORT") let port: Int = 8765 if !str_eq(port_str, "") { let port: Int = str_to_int(port_str) } println("DHARMA Registry (El) starting on port " + int_to_str(port)) println(" Engram: " + engram_url()) let seed_result: String = run_seed() println(" Seed: " + seed_result) // ── Request router ──────────────────────────────────────────────────────────── fn handle_request(method: String, path: String, body: String) -> String { // /health — no auth if str_eq(path, "/health") { return "{\"status\":\"ok\",\"service\":\"dharma-registry-el\"}" } // All other routes require authentication if !check_auth() { return unauthorized() } // Route: /principals and /principals/:id if str_eq(path, "/principals") { return handle_principals(method, path, body) } if str_starts_with(path, "/principals/") { return handle_principals(method, path, body) } // Route: /audit/transmission if str_starts_with(path, "/audit/transmission") { return handle_audit(method, body) } // Route: /internal-state if str_starts_with(path, "/internal-state") { return handle_internal_state(method, path, body) } // Route: /cgis and /cgis/:id and sub-resources if str_eq(path, "/cgis") { return handle_cgis_root(method, body) } if str_starts_with(path, "/cgis/") { return route_cgi_subpath(method, path, body) } return "{\"error\":\"not found\"}" } // route_cgi_subpath routes /cgis/:id and /cgis/:id/* requests. fn route_cgi_subpath(method: String, path: String, body: String) -> String { // Extract segments: ["", "cgis", ":id", "subresource", ...] let parts: [String] = str_split(path, "/") let nparts: Int = list_len(parts) // Need at least ["", "cgis", ":id"] → 3 parts if nparts < 3 { return "{\"error\":\"not found\"}" } let cgi_id: String = list_get(parts, 2) // Just /cgis/:id (nparts == 3 or seg3 is "") if nparts == 3 { return handle_cgis_id(method, cgi_id) } let sub: String = list_get(parts, 3) // /cgis/:id/seed if str_eq(sub, "seed") { return handle_cgis_seed(method, cgi_id) } // /cgis/:id/covenant if str_eq(sub, "covenant") { return handle_covenant(method, cgi_id, body) } // /cgis/:id/evaluation if str_eq(sub, "evaluation") { return handle_evaluation(method, cgi_id, body) } // /cgis/:id/accumulation and /cgis/:id/accumulation/history if str_eq(sub, "accumulation") { return handle_accumulation(method, cgi_id, path, body) } // /cgis/:id/drift and /cgis/:id/drift/:drift_id if str_eq(sub, "drift") { return handle_drift(method, cgi_id, path, body) } // /cgis/:id/kindred if str_eq(sub, "kindred") { return handle_kindred(method, cgi_id, body) } return "{\"error\":\"not found\"}" } // ── Start HTTP server (blocking) ────────────────────────────────────────────── http_serve(port, handle_request)