Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/routes/__tests__/gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import request from 'supertest';
import { createTestApp, signedCookie } from '../../__tests__/test-helpers.js';

let previousUsers: string | undefined;
let previousWebPort: string | undefined;
let stateDir: string;

beforeEach(() => {
previousUsers = process.env.USERS;
previousWebPort = process.env.WEB_PORT;
stateDir = mkdtempSync(join(tmpdir(), 'gateway-route-settings-'));
process.env.USERS = JSON.stringify([{ id: 'u1', name: 'User', workDir: stateDir, stateDir }]);
});

afterEach(() => {
if (previousUsers === undefined) delete process.env.USERS;
else process.env.USERS = previousUsers;
if (previousWebPort === undefined) delete process.env.WEB_PORT;
else process.env.WEB_PORT = previousWebPort;
rmSync(stateDir, { recursive: true, force: true });
});

Expand Down Expand Up @@ -58,4 +62,15 @@ describe('/api/gateway', () => {
const disabled = await request(server).post('/api/gateway').set('Cookie', cookie).send({ enabled: false });
expect(disabled.body.gateway).toMatchObject({ enabled: false, configured: false, source: 'state' });
});
});

it('returns backend WEB_PORT base URL when host is vite dev server', async () => {
process.env.WEB_PORT = '3000';
const cookie = signedCookie('u1');
const res = await request(await app())
.get('/api/gateway')
.set('Cookie', cookie)
.set('Host', 'localhost:5173');
expect(res.status).toBe(200);
expect(res.body.gateway.baseUrl).toBe('http://localhost:3000/v1');
});
});
21 changes: 18 additions & 3 deletions src/routes/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ function userConfig(userId: string) {
return getUsersConfig().find((user) => user.id === userId);
}

function gatewayBaseUrl(origin: string | null | undefined): string {
const rawOrigin = typeof origin === 'string' ? origin : '';
try {
const url = new URL(rawOrigin);
if (url.port === '5173') {
url.port = process.env.WEB_PORT ?? '3000';
}
return `${url.origin}/v1`;
} catch {
return rawOrigin ? `${rawOrigin.replace(/\/$/, '')}/v1` : '/v1';
}
}

export function gateway(router: Router): void {
router.get('/api/gateway', async (ctx) => {
const userId = ctx.state.userId as string | undefined;
Expand All @@ -26,7 +39,8 @@ export function gateway(router: Router): void {
return;
}
const userCtx = await calcUser(userId);
ctx.body = { gateway: await getGatewayStatus(cfg, userCtx.stateDir, `${ctx.origin}/v1`) };
const requestOrigin = ctx.origin ?? `${ctx.protocol}://${ctx.host}`;
ctx.body = { gateway: await getGatewayStatus(cfg, userCtx.stateDir, gatewayBaseUrl(requestOrigin)) };
});

router.post('/api/gateway', async (ctx) => {
Expand All @@ -44,11 +58,12 @@ export function gateway(router: Router): void {
}
const body = (ctx.request.body ?? {}) as Record<string, unknown>;
const userCtx = await calcUser(userId);
const requestOrigin = ctx.origin ?? `${ctx.protocol}://${ctx.host}`;
ctx.body = {
gateway: await updateGatewayStatus(cfg, userCtx.stateDir, {
enabled: typeof body.enabled === 'boolean' ? body.enabled : undefined,
rotate: body.rotate === true,
}, `${ctx.origin}/v1`),
}, gatewayBaseUrl(requestOrigin)),
};
});
}
}