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

[Staging API] To Use Staging Server or not to use staging server #15517

Merged
merged 10 commits into from
Mar 6, 2023
2 changes: 2 additions & 0 deletions config/webpack/webpack.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module.exports = (env = {}) => portfinder.getPortPromise({port: BASE_PORT})
: {
proxy: {
'/api': 'http://[::1]:9000',
'/staging-api': 'http://[::1]:9000',
'/staging-secure-api': 'http://[::1]:9000',
'/chat-attachments': 'http://[::1]:9000',
},
};
Expand Down
4 changes: 4 additions & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,10 @@ const CONST = {
SAMPLE_INPUT: '123456.789',
EXPECTED_OUTPUT: 'FCFA 123,457',
},
API_MAP: {
STAGING: 'staging-',
STAGING_SECURE: 'staging-secure-',
},
};

export default CONST;
11 changes: 7 additions & 4 deletions src/libs/HttpUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import CONFIG from '../CONFIG';
import CONST from '../CONST';
import ONYXKEYS from '../ONYXKEYS';
import HttpsError from './Errors/HttpsError';
import shouldUseStagingServer from './shouldUseStagingServer';
import getPlatform from './getPlatform';

// Desktop and web use staging config too so we we should default to staging API endpoint if on those platforms
Expand Down Expand Up @@ -109,10 +108,14 @@ function xhr(command, data, type = CONST.NETWORK.METHOD.POST, shouldUseSecure =

let apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.URL_API_ROOT;

if (shouldUseStagingServer(stagingServerToggleState)) {
apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.STAGING_SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.STAGING_EXPENSIFY_URL;
if (stagingServerToggleState) {
Prince-Mendiratta marked this conversation as resolved.
Show resolved Hide resolved
// If web proxy is used, URL will always be /
if (CONFIG.EXPENSIFY.URL_API_ROOT === '/') {
Prince-Mendiratta marked this conversation as resolved.
Show resolved Hide resolved
apiRoot += shouldUseSecure ? CONST.API_MAP.STAGING_SECURE : CONST.API_MAP.STAGING;
Prince-Mendiratta marked this conversation as resolved.
Show resolved Hide resolved
} else {
apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.STAGING_SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.STAGING_EXPENSIFY_URL;
}
}

return processHTTPRequest(`${apiRoot}api?command=${command}`, type, formData, data.canCancel);
}

Expand Down
13 changes: 0 additions & 13 deletions src/libs/shouldUseStagingServer/index.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/libs/shouldUseStagingServer/index.native.js

This file was deleted.

38 changes: 27 additions & 11 deletions web/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ if (process.env.USE_WEB_PROXY === 'false') {
process.stdout.write('Skipping proxy as USE_WEB_PROXY was set to false.\n');
process.exit();
}

let host = 'www.expensify.com';

// If we are testing against the staging API then we must use the correct host here or nothing with work.
if (/staging/.test(process.env.EXPENSIFY_URL)) {
host = 'staging.expensify.com';
}
const host = new URL(process.env.EXPENSIFY_URL || 'https://www.expensify.com').hostname;
const stagingHost = new URL(process.env.STAGING_EXPENSIFY_URL || 'https://staging.expensify.com').hostname;
const stagingSecureHost = new URL(process.env.STAGING_SECURE_EXPENSIFY_URL || 'https://staging-secure.expensify.com').hostname;
const HOST_MAP = {
'staging-api': stagingHost,
'staging-secure-api': stagingSecureHost,
};

// eslint-disable-next-line no-console
console.log(`Creating proxy with host: ${host}`);
console.log(`Creating proxy with host: ${host} for production API and ${stagingHost} for staging API`);

/**
* Local proxy server that hits the production endpoint
Expand All @@ -24,13 +24,29 @@ console.log(`Creating proxy with host: ${host}`);
* environment that has no local API.
*/
const server = http.createServer((request, response) => {
let hostname = host;
let requestPath = request.url;

// regex not declared globally to avoid internal regex pointer reset for each request
// We only match for staging related root since default host is prod
Prince-Mendiratta marked this conversation as resolved.
Show resolved Hide resolved
const apiRegex = /\/(staging.*api)/g;
const apiRootMatch = apiRegex.exec(request.url);

// Switch host only if API call, not on chat attachments
if (apiRootMatch) {
const apiRoot = apiRootMatch[1];
hostname = HOST_MAP[apiRoot];

// replace the mapping url with the actual path
requestPath = request.url.replace(apiRoot, 'api');
}
const proxyRequest = https.request({
hostname: host,
hostname,
method: 'POST',
path: request.url,
path: requestPath,
headers: {
...request.headers,
host,
host: hostname,
'user-agent': request.headers['user-agent'].concat(' Development-NewDot/1.0'),
},
port: 443,
Expand Down