Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ cli
deploy
*.md
.env*
.claude
data
bin
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules/
.env
.env.*
*.log
.claude/
data/
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# dbml.db on c0mpute Apps. Stateless: no /data, no HEALTHCHECK (the c0mpute
# dbml.db on c0mpute Apps. State (accounts, balances, Ask history) is one
# SQLite file in /data, which c0mpute snapshots and restores. No HEALTHCHECK (the c0mpute
# worker probes /health itself; a Docker HEALTHCHECK fights its snapshot pause).
FROM node:24-alpine
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN corepack enable && pnpm install --prod --frozen-lockfile && pnpm store prune
COPY --chown=node:node src ./src
RUN chmod -R a+rX /app
ENV NODE_ENV=production PORT=8080 HOST=0.0.0.0
RUN chmod -R a+rX /app && install -d -o node -g node -m 750 /data
ENV NODE_ENV=production PORT=8080 HOST=0.0.0.0 DB_PATH=/data/dbml.db NODE_NO_WARNINGS=1
USER node
EXPOSE 8080
CMD ["node", "src/server.js"]
63 changes: 63 additions & 0 deletions bin/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env node
/**
* Operator CLI for dbml.db Ask accounts. Talks to the live admin API.
*
* DBML_ADMIN_KEY=... node bin/admin.js create "Brian" --credit 1000 # cents; prints the personal link
* node bin/admin.js list
* node bin/admin.js credit <account-id> 500 # cents (negative takes back)
* node bin/admin.js link <account-id> # new link, old one stops working
* node bin/admin.js revoke <account-id> # link off, no new one
* node bin/admin.js costs # answers and real cost by provider
* node bin/admin.js backup <file> # download the whole SQLite database
*
* DBML_ADMIN_KEY is ADMIN_KEY from the vault profullstack/dbml-db--prod.
* DBML_API defaults to https://dbml.db.pit.world.
*/
const API = (process.env.DBML_API || 'https://dbml.db.pit.world').replace(/\/$/, '');
const KEY = process.env.DBML_ADMIN_KEY;

async function call(method, path, body) {
const r = await fetch(API + path, {
method,
headers: { authorization: `Bearer ${KEY}`, 'content-type': 'application/json' },
body: body ? JSON.stringify(body) : undefined,
});
const j = await r.json().catch(() => ({}));
if (!r.ok) throw new Error(j.error || `HTTP ${r.status}`);
return j;
}

const [cmd, a, b, ...rest] = process.argv.slice(2);
const flag = (name) => {
const all = [a, b, ...rest];
const i = all.indexOf(`--${name}`);
return i >= 0 ? all[i + 1] : undefined;
};

