-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# IDEA specific files | ||
.idea | ||
*.iml | ||
|
||
# Packages | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2016 KTH Royal Institute of Technology | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,52 @@ | ||
# kth-node-configuration | ||
|
||
Configuration module for Node.js projects. | ||
|
||
## Usage | ||
|
||
```javascript | ||
const configurator = require('kth-node-configuration') | ||
|
||
const config = configurator({ | ||
defaults: require('./config/defaults'), | ||
dev: require('./config/dev'), | ||
prod: require('./config/prod'), | ||
ref: require('./config/ref'), | ||
local: require('./config/local') | ||
}) | ||
|
||
module.exports = { | ||
full: config.full(), | ||
secure: config.secure(), | ||
safe: config.safe(), | ||
env: config.env() | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
- `defaults` should contain settings that will apply if no other config | ||
file has it set. It's recommended that this file contains a "skeleton" | ||
for the secure settings to document what the local settings can set. | ||
- `dev`, `ref`, and `prod` should contain environment specific settings. | ||
They will override the defaults. The configurator selects this file | ||
depending on the `process.env.NODE_ENV` variable. | ||
- `local` should contain settings that either shouldn't be checked into | ||
source control or local overrides specific to the machine running the | ||
app. | ||
|
||
## API | ||
|
||
- `full()` returns the fully merged configuration. | ||
- `secure()` returns only the secure merged configuration. | ||
- `safe()` returns the fully merged configuration with the secure | ||
section blanked out. This should be safe to use on the client-side. | ||
- `env()` returns the current enviroment setting. Will be one of the | ||
following: `dev`, `ref`, or `prod`. | ||
|
||
## Pro-tip! | ||
|
||
Use the [npm package __dotenv__][dotenv] to set environment variables. | ||
Take a look at the unit tests for example usage. | ||
|
||
[dotenv]: https://www.npmjs.com/package/dotenv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict' | ||
|
||
const _ = require('lodash') | ||
|
||
const defaults = { | ||
dev: {}, | ||
ref: {}, | ||
prod: {}, | ||
defaults: {}, | ||
local: {} | ||
} | ||
|
||
module.exports = function (options) { | ||
options = _.merge({}, defaults, options) | ||
|
||
function _getLowerCaseNodeEnv () { | ||
return process.env.NODE_ENV && process.env.NODE_ENV.toLowerCase() | ||
} | ||
|
||
function _merged () { | ||
return _.merge({}, options.defaults, options[env()], options.local) | ||
} | ||
|
||
function _isProduction (nodeEnv) { | ||
return nodeEnv === 'produktion' || nodeEnv === 'production' || nodeEnv === 'prod' | ||
} | ||
|
||
function _isReference (nodeEnv) { | ||
return nodeEnv === 'referens' || nodeEnv === 'reference' || nodeEnv === 'ref' | ||
} | ||
|
||
function _isDevelopment (nodeEnv) { | ||
return nodeEnv === 'development' || nodeEnv === 'dev' | ||
} | ||
|
||
function env () { | ||
const nodeEnv = _getLowerCaseNodeEnv() | ||
|
||
if (_isProduction(nodeEnv)) { | ||
return 'prod' | ||
} | ||
|
||
if (_isReference(nodeEnv)) { | ||
return 'ref' | ||
} | ||
|
||
if (_isDevelopment(nodeEnv)) { | ||
return 'dev' | ||
} | ||
|
||
throw new Error(`Invalid NODE_ENV variable: ${nodeEnv}`) | ||
} | ||
|
||
return { | ||
env: env, | ||
|
||
full: function () { | ||
return _merged() | ||
}, | ||
|
||
safe: function () { | ||
const full = this.full() | ||
full.secure = {} | ||
return full | ||
}, | ||
|
||
secure: function () { | ||
return this.full().secure | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "kth-node-configuration", | ||
"version": "1.0.0", | ||
"description": "Configuration module for Node.js projects", | ||
"main": "configurator.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/KTH/kth-node-configuration" | ||
}, | ||
"dependencies": { | ||
"lodash": "4.13.1" | ||
}, | ||
"keywords": [ | ||
"node", | ||
"configuration" | ||
], | ||
"private": true, | ||
"devDependencies": { | ||
"dotenv": "2.0.0", | ||
"standard": "7.1.0", | ||
"tap-spec": "4.1.1", | ||
"tape": "4.5.1" | ||
}, | ||
"scripts": { | ||
"test": "tape test/**/*Test.js | tap-spec", | ||
"codecheck": "standard", | ||
"preversion": "npm run codecheck && npm run test", | ||
"postversion": "git push && git push --tags" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
NODE_ENV=ref | ||
MONGODB_URL=mongodb://mongodb.local/dbname | ||
SESSION_SECRET=ref-secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Contains common configuration that could be used in any environment. | ||
// This is the first file that is processed and can be overridden by | ||
// environment files and local settings. | ||
module.exports = { | ||
secure: { | ||
mongodb: { | ||
url: '' | ||
}, | ||
|
||
session: '' | ||
}, | ||
|
||
common: 'app-name', | ||
name: 'configuration' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Contains configuration that should not be in source control. Anything | ||
// in here will override settings in the other files. | ||
module.exports = { | ||
secure: { | ||
mongodb: { | ||
url: 'mongodb://localhost/test' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// This file contains environment specific configuration that can be in | ||
// source control. Note the process.env.MONGODB_URL usage! Settings here | ||
// can still be overridden in the local file. | ||
module.exports = { | ||
secure: { | ||
mongodb: { | ||
url: process.env.MONGODB_URL | ||
}, | ||
|
||
session: process.env.SESSION_SECRET | ||
}, | ||
|
||
name: 'configuration-ref' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
const test = require('tape') | ||
|
||
// You can safely use dotenv to set environment variables. | ||
// Just make sure you do not check in the .env file to source control. | ||
require('dotenv').config({ path: path.join(__dirname, 'config/.env') }) | ||
|
||
const configurator = require('../../configurator') | ||
|
||
// get config files | ||
// const devConfig = require('./config/dev') | ||
const refConfig = require('./config/ref') | ||
// const prodConfig = require('./config/prod') | ||
const localConfig = require('./config/local') | ||
const defaultsConfig = require('./config/defaults') | ||
|
||
function create () { | ||
return configurator({ | ||
// prod: prodConfig, | ||
// dev: devConfig, | ||
ref: refConfig, | ||
defaults: defaultsConfig, | ||
local: localConfig | ||
}) | ||
} | ||
|
||
test('falls back to default value', (assert) => { | ||
const config = create() | ||
const full = config.full() | ||
const expected = 'configuration-ref' | ||
assert.equal(full.name, expected, 'should equal ref name setting') | ||
assert.end() | ||
}) | ||
|
||
test('hides secure options', (assert) => { | ||
const config = create() | ||
const safe = config.safe() | ||
const expected = { secure: {}, name: 'configuration-ref', common: 'app-name' } | ||
assert.deepEqual(safe, expected, 'should equal ref safe settings') | ||
assert.end() | ||
}) | ||
|
||
test('gets only secure options', (assert) => { | ||
const config = create() | ||
const secure = config.secure() | ||
const expected = { mongodb: { url: 'mongodb://localhost/test' }, session: process.env.SESSION_SECRET } | ||
assert.deepEqual(secure, expected, 'should equal ref secure settings') | ||
assert.end() | ||
}) | ||
|
||
test('gets correct environment', (assert) => { | ||
const config = create() | ||
const env = config.env() | ||
const full = config.full() | ||
const expectedName = 'configuration-ref' | ||
const expectedEnv = 'ref' | ||
const expectedSession = 'ref-secret' | ||
const expectedCommon = 'app-name' | ||
assert.equal(full.name, expectedName, 'should equal ref name setting') | ||
assert.equal(env, expectedEnv, 'should equal ref environment') | ||
assert.equal(full.secure.session, expectedSession, 'should equal session setting') | ||
assert.equal(full.common, expectedCommon, 'should use default common setting') | ||
assert.end() | ||
}) |