From f33dcf13564142b3d3ff6c2a12b31aa9f2c947ec Mon Sep 17 00:00:00 2001 From: Johan Bisse Mattsson Date: Fri, 26 Jan 2024 12:50:16 +0100 Subject: [PATCH 1/2] Add localhost API bypass --- lxl-web/.env.example | 3 +++ lxl-web/src/hooks.server.ts | 25 ++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lxl-web/.env.example b/lxl-web/.env.example index e74308e8e..43e0632fb 100644 --- a/lxl-web/.env.example +++ b/lxl-web/.env.example @@ -1,3 +1,6 @@ SVELTE_INSPECTOR_TOGGLE=shift-alt API_URL= ID_URL= +USE_LOCALHOST_API=false +LOCALHOST_API_PORT= +LOCALHOST_ID_PORT= \ No newline at end of file diff --git a/lxl-web/src/hooks.server.ts b/lxl-web/src/hooks.server.ts index af8e13933..19cc951c6 100644 --- a/lxl-web/src/hooks.server.ts +++ b/lxl-web/src/hooks.server.ts @@ -1,6 +1,8 @@ +import type { Handle, HandleFetch } from '@sveltejs/kit'; import { defaultLocale, Locales } from '$lib/i18n/locales'; +import { env } from '$env/dynamic/private'; -export const handle = async ({ event, resolve }) => { +export const handle: Handle = async ({ event, resolve }) => { // set HTML lang // https://github.com/sveltejs/kit/issues/3091#issuecomment-1112589090 const path = event.url.pathname; @@ -16,3 +18,24 @@ export const handle = async ({ event, resolve }) => { transformPageChunk: ({ html }) => html.replace('%lang%', lang) }); }; + +export const handleFetch: HandleFetch = async ({ request, fetch }) => { + if (env.USE_LOCALHOST_API === 'true') { + // Hit API directly bypassing whatever proxies and load balancers sit between it and the public internet). + // clone the original request, but change the URL + if (request.url.startsWith(env.API_URL)) { + request = new Request( + request.url.replace(env.API_URL, `http://localhost:${env.LOCALHOST_API_PORT}/`), + request + ); + } + if (request.url.startsWith(env.ID_URL)) { + request = new Request( + request.url.replace(env.ID_URL, `http://localhost:${env.LOCALHOST_ID_PORT}/`), + request + ); + } + } + + return fetch(request); +}; From 2d478bb885d1aea3648c227bbabb98d186439bb4 Mon Sep 17 00:00:00 2001 From: Johan Bisse Mattsson Date: Fri, 26 Jan 2024 13:10:36 +0100 Subject: [PATCH 2/2] Remove duplicate env imports --- lxl-web/src/hooks.server.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lxl-web/src/hooks.server.ts b/lxl-web/src/hooks.server.ts index e48bd5a42..4e6dac47e 100644 --- a/lxl-web/src/hooks.server.ts +++ b/lxl-web/src/hooks.server.ts @@ -1,6 +1,5 @@ import type { Handle, HandleFetch } from '@sveltejs/kit'; import { defaultLocale, Locales } from '$lib/i18n/locales'; -import { env } from '$env/dynamic/private'; import { USE_LOCAL_DISPLAY_JSONLD } from '$env/static/private'; import { env } from '$env/dynamic/private'; import { DisplayUtil, VocabUtil } from '$lib/utils/xl';