Skip to content

Commit

Permalink
Http client and frontend-proxy tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
espen42 committed Aug 10, 2021
1 parent 1f82a0e commit e7c9dad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions hmdb/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
include "com.enonic.xp:lib-portal:${xpVersion}"
include "com.enonic.xp:lib-cluster:${xpVersion}"
include "com.enonic.xp:lib-context:${xpVersion}"
include 'com.enonic.lib:lib-http-client:2.3.0'
include "com.enonic.lib:lib-guillotine:5.1.0"
include "com.enonic.lib:lib-graphql-playground:0.0.1"
include "com.enonic.lib:lib-thymeleaf:2.0.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const httpClient = require('/lib/http-client');
const frontendOrigin = "frontendOrigin=http://localhost:3000" // <- hardcode for poc
const frontendOrigin = "http://localhost:3000" // <- hardcode for poc

const loopbackCheckParam = 'fromXp';

const errorResponse = (url, status, message) => {
const errorResponse = function(url, status, message) {
const msg = `Failed to fetch from frontend: ${url} - ${status}: ${message}`;
if (status >= 400) {
log.error(msg);
Expand All @@ -18,13 +18,9 @@ const errorResponse = (url, status, message) => {

// This proxies requests made directly to XP to the frontend. Normally this will
// only be used in the portal-admin content studio previews
const frontendProxy = (req) => {
return {
contentType: 'text/html',
body: `<div><h1>Success!</h1><p>Triggered the frontend proxy!</p></div>`,
status: 200,
};
/*
const frontendProxy = function(req) {


const isLoopback = req.params[loopbackCheckParam];
if (isLoopback) {
log.info(`Loopback to XP detected from path ${req.rawPath}`);
Expand All @@ -38,11 +34,18 @@ const frontendProxy = (req) => {
const pathStartIndex = req.rawPath.indexOf(req.branch) + req.branch.length;

// this isn't likely to show up at when not working with Enonic's stuff.
// const contentPath = req.rawPath.slice(pathStartIndex).replace('/www.nav.no', '');
const contentPath = req.rawPath; //.slice(pathStartIndex).replace('/www.nav.no', '');

const frontendPath = req.branch === 'draft' ? `/draft${contentPath}` : contentPath;
const frontendUrl = `${frontendOrigin}${frontendPath}?${loopbackCheckParam}=true`;

log.info("frontendUrl (" +
(Array.isArray(frontendUrl) ?
("array[" + frontendUrl.length + "]") :
(typeof frontendUrl + (frontendUrl && typeof frontendUrl === 'object' ? (" with keys: " + JSON.stringify(Object.keys(frontendUrl))) : ""))
) + "): " + JSON.stringify(frontendUrl, null, 2)
);

try {
const response = httpClient.request({
url: frontendUrl,
Expand All @@ -58,7 +61,8 @@ const frontendProxy = (req) => {
return errorResponse(frontendUrl, 500, 'No response from HTTP client');
}

const { status, message } = response;
const status = response.status;
const message = response.message;

if (status >= 400) {
log.info(`Error response from frontend for ${frontendUrl}: ${status} - ${message}`);
Expand All @@ -70,10 +74,17 @@ const frontendProxy = (req) => {
return errorResponse(frontendUrl, status, 'Redirects are not supported in editor view');
}

return response;
//return response;
} catch (e) {
return errorResponse(frontendUrl, 500, `Exception: ${e}`);
}*/
log.error(e);
//return errorResponse(frontendUrl, 500, `Exception: ${e}`);
}

return {
contentType: 'text/html',
body: `<div><h1>Success!</h1><p>Triggered the frontend proxy!</p></div>`,
status: 200,
};
};

exports.get = frontendProxy;
Expand Down

0 comments on commit e7c9dad

Please sign in to comment.