feat(runtime): add isolated same-origin canary gateway
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
import {
|
||||
CANARY_DEFAULT_PORT,
|
||||
CANARY_DEFAULT_UPSTREAM_PORT,
|
||||
createCanaryGateway,
|
||||
} from './canary-gateway.ts';
|
||||
|
||||
function environmentPort(name: string, fallback: number): number {
|
||||
const value = process.env[name];
|
||||
if (value === undefined || value === '') return fallback;
|
||||
const port = Number(value);
|
||||
if (!Number.isInteger(port)) throw new Error(`${name} must be an integer port`);
|
||||
return port;
|
||||
}
|
||||
|
||||
const gateway = createCanaryGateway({
|
||||
distDir: resolve(process.env.CANARY_DIST_DIR ?? 'apps/web/dist'),
|
||||
port: environmentPort('CANARY_PORT', CANARY_DEFAULT_PORT),
|
||||
upstreamPort: environmentPort('CANARY_UPSTREAM_PORT', CANARY_DEFAULT_UPSTREAM_PORT),
|
||||
});
|
||||
|
||||
let stopping = false;
|
||||
async function stop(signal: string): Promise<void> {
|
||||
if (stopping) return;
|
||||
stopping = true;
|
||||
process.stdout.write(`Stopping canary gateway (${signal})\n`);
|
||||
await gateway.stop();
|
||||
}
|
||||
|
||||
process.once('SIGINT', () => void stop('SIGINT'));
|
||||
process.once('SIGTERM', () => void stop('SIGTERM'));
|
||||
|
||||
try {
|
||||
await gateway.start();
|
||||
process.stdout.write(`Canary gateway listening at ${gateway.origin}\n`);
|
||||
} catch (error) {
|
||||
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
Reference in New Issue
Block a user