openvibers/openvibe.finance serverindex.js
index.js
| 1 | ; |
| 2 | /** |
| 3 | * OpenVibe.Finance's process: the database (DATABASE_URL through PgBouncer; migrations on DATABASE_DIRECT_URL), the |
| 4 | * vault (FINANCE_DATA_KEY), sign-in through OpenVibe.Network (OV_OAUTH_CLIENT_ID / OV_OAUTH_CLIENT_SECRET), and the |
| 5 | * app on 127.0.0.1:PORT (5292). SIGTERM closes the server and the pool. |
| 6 | */ |
| 7 | const path = require('node:path'); |
| 8 | const { createDb } = require('openvibe-sdk/db'); |
| 9 | const { createSsoClient } = require('openvibe-sdk/sso'); |
| 10 | const { createVault } = require('./crypto'); |
| 11 | const { createApp } = require('./app'); |
| 12 | |
| 13 | async function main() { |
| 14 | const env = process.env; |
| 15 | const port = Number(env.PORT || 5292); |
| 16 | const baseUrl = env.BASE_URL || 'https://openvibe.finance'; |
| 17 | // Migrations with the owner role on a direct connection (the runtime role through PgBouncer may only read and write |
| 18 | // rows: OpenVibe.Host roles/data/add-service.sh), then the runtime handle. |
| 19 | if (!env.DATABASE_URL || !env.DATABASE_DIRECT_URL) throw new Error('DATABASE_URL and DATABASE_DIRECT_URL are required (OpenVibe.Host roles/data/add-service.sh finance)'); |
| 20 | const owner = createDb({ url: env.DATABASE_DIRECT_URL, service: 'finance-migrate', max: 1 }); |
| 21 | try { await owner.migrate({ dir: path.join(__dirname, '..', 'migrations') }); } finally { await owner.close(); } |
| 22 | const db = createDb({ url: env.DATABASE_URL, service: 'finance' }); |
| 23 | const vault = createVault(env.FINANCE_DATA_KEY); |
| 24 | const sso = (more = {}) => createSsoClient({ |
| 25 | site: 'finance', siteName: 'OpenVibe.Finance', baseUrl, clientId: env.OV_OAUTH_CLIENT_ID, clientSecret: env.OV_OAUTH_CLIENT_SECRET, |
| 26 | networkUrl: env.OV_NETWORK_URL || 'https://openvibe.network', networkInternalUrl: env.OV_NETWORK_INTERNAL_URL || 'http://127.0.0.1:4000', |
| 27 | secureCookies: env.NODE_ENV === 'production', ...more, |
| 28 | }); |
| 29 | const auth = env.OV_OAUTH_CLIENT_ID && env.OV_OAUTH_CLIENT_SECRET ? sso() : null; |
| 30 | // POST /mcp only: also a delegated person token naming finance (SDK 0.187; Network's Connect mints it per call). |
| 31 | const mcpAuth = auth ? sso({ delegated: true }) : null; |
| 32 | if (!auth) console.warn('[finance] no OAuth client: sign-in is off'); |
| 33 | const server = createApp({ db, vault, auth, mcpAuth, baseUrl }).listen(port, '127.0.0.1', () => console.log(`[finance] listening on 127.0.0.1:${port}`)); |
| 34 | process.on('SIGTERM', () => server.close(async () => { try { await db.close(); } finally { process.exit(0); } })); |
| 35 | } |
| 36 | |
| 37 | main().catch((err) => { console.error('[finance] could not start:', err && err.message); process.exit(1); }); |
| 38 |