Skip to content

Commit 04718cd

Browse files
committed
moved redis connection to db folder
1 parent a8e2599 commit 04718cd

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/app.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
const Koa = require('koa');
33
const cache = require('koa-redis-cache');
4+
const options = require('./db/redis');
45
const compress = require('koa-compress');
56
const Logger = require('koa-logger');
67
const Cors = require('@koa/cors');
78
const Helmet = require('koa-helmet');
89
const MongoClient = require('mongodb');
910
const json = require('koa-json');
10-
const urlParse = require('url');
1111

1212
const home = require('./v2-routes/v2-home');
1313
const info = require('./v2-routes/v2-info');
@@ -35,21 +35,6 @@ app.use(Cors());
3535
// Add pretty output option for debugging
3636
app.use(json({ pretty: false, param: 'pretty' }));
3737

38-
// Redis cache options
39-
// 90 minute TTL
40-
const redisURL = urlParse.parse(process.env.REDISCLOUD_URL || 'redis://default:default@localhost:6379');
41-
42-
const options = {
43-
expire: 5000,
44-
redis: {
45-
host: redisURL.hostname,
46-
port: redisURL.port,
47-
options: {
48-
password: redisURL.auth.split(':')[1],
49-
},
50-
},
51-
};
52-
5338
// Hide logging when running tests
5439
// Disable Redis caching when running tests
5540
if (process.env.NODE_ENV !== 'test') {

src/db/redis.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
const url = require('url');
3+
4+
/**
5+
* Declare options for Redis on Heroku
6+
*/
7+
8+
let options;
9+
if (process.env.REDISCLOUD_URL) {
10+
const redisURL = url.parse(process.env.REDISCLOUD_URL);
11+
options = {
12+
expire: 5000,
13+
redis: {
14+
host: redisURL.hostname,
15+
port: redisURL.port,
16+
options: {
17+
password: 'null' || redisURL.auth.split(':')[1],
18+
},
19+
},
20+
};
21+
} else {
22+
options = {
23+
expire: 5000,
24+
};
25+
}
26+
27+
module.exports = options;

0 commit comments

Comments
 (0)