Files
Codewalkers/apps/server/preview/port-allocator.ts
Lukas May 143aad58e8 feat: Replace per-preview Caddy sidecars with shared gateway architecture
Refactor preview deployments to use a single shared Caddy gateway container
with subdomain routing (<previewId>.localhost:<port>) instead of one Caddy
sidecar and one port per preview. Adds dev/preview modes, git worktree
support for branch checkouts, and auto-start on phase:pending_review.

- Add GatewayManager for shared Caddy lifecycle + Caddyfile generation
- Add git worktree helpers for preview mode branch checkouts
- Add dev mode: volume-mount + dev server image instead of build
- Remove per-preview Caddy sidecar and port publishing
- Use shared cw-preview-net Docker network with container name DNS
- Auto-start previews when phase enters pending_review
- Delete unused PreviewPanel.tsx
- Update all tests (40 pass), docs, events, CLI, tRPC, frontend
2026-03-05 12:22:29 +01:00

55 lines
1.3 KiB
TypeScript

/**
* Port Allocator
*
* Finds the next available port for the gateway.
* Only called once when the gateway first starts — subsequent previews
* reuse the same gateway port.
*/
import { createServer } from 'node:net';
import { createModuleLogger } from '../logger/index.js';
const log = createModuleLogger('preview:port');
/** Starting port for preview deployments */
const BASE_PORT = 9100;
/** Maximum port to try before giving up */
const MAX_PORT = 9200;
/**
* Allocate the next available port by performing a bind test.
*
* @returns An available port number
* @throws If no port is available in the range
*/
export async function allocatePort(): Promise<number> {
for (let port = BASE_PORT; port < MAX_PORT; port++) {
if (await isPortAvailable(port)) {
log.info({ port }, 'allocated port');
return port;
}
}
throw new Error(`No available ports in range ${BASE_PORT}-${MAX_PORT}`);
}
/**
* Test if a port is available by attempting to bind to it.
*/
async function isPortAvailable(port: number): Promise<boolean> {
return new Promise((resolve) => {
const server = createServer();
server.once('error', () => {
resolve(false);
});
server.listen(port, '127.0.0.1', () => {
server.close(() => {
resolve(true);
});
});
});
}