Middleware proxy updates

This commit is contained in:
Darren Clarke 2023-07-21 12:26:02 +00:00 committed by GitHub
parent 282808583b
commit 38d3e9349c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 329 additions and 224 deletions

View file

@ -21,49 +21,31 @@ const rewriteURL = (request: NextRequestWithAuth, originBaseURL: string, destina
requestHeaders.delete('connection');
// console.log({ finalHeaders: requestHeaders });
return NextResponse.rewrite(new URL(destinationURL), { request: { headers: requestHeaders } });
};
const checkRewrites = async (request: NextRequestWithAuth) => {
console.log({ currentURL: request.nextUrl.href });
const linkBaseURL = process.env.LINK_URL ?? "http://localhost:3000";
const zammadURL = process.env.ZAMMAD_URL ?? "http://zammad-nginx:8080";
const leafcutterURL = process.env.LEAFCUTTER_URL ?? "https://lc.digiresilience.org";
const metamigoURL = process.env.METAMIGO_URL ?? "http://metamigo-frontend:3000";
console.log({ linkBaseURL, zammadURL, leafcutterURL, metamigoURL });
const labelStudioURL = process.env.LABEL_STUDIO_URL ?? "http://label-studio:8080";
if (request.nextUrl.pathname.startsWith('/proxy/leafcutter')) {
const headers = { 'X-Leafcutter-Embedded': "true" };
return rewriteURL(request, linkBaseURL, leafcutterURL, headers);
return rewriteURL(request, `${linkBaseURL}/proxy/leafcutter`, leafcutterURL, headers);
} else if (request.nextUrl.pathname.startsWith('/proxy/metamigo')) {
return rewriteURL(request, linkBaseURL, metamigoURL);
return rewriteURL(request, `${linkBaseURL}/proxy/metamigo`, metamigoURL);
} else if (request.nextUrl.pathname.startsWith('/proxy/label-studio')) {
return rewriteURL(request, `${linkBaseURL}/proxy/label-studio`, labelStudioURL);
} else if (request.nextUrl.pathname.startsWith('/proxy/zammad')) {
console.log('proxying to zammad');
const { token } = request.nextauth;
// console.log({ nextauth: request.nextauth });
const headers = {
'X-Forwarded-User': token.email.toLowerCase(),
host: 'link-stack-dev.digiresilience.org'
};
// console.log({ headers });
const headers = { 'X-Forwarded-User': token.email.toLowerCase() };
return rewriteURL(request, `${linkBaseURL}/proxy/zammad`, zammadURL, headers);
} else if (request.nextUrl.pathname.startsWith('/assets') || request.nextUrl.pathname.startsWith('/api/v1')) {
console.log('asset');
return rewriteURL(request, linkBaseURL, zammadURL);
} else if (request.nextUrl.pathname.startsWith('/proxy/assets')) {
console.log('proxy asset');
return rewriteURL(request, `${linkBaseURL}/proxy`, zammadURL);
} else if (request.nextUrl.pathname.startsWith('/proxy/api')) {
console.log('proxy api');
return rewriteURL(request, `${linkBaseURL}/proxy`, zammadURL);
} else if (request.nextUrl.pathname.startsWith('/api/v1')) {
return rewriteURL(request, linkBaseURL, zammadURL);
}
};
@ -77,7 +59,6 @@ export default withAuth(
authorized: ({ token, req }) => {
const {
url,
headers,
} = req;
// check login page