Skip to content

Commit

Permalink
Merge pull request #10 from Eomm/master
Browse files Browse the repository at this point in the history
fix port loading
  • Loading branch information
Eomm committed Oct 24, 2020
2 parents b850d1a + bef3821 commit 1103972
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ It will target Discord just for fun and to add some not-so-useful features.
Every upgrade will be documented and posted on [dev.to](https://dev.to/eomm)
and here you will find the source code and the commit history!

To see the last version of the application, it is published at [https://fastify-discord-app-demo.herokuapp.com/](https://fastify-discord-app-demo.herokuapp.com/)

## Posts

1. [A Discord app with Fastify!](https://dev.to/eomm/a-discord-app-with-fastify-3h8c) - [📝](./posts/01-init-application.md)
Expand Down
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 1103972

Please sign in to comment.