try {
if (!KEY) throw new Error('set DBML_ADMIN_KEY (ADMIN_KEY in vault profullstack/dbml-db--prod)');
let out;
if (cmd === 'create') out = await call('POST', '/admin/api/accounts', { name: a, credit_cents: Number(flag('credit') || 0) });
else if (cmd === 'list') out = await call('GET', '/admin/api/accounts');
else if (cmd === 'credit') out = await call('POST', `/admin/api/accounts/${a}/credit`, { cents: Number(b) });
else if (cmd === 'link') out = await call('POST', `/admin/api/accounts/${a}/link`);
else if (cmd === 'revoke') out = await call('POST', `/admin/api/accounts/${a}/revoke`);
else if (cmd === 'costs') out = await call('GET', '/admin/api/costs');
else if (cmd === 'backup') {
const r = await fetch(`${API}/admin/api/backup`, { headers: { authorization: `Bearer ${KEY}` } });
if (!r.ok) throw new Error(`HTTP ${r.status}`);
const { writeFileSync } = await import('node:fs');
const buf = Buffer.from(await r.arrayBuffer());
if (buf.subarray(0, 15).toString() !== 'SQLite format 3') throw new Error('not a SQLite file');
writeFileSync(a || 'dbml-db.sqlite', buf, { mode: 0o600 });
out = { saved: a || 'dbml-db.sqlite', bytes: buf.length };
}
else {
console.log('usage: admin.js create <name> [--credit cents] | list | credit <id> <cents> | link <id> | revoke <id> | costs | backup <file>');
process.exit(2);
}
console.log(JSON.stringify(out, null, 2));
} catch (e) {
console.error(`admin: ${e.message}`);
process.exit(1);
}
6 changes: 5 additions & 1 deletion cli/bin/dbml.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ async function main() {
}
case 'from-sql': {
const r = await call('POST', '/api/v1/sql-to-dbml', { sql: await readSource(file), dialect });
if (!r.ok) return printErrors(r.errors, file), 1;
if (!r.ok) {
printErrors(r.errors, file);
if (r.hint) console.error(`hint: that file looks like DBML, not SQL. Use: dbml to-sql ${file || '-'}`);
return 1;
}
process.stdout.write(json ? JSON.stringify(r, null, 2) + '\n' : r.dbml);
return 0;
}
Expand Down
6 changes: 5 additions & 1 deletion deploy/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh
# Deploy dbml.db to c0mpute Apps (run on a box with the c0mpute CLI and the
# Deploy dbml.db to c0mpute Apps. Accounts and Ask history live in /data
# (SQLite), snapshotted every 30 s and restored on redeploy or failover.
# Deploy (run on a box with the c0mpute CLI and the
# owner key, e.g. dev2). Stateless app: two copies, primary on vienna2 with
# dev2 allowed as a second holder. The Moshpit name dbml.db points at the app id.
#
Expand All @@ -22,6 +24,8 @@ exec c0mpute apps deploy . \
--memory-mb 256 \
--cpus 0.5 \
--replicas 2 \
--state /data \
--snapshot-secs 30 \
--wait-secs 240 \
--host "$VIENNA2" \
--host "$DEV2"
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
"scripts": {
"start": "node src/server.js",
"dev": "node --watch src/server.js",
"test": "node --test test/*.test.js"
"test": "DB_PATH=:memory: node --test test/*.test.js",
"admin": "node bin/admin.js"
},
"dependencies": {
"@dbml/core": "13.1.0",
"@hono/node-server": "^2.1.11",
"@modelcontextprotocol/sdk": "^1.32.2",
"@profullstack/ai-providers": "0.3.3",
"@profullstack/coinpay-onboard": "0.5.1",
"@profullstack/footer": "0.7.2",
"@profullstack/throttle": "0.5.2",
"@profullstack/x402-gateway": "0.20.0",
Expand Down
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { SYNTAX } from './syntax.js';
import { docsPage, homePage, SITE } from './pages.js';
import { VERSION } from './version.js';
import { createPaywall } from './pay.js';
import { mountAsk } from './askroutes.js';
import { openStore } from './store.js';

export const app = new Hono();

Expand All @@ -35,6 +37,7 @@ app.use(
{ path: '/health', open: true },
{ path: '/robots.txt', open: true },
{ path: '/llms.txt', open: true },
{ path: '/hooks/', open: true },
],
}),
);
Expand All @@ -53,6 +56,10 @@ app.use('*', async (c, next) => {
app.options('/api/*', (c) => c.body(null, 204));
app.options('/mcp', (c) => c.body(null, 204));

// Ask (pay as you go answers), top-ups, operator API. Before the /api/* 404.
openStore();
mountAsk(app);

/** Read the input from JSON ({dbml}/{sql}/{input}) or a raw text body. */
async function readInput(c, field) {
const type = c.req.header('content-type') || '';
Expand Down Expand Up @@ -140,9 +147,6 @@ app.all('/mcp', async (c) => {
// The day-pass sales page and x402 settle endpoint (only with the paid tier on).
app.all('/crawl', async (c) => (gateway && (await gateway.handle(c.req.raw))) || c.text('not found', 404));

// CoinPay business webhook. Day passes settle synchronously over x402 or the
// gateway checkout return page, so events are only acknowledged here.
app.post('/hooks/coinpay', (c) => c.body(null, 204));

app.get('/health', (c) => c.json({ ok: true, version: VERSION }));
app.get('/', async (c) => c.html(await homePage()));
Expand Down
Loading
Loading