Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add localhost API bypass #951

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions lxl-web/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ SVELTE_INSPECTOR_TOGGLE=shift-alt
API_URL=
ID_URL=
USE_LOCAL_DISPLAY_JSONLD=false
USE_LOCALHOST_API=false
LOCALHOST_API_PORT=
LOCALHOST_ID_PORT=
24 changes: 23 additions & 1 deletion lxl-web/src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Handle, HandleFetch } from '@sveltejs/kit';
import { defaultLocale, Locales } from '$lib/i18n/locales';
import { USE_LOCAL_DISPLAY_JSONLD } from '$env/static/private';
import { env } from '$env/dynamic/private';
Expand All @@ -6,7 +7,7 @@ import fs from 'fs';

let utilCache;

export const handle = async ({ event, resolve }) => {
export const handle: Handle = async ({ event, resolve }) => {
const [vocabUtil, displayUtil] = await loadUtilCached();
event.locals.vocab = vocabUtil;
event.locals.display = displayUtil;
Expand All @@ -27,6 +28,27 @@ export const handle = async ({ event, resolve }) => {
});
};

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);
};

async function loadUtilCached() {
if (!utilCache) {
utilCache = loadUtil();
Expand Down