Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/Options.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Options

The following environment variables are used by Ackee. You can also create a [`.env` file](https://www.npmjs.com/package/dotenv) in the root of the project to store all variables in one file.
The following environment variables are used by Ackee. You can also create a [`.env` file](https://www.npmjs.com/package/dotenv) in the root of the project to store all variables in one file. All environment
variables also support a `_FILE` suffix, which will fetch the value from the specified file path.

- [Database](#database)
- [Port](#port)
Expand Down
44 changes: 32 additions & 12 deletions src/utils/config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
'use static'

const fs = require('fs')
const { day } = require('./times')

// Must be a function or object that loads and returns the env variables at runtime.
// Otherwise it wouldn't be possible to mock the env variables with mockedEnv.
module.exports = new Proxy({}, {
get: function(target, prop) {
const data = {
ttl: process.env.ACKEE_TTL || day,
port: process.env.ACKEE_PORT || process.env.PORT || 3000,
dbUrl: process.env.ACKEE_MONGODB || process.env.MONGODB_URI,
allowOrigin: process.env.ACKEE_ALLOW_ORIGIN,
autoOrigin: process.env.ACKEE_AUTO_ORIGIN === 'true',
username: process.env.ACKEE_USERNAME,
password: process.env.ACKEE_PASSWORD,
isDemoMode: process.env.ACKEE_DEMO === 'true',
isDevelopmentMode: process.env.NODE_ENV === 'development',
isPreBuildMode: process.env.BUILD_ENV === 'pre',
ttl: configValueFromEnv('ACKEE_TTL') || day,
port: configValueFromEnv('ACKEE_PORT', 'PORT') || 3000,
dbUrl: configValueFromEnv('ACKEE_MONGODB', 'MONGODB_URI'),
allowOrigin: configValueFromEnv('ACKEE_ALLOW_ORIGIN'),
autoOrigin: configValueFromEnv('ACKEE_AUTO_ORIGIN') === 'true',
username: configValueFromEnv('ACKEE_USERNAME'),
password: configValueFromEnv('ACKEE_PASSWORD'),
isDemoMode: configValueFromEnv('ACKEE_DEMO') === 'true',
isDevelopmentMode: configValueFromEnv('NODE_ENV') === 'development',
isPreBuildMode: configValueFromEnv('BUILD_ENV') === 'pre',
}

return data[prop]
},
})
},
)

// Retrieves value of the environment variable, if it exists. We also check for
// another enviroment variable with the same name as the but with the suffix
// _FILE. If it exists, we read the value from the file and return it. This is
// useful for Docker secrets.
const configValueFromEnv = (...envVariable) => {
for (const env of envVariable) {
if (process.env[env]) {
return process.env[env]
}

const envFile = process.env[`${ env }_FILE`]
if (envFile) {
return fs.readFileSync(envFile, 'utf8')
}
}

return
}