Skip to content

Commit 9d085c5

Browse files
committed
Add logic to read an env var
1 parent bf31636 commit 9d085c5

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ RATE_LIMIT_WINDOW_SECONDS=60
66
# The maximum number of connections allowed within the rate limiting window.
77
# This sets the threshold for how many requests can be made in the specified time frame.
88
RATE_LIMIT_MAX_CONNECTIONS=1000
9+
USEMONGO=true

src/index.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,11 @@ app.use((req, res, next) => {
4848

4949
// Connect to MongoDB database
5050
const dbConnectionStr = dbUtils.getMongoConnectionString()
51+
const dbConnectionOptions = dbUtils.getMongoConnectionOptions()
5152

52-
try {
53-
// First attempt: try connecting with authMechanism
54-
mongoose.connect(dbConnectionStr, {
55-
authMechanism: 'SCRAM-SHA-1',
56-
tls: false
57-
})
58-
console.log('Connected to MongoDB with authMechanism.')
59-
} catch (error) {
60-
console.error('Failed to connect with authMechanism:', error.message)
61-
}
53+
// First attempt: try connecting with authMechanism
54+
mongoose.connect(dbConnectionStr, dbConnectionOptions)
6255

63-
// database connection
6456
const db = mongoose.connection
6557

6658
db.on('error', () => {

src/utils/db.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const config = require('config')
2-
2+
const { booleanIsTrue } = require('./utils')
33
const logger = require('../middleware/logger')
44

55
/* constructs MongoDB connection string
@@ -30,6 +30,19 @@ function getMongoConnectionString () {
3030
return `mongodb://${dbLoginPrepend}${dbHost}:${dbPort}/${dbName}`
3131
}
3232

33+
function getMongoConnectionOptions () {
34+
if (process.env.USEMONGO !== undefined && booleanIsTrue(process.env.USEMONGO)) {
35+
return {
36+
tls: false
37+
}
38+
}
39+
return {
40+
authMechanism: 'SCRAM-SHA-1',
41+
tls: false
42+
}
43+
}
44+
3345
module.exports = {
34-
getMongoConnectionString
46+
getMongoConnectionString,
47+
getMongoConnectionOptions
3548
}

0 commit comments

Comments
 (0)