Skip to content

Commit f26fddb

Browse files
committed
Support sticky sessions configuration (source-academy#3416)
* Add new environment variable * Update example .env file
1 parent 7e3f8ca commit f26fddb

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
REACT_APP_DEPLOYMENT_NAME=Source Academy
22

33
REACT_APP_BACKEND_URL=http://localhost:4000
4+
REACT_APP_FORWARD_LOAD_BALANCER_COOKIES=FALSE
45
REACT_APP_USE_BACKEND=TRUE
56
REACT_APP_USE_EMPTY_ASSET_PREFIX=FALSE
67
REACT_APP_PLAYGROUND_ONLY=FALSE

src/commons/utils/Constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const storiesBackendUrl = process.env.REACT_APP_STORIES_BACKEND_URL;
2121
const cadetLoggerUrl = isTest ? undefined : process.env.REACT_APP_CADET_LOGGER;
2222
const cadetLoggerInterval = parseInt(process.env.REACT_APP_CADET_LOGGER_INTERVAL || '10000', 10);
2323
const useBackend = !isTest && isTrue(process.env.REACT_APP_USE_BACKEND);
24+
/**
25+
* Whether or not attach cookies to every API call. Useful for load balancers that
26+
* route requests based on cookies (e.g. for sticky sessions).
27+
*/
28+
const forwardLoadBalancerCookies = isTrue(process.env.REACT_APP_FORWARD_LOAD_BALANCER_COOKIES);
2429
const useEmptyAssetPrefix = isTrue(process.env.REACT_APP_USE_EMPTY_ASSET_PREFIX);
2530
const defaultSourceChapter = Chapter.SOURCE_4;
2631
const defaultSourceVariant = Variant.DEFAULT;
@@ -158,6 +163,7 @@ const Constants = {
158163
storiesBackendUrl,
159164
cadetLoggerUrl,
160165
useBackend,
166+
forwardLoadBalancerCookies,
161167
useEmptyAssetPrefix,
162168
defaultSourceChapter,
163169
defaultSourceVariant,

src/commons/utils/RequestHelper.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export const request = async (
5151
opts: RequestOptions,
5252
rawUrl?: string
5353
): Promise<Response | null> => {
54+
if (Constants.forwardLoadBalancerCookies) {
55+
// Always attach cookies to every API call
56+
opts.withCredentials = true;
57+
}
5458
const fetchOptions = generateApiCallHeadersAndFetchOptions(method, opts);
5559

5660
try {

0 commit comments

Comments
 (0)