Files
korrithwebsite/apps/api/docker-entrypoint.js

31 lines
1.0 KiB
JavaScript

// Docker-only entrypoint.
// The API's PocketBase client targets http://localhost:8090 (fixed in a
// platform-managed file). In docker-compose, PocketBase runs in its own
// container, so forward localhost:8090 -> the pocketbase service before
// starting the API. Local development (npm run dev) does not use this file.
import net from 'node:net';
import { spawn } from 'node:child_process';
const UPSTREAM_HOST = process.env.PB_UPSTREAM_HOST || 'pocketbase';
const UPSTREAM_PORT = 8090;
function forward(socket) {
const upstream = net.connect(UPSTREAM_PORT, UPSTREAM_HOST);
socket.pipe(upstream).pipe(socket);
socket.on('error', () => socket.destroy());
upstream.on('error', () => socket.destroy());
}
function bind(host) {
const server = net.createServer(forward);
server.on('error', () => {}); // e.g. IPv6 unavailable in the container
server.listen(UPSTREAM_PORT, host);
}
bind('127.0.0.1');
bind('::1');
const child = spawn('node', ['src/main.js'], { stdio: 'inherit', env: process.env });
child.on('exit', (code) => process.exit(code ?? 0));