#!/usr/bin/env node /** Direct customer web access. Not Zapier. NATS only after authz. */ import http from 'node:http'; import { PLANE, statement, reload } from './gate.js'; const PORT = Number(process.env.PORT || 3021); const server = http.createServer(async (req, res) => { const url = new URL(req.url || '/', `http://127.0.0.1:${PORT}`); const json = (code, obj) => { res.writeHead(code, { 'content-type': 'application/json' }); res.end(JSON.stringify(obj)); }; try { if (req.method === 'GET' && url.pathname === '/health') { return json(200, { ok: true, role: 'verae-access-web', plane: PLANE }); } const st = url.pathname.match(/^\/statement\/([^/]+)$/); if (req.method === 'GET' && st) { const out = await statement(st[1], st[1]); return json(out.status, out.body); } if (req.method === 'POST' && url.pathname === '/reload') { const chunks = []; for await (const c of req) chunks.push(c); const body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}'); const out = await reload(body.customerId, body.cents, body.customerId); return json(out.status, out.body); } json(404, { error: 'not found' }); } catch (err) { json(502, { error: err.message }); } }); server.listen(PORT, '0.0.0.0', () => { process.stdout.write(`verae-access-web http://0.0.0.0:${PORT}/ plane=${PLANE}\n`); });