Skip to content

Commit 0314f97

Browse files
Ihor MasechkoIhor Masechko
authored andcommitted
feat: make Redis session store optional
1 parent 2b838fb commit 0314f97

5 files changed

Lines changed: 64 additions & 33 deletions

File tree

docker-compose.yml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ services:
3737
ports:
3838
- "3000:3000"
3939
environment:
40-
- REDIS_URI=redis://redis:6379
4140
- BASE_URL=http://localhost:3000
4241
- NODE_ENV=development
4342
- APOS_MONGODB_URI=mongodb://mongodb:27017/apostrophe
@@ -61,7 +60,6 @@ services:
6160
depends_on:
6261
- mongodb
6362
- localstack
64-
- redis
6563
restart: unless-stopped
6664
healthcheck:
6765
test:
@@ -118,25 +116,6 @@ services:
118116
networks:
119117
- proxynet
120118

121-
# Redis for caching (optional, but recommended for production)
122-
redis:
123-
image: redis:7-alpine
124-
container_name: apostrophe-redis
125-
ports:
126-
- "6379:6379"
127-
volumes:
128-
- redis_data:/data
129-
command: redis-server --appendonly yes
130-
restart: unless-stopped
131-
healthcheck:
132-
test: ["CMD", "redis-cli", "ping"]
133-
interval: 10s
134-
timeout: 5s
135-
retries: 3
136-
networks:
137-
- proxynet
138-
139119
volumes:
140120
mongodb_data:
141-
redis_data:
142121
localstack_data:

website/app.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
const apostrophe = require('apostrophe');
22
require('dotenv').config({ path: '../.env' });
3-
const { getEnv } = require('./utils/env');
3+
const { getEnv, getOptionalEnv } = require('./utils/env');
44

55
function createAposConfig() {
6+
const redisUri = getOptionalEnv('REDIS_URI');
7+
8+
const sessionConfig = {
9+
secret: getEnv('SESSION_SECRET'),
10+
};
11+
12+
if (redisUri) {
13+
sessionConfig.store = {
14+
connect: require('connect-redis'),
15+
options: {
16+
url: redisUri,
17+
},
18+
};
19+
}
20+
621
return {
722
shortName: 'apostrophe-site',
823
baseUrl: process.env.BASE_URL || 'https://speedandfunction.com',
@@ -13,16 +28,7 @@ function createAposConfig() {
1328
'@apostrophecms/security-headers': {},
1429
'@apostrophecms/express': {
1530
options: {
16-
session: {
17-
// If using Redis (recommended for production)
18-
secret: getEnv('SESSION_SECRET'),
19-
store: {
20-
connect: require('connect-redis'),
21-
options: {
22-
url: getEnv('REDIS_URI'),
23-
},
24-
},
25-
},
31+
session: sessionConfig,
2632
csrf: {
2733
cookie: {
2834
key: '_csrf',

website/app.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ describe('createAposConfig', () => {
5858
});
5959
});
6060

61+
test('uses in-memory session store when REDIS_URI is not set', () => {
62+
delete process.env.REDIS_URI;
63+
64+
const config = createAposConfig();
65+
66+
expect(
67+
config.modules['@apostrophecms/express'].options.session.store,
68+
).toBeUndefined();
69+
});
70+
6171
// Define module categories for verification - moved outside the test
6272
const moduleCategories = [
6373
{

website/utils/env.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ const getEnv = (name) => {
77
return value;
88
};
99

10+
const getOptionalEnv = (name, defaultValue) => {
11+
const value = process.env[name];
12+
if (value === undefined) {
13+
return defaultValue;
14+
}
15+
return value;
16+
};
17+
1018
module.exports = {
1119
getEnv,
20+
getOptionalEnv,
1221
};

website/utils/env.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getEnv } = require('./env');
1+
const { getEnv, getOptionalEnv } = require('./env');
22

33
describe('getEnv utility', () => {
44
const OLD_ENV = process.env;
@@ -30,3 +30,30 @@ describe('getEnv utility', () => {
3030
}).toThrow('Environment variable "NON_EXISTENT_VAR" is not defined');
3131
});
3232
});
33+
34+
describe('getOptionalEnv utility', () => {
35+
const OLD_ENV = process.env;
36+
37+
beforeEach(() => {
38+
jest.resetModules();
39+
process.env = { ...OLD_ENV };
40+
});
41+
42+
afterAll(() => {
43+
process.env = OLD_ENV;
44+
});
45+
46+
test('should return environment variable value when it exists', () => {
47+
process.env.OPTIONAL_TEST_VAR = 'optional-test-value';
48+
49+
const result = getOptionalEnv('OPTIONAL_TEST_VAR');
50+
51+
expect(result).toBe('optional-test-value');
52+
});
53+
54+
test('should return default value when environment variable does not exist', () => {
55+
const result = getOptionalEnv('NON_EXISTENT_OPTIONAL_VAR', 'default');
56+
57+
expect(result).toBe('default');
58+
});
59+
});

0 commit comments

Comments
 (0)