|
| 1 | +import { createJobApp } from '@constructive-io/knative-job-fn'; |
| 2 | +import { createLogger } from '@pgpmjs/logger'; |
| 3 | +import { Pool, PoolClient } from 'pg'; |
| 4 | +import handler from './handler'; |
| 5 | + |
| 6 | +let pool: Pool | null = null; |
| 7 | + |
| 8 | +function getPool(): Pool { |
| 9 | + if (!pool) { |
| 10 | + pool = new Pool({ |
| 11 | + host: process.env.PGHOST, |
| 12 | + port: Number(process.env.PGPORT || 5432), |
| 13 | + database: process.env.PGDATABASE, |
| 14 | + user: process.env.PGUSER, |
| 15 | + password: process.env.PGPASSWORD, |
| 16 | + max: Number(process.env.PGPOOL_MAX || 10), |
| 17 | + idleTimeoutMillis: Number(process.env.PGPOOL_IDLE_TIMEOUT || 30000), |
| 18 | + connectionTimeoutMillis: Number(process.env.PGPOOL_CONNECTION_TIMEOUT || 5000), |
| 19 | + }); |
| 20 | + } |
| 21 | + return pool; |
| 22 | +} |
| 23 | + |
| 24 | +function createWithUserContext(pool: Pool, databaseId: string | undefined) { |
| 25 | + return async function withUserContext<T>( |
| 26 | + actorId: string | undefined, |
| 27 | + fn: (client: PoolClient) => Promise<T> |
| 28 | + ): Promise<T> { |
| 29 | + const client = await pool.connect(); |
| 30 | + try { |
| 31 | + await client.query('BEGIN'); |
| 32 | + |
| 33 | + if (databaseId) { |
| 34 | + await client.query(`SELECT set_config('jwt.claims.database_id', $1, true)`, [databaseId]); |
| 35 | + } |
| 36 | + if (actorId) { |
| 37 | + await client.query(`SELECT set_config('jwt.claims.user_id', $1, true)`, [actorId]); |
| 38 | + await client.query('SET LOCAL ROLE authenticated'); |
| 39 | + } |
| 40 | + |
| 41 | + const result = await fn(client); |
| 42 | + |
| 43 | + await client.query('COMMIT'); |
| 44 | + return result; |
| 45 | + } catch (err) { |
| 46 | + try { |
| 47 | + await client.query('ROLLBACK'); |
| 48 | + } catch { |
| 49 | + // Ignore rollback errors |
| 50 | + } |
| 51 | + throw err; |
| 52 | + } finally { |
| 53 | + client.release(); |
| 54 | + } |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +const app = createJobApp(); |
| 59 | +const log = createLogger('{{name}}'); |
| 60 | + |
| 61 | +app.post('/', async (req: any, res: any, next: any) => { |
| 62 | + try { |
| 63 | + const databaseId = req.get('X-Database-Id') || req.get('x-database-id') || process.env.DEFAULT_DATABASE_ID; |
| 64 | + const currentPool = getPool(); |
| 65 | + |
| 66 | + const context = { |
| 67 | + job: { |
| 68 | + jobId: req.get('X-Job-Id') || req.get('x-job-id'), |
| 69 | + workerId: req.get('X-Worker-Id') || req.get('x-worker-id'), |
| 70 | + databaseId, |
| 71 | + }, |
| 72 | + pool: currentPool, |
| 73 | + withUserContext: createWithUserContext(currentPool, databaseId), |
| 74 | + log, |
| 75 | + env: process.env as Record<string, string | undefined>, |
| 76 | + }; |
| 77 | + |
| 78 | + const params = req.body || {}; |
| 79 | + const result = await handler(params, context); |
| 80 | + |
| 81 | + res.status(200).json(result); |
| 82 | + } catch (err) { |
| 83 | + next(err); |
| 84 | + } |
| 85 | +}); |
| 86 | + |
| 87 | +export default app; |
| 88 | + |
| 89 | +if (require.main === module) { |
| 90 | + app.listen(Number(process.env.PORT || 8080)); |
| 91 | +} |
0 commit comments