Initial import of verae-access-web from zapier monorepo

This commit is contained in:
George Lambert 2026-09-11 17:16:51 -04:00
commit 6c2523ae26
7 changed files with 218 additions and 0 deletions

50
src/gate.js Normal file
View file

@ -0,0 +1,50 @@
const AUTHZ = (process.env.AUTHZ_URL || 'http://127.0.0.1:3020').replace(/\/$/, '');
const BOOKS = (process.env.ACCOUNT_BALANCE_URL || 'http://127.0.0.1:3010').replace(/\/$/, '');
export const PLANE = 'web';
export const AUTHZ_CHECK = 'verae.access.authz.check';
export async function check(subject, { principal, kind, payload } = {}) {
const r = await fetch(`${AUTHZ}/check`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ plane: PLANE, subject, principal, kind, payload }),
});
return { status: r.status, body: await r.json().catch(() => ({ allow: false })) };
}
export async function statement(customerId, principal) {
const gate = await check('verae.billing.statement.get', { principal });
if (!gate.body.allow) return { status: 403, body: gate.body };
const url = process.env.NATS_URL;
if (url) {
try {
const { connect, StringCodec } = await import('nats');
const nc = await connect({ servers: url.split(','), name: 'verae-access-web' });
const sc = StringCodec();
const m = await nc.request(
'verae.billing.statement.get',
sc.encode(JSON.stringify({ customerId, plane: PLANE, principal })),
{ timeout: 2000 },
);
const out = JSON.parse(sc.decode(m.data) || '{}');
await nc.close();
return { status: 200, body: { ...out, source: 'nats', plane: PLANE } };
} catch {
/* HTTP fallback */
}
}
const r = await fetch(`${BOOKS}/statement/${customerId}`);
return { status: r.status, body: { ...(await r.json().catch(() => ({}))), source: 'account-balance', plane: PLANE } };
}
export async function reload(customerId, cents, principal) {
const gate = await check('verae.billing.balance.adjust', { principal, kind: 'reload' });
if (!gate.body.allow) return { status: 403, body: gate.body };
const r = await fetch(`${BOOKS}/adjust`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ customerId, cents, reason: 'reload', agent: principal || 'web', kind: 'reload' }),
});
return { status: r.status, body: { ...(await r.json().catch(() => ({}))), plane: PLANE } };
}

38
src/server.js Normal file
View file

@ -0,0 +1,38 @@
#!/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`);
});