allow overriding the backend url on frontend w/ burnettk

This commit is contained in:
jasquat 2023-04-26 13:31:12 -04:00
parent a1a2af3020
commit c10cc5035c

View File

@ -12,6 +12,7 @@ declare global {
let spiffEnvironment = ''; let spiffEnvironment = '';
let appRoutingStrategy = 'subdomain_based'; let appRoutingStrategy = 'subdomain_based';
let backendBaseUrl = null;
if ('spiffworkflowFrontendJsenv' in window) { if ('spiffworkflowFrontendJsenv' in window) {
if ('APP_ROUTING_STRATEGY' in window.spiffworkflowFrontendJsenv) { if ('APP_ROUTING_STRATEGY' in window.spiffworkflowFrontendJsenv) {
appRoutingStrategy = window.spiffworkflowFrontendJsenv.APP_ROUTING_STRATEGY; appRoutingStrategy = window.spiffworkflowFrontendJsenv.APP_ROUTING_STRATEGY;
@ -19,18 +20,22 @@ if ('spiffworkflowFrontendJsenv' in window) {
if ('ENVIRONMENT_IDENTIFIER' in window.spiffworkflowFrontendJsenv) { if ('ENVIRONMENT_IDENTIFIER' in window.spiffworkflowFrontendJsenv) {
spiffEnvironment = window.spiffworkflowFrontendJsenv.ENVIRONMENT_IDENTIFIER; spiffEnvironment = window.spiffworkflowFrontendJsenv.ENVIRONMENT_IDENTIFIER;
} }
if ('BACKEND_BASE_URL' in window.spiffworkflowFrontendJsenv) {
backendBaseUrl = window.spiffworkflowFrontendJsenv.BACKEND_BASE_URL;
}
} }
let hostAndPortAndPathPrefix; if (!backendBaseUrl) {
if (appRoutingStrategy === 'subdomain_based') { let hostAndPortAndPathPrefix;
if (appRoutingStrategy === 'subdomain_based') {
hostAndPortAndPathPrefix = `api.${hostname}`; hostAndPortAndPathPrefix = `api.${hostname}`;
} else if (appRoutingStrategy === 'path_based') { } else if (appRoutingStrategy === 'path_based') {
hostAndPortAndPathPrefix = `${hostname}/api`; hostAndPortAndPathPrefix = `${hostname}/api`;
} else { } else {
throw new Error(`Invalid app routing strategy: ${appRoutingStrategy}`); throw new Error(`Invalid app routing strategy: ${appRoutingStrategy}`);
} }
if (/^\d+\./.test(hostname) || hostname === 'localhost') { if (/^\d+\./.test(hostname) || hostname === 'localhost') {
let serverPort = 7000; let serverPort = 7000;
if (!Number.isNaN(Number(port))) { if (!Number.isNaN(Number(port))) {
serverPort = Number(port) - 1; serverPort = Number(port) - 1;
@ -44,25 +49,23 @@ if (/^\d+\./.test(hostname) || hostname === 'localhost') {
// eslint-disable-next-line prefer-destructuring // eslint-disable-next-line prefer-destructuring
spiffEnvironment = hostname.split('.')[0]; spiffEnvironment = hostname.split('.')[0];
} }
}
backendBaseUrl = `${protocol}://${hostAndPortAndPathPrefix}/v1.0`;
// this can only ever work locally since this is a static site.
// use spiffworkflowFrontendJsenv if you want to inject env vars
// that can be read by the static site.
if (process.env.REACT_APP_BACKEND_BASE_URL) {
backendBaseUrl = process.env.REACT_APP_BACKEND_BASE_URL;
}
} }
if ( if (!backendBaseUrl.endsWith('/v1.0')) {
'spiffworkflowFrontendJsenv' in window && backendBaseUrl += '/v1.0';
'APP_ROUTING_STRATEGY' in window.spiffworkflowFrontendJsenv
) {
appRoutingStrategy = window.spiffworkflowFrontendJsenv.APP_ROUTING_STRATEGY;
} }
let url = `${protocol}://${hostAndPortAndPathPrefix}/v1.0`; export const BACKEND_BASE_URL = backendBaseUrl;
// this can only ever work locally since this is a static site.
// use spiffworkflowFrontendJsenv if you want to inject env vars
// that can be read by the static site.
if (process.env.REACT_APP_BACKEND_BASE_URL) {
url = process.env.REACT_APP_BACKEND_BASE_URL;
}
export const BACKEND_BASE_URL = url;
export const PROCESS_STATUSES = [ export const PROCESS_STATUSES = [
'not_started', 'not_started',