Skip to content

Commit

Permalink
fix: port loading on heroku
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed Oct 24, 2020
1 parent 61b2a17 commit 0f918e9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/utils/configuration-loader.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@

import dotenv from 'dotenv'

export default async function load () {
const configuration = dotenv.config()
export default async function load (path) {
const result = dotenv.config({ path })

if (result.error) {
throw result.error
}

/**
* This function is async because we could
* load some KEYS from external services (like AWS Secrets Manager)
* in future
*/

configuration.fastify = {
logger: configuration.NODE_ENV !== 'production'
const configuration = {
...process.env,
fastify: {
logger: result.parsed.NODE_ENV !== 'production'
}
}

return configuration
Expand Down
32 changes: 32 additions & 0 deletions test/config-loader.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

import path from 'path'
import t from 'tap'
import { fileURLToPath } from 'url'

import configurationLoader from '../lib/utils/configuration-loader.mjs'

t.test('check config loaded', async t => {
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const config = await configurationLoader(path.join(__dirname, '../.env.example'))
t.like(config, {
NODE_ENV: 'development',
BASE_URL: 'http://localhost:3000',
DISCORD_CLIENT_ID: '12345678',
DISCORD_SECRET: 'XXXXXXXXXXXXXXXXX',
DB_URI: 'mongodb+srv://<user>:<password>@playground.xxxxx.mongodb.net/<dbname>?retryWrites=true&w=majority'
})
})

t.test('check PORT loaded', async t => {
const __dirname = path.dirname(fileURLToPath(import.meta.url))
process.env.PORT = '123456789'
const config = await configurationLoader(path.join(__dirname, '../.env.example'))
t.like(config, {
PORT: '123456789',
NODE_ENV: 'development',
BASE_URL: 'http://localhost:3000',
DISCORD_CLIENT_ID: '12345678',
DISCORD_SECRET: 'XXXXXXXXXXXXXXXXX',
DB_URI: 'mongodb+srv://<user>:<password>@playground.xxxxx.mongodb.net/<dbname>?retryWrites=true&w=majority'
})
})

0 comments on commit 0f918e9

Please sign in to comment.