-
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.
Merge pull request #1 from KTH/refactor/env-style-config
Refactor of configuration to simplify config-files
- Loading branch information
Showing
10 changed files
with
218 additions
and
105 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
This file was deleted.
Oops, something went wrong.
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 @@ | ||
'use strict' | ||
|
||
const { getEnv } = require('./utils') | ||
const urlgrey = require('urlgrey') | ||
|
||
module.exports = function (envVarName, defaultUri) { | ||
return urlgrey(getEnv(envVarName, defaultUri)) | ||
} |
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,32 @@ | ||
'use strict' | ||
|
||
const deepAssign = require('deep-assign') | ||
|
||
function _currentNodeEnv () { | ||
const nodeEnv = process.env.NODE_ENV && process.env.NODE_ENV.toLowerCase() | ||
|
||
if (nodeEnv === 'production') { | ||
return 'prod' | ||
} else if (nodeEnv === 'referens' || nodeEnv === 'reference' || nodeEnv === 'ref') { | ||
return 'ref' | ||
} else if (nodeEnv === 'development' || nodeEnv === 'dev' || !nodeEnv) { | ||
return 'dev' | ||
} | ||
|
||
// To run Node in production mode NODE_currentNodeEnv must be 'production' | ||
throw new Error(`Invalid NODE_currentNodeEnv variable: ${nodeEnv}`) | ||
} | ||
|
||
module.exports = function (inpArr) { | ||
// Create array of source objects | ||
const mergeThese = inpArr.slice() | ||
// Add target object | ||
const mergedObj = {} | ||
mergeThese.unshift(mergedObj) | ||
// Merge all objects | ||
deepAssign.apply(this, mergeThese) | ||
// Add the env variable | ||
mergedObj.env = _currentNodeEnv() | ||
|
||
return mergedObj | ||
} |
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 @@ | ||
module.exports = { | ||
generateConfig: require('./generateConfig'), | ||
decodeUri: require('./decodeUri'), | ||
unpackLDAPConfig: require('./unpackLDAPConfig'), | ||
unpackNodeApiConfig: require('./unpackNodeApiConfig'), | ||
unpackRedisConfig: require('./unpackRedisConfig'), | ||
getEnv: require('./utils').getEnv | ||
} |
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,40 @@ | ||
'use strict' | ||
|
||
const { getEnv, typeConversion } = require('./utils') | ||
const urlgrey = require('urlgrey') | ||
|
||
module.exports = function (envVarName, password, defaultUri, options) { | ||
const envObj = urlgrey(getEnv(envVarName, defaultUri)) | ||
|
||
const outp = { | ||
uri: `${envObj.protocol()}://${envObj.hostname()}`, | ||
username: envObj.username(), | ||
password: password | ||
} | ||
|
||
if (typeof options === 'object') { | ||
Object.assign(outp, options) | ||
} | ||
|
||
if (typeof envObj.query === 'object') { | ||
var tmpQuery = envObj.query() | ||
Object.keys(tmpQuery).forEach((key) => { | ||
outp[key] = typeConversion(tmpQuery[key]) | ||
}) | ||
} | ||
return outp | ||
} | ||
|
||
module.exports.defaultSettings = { | ||
ldapClient: { | ||
version: 3, | ||
searchlimit: 10, | ||
searchtimeout: 10, // seconds | ||
connecttimeout: 3000, // ms | ||
timeout: 3000, // ms | ||
maxconnections: 10, // | ||
checkinterval: 10000, // ms | ||
maxidletime: 300000, // ms | ||
scope: 'sub' | ||
} | ||
} |
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,27 @@ | ||
'use strict' | ||
|
||
const { getEnv, typeConversion } = require('./utils') | ||
const urlgrey = require('urlgrey') | ||
|
||
module.exports = function (envVarName, defaultUri, options) { | ||
const envObj = urlgrey(getEnv(envVarName, defaultUri)) | ||
const outp = { | ||
https: envObj.protocol() === 'https', | ||
// Netscaler doesn't answer https calls properly if we pass the port to kth-node-api-call | ||
port: (envObj.protocol() === 'https' ? undefined : envObj.port()), | ||
host: envObj.hostname(), | ||
proxyBasePath: envObj.path() | ||
} | ||
|
||
if (typeof options === 'object') { | ||
Object.assign(outp, options) | ||
} | ||
|
||
if (typeof envObj.query === 'object') { | ||
var tmpQuery = envObj.query() | ||
Object.keys(tmpQuery).forEach((key) => { | ||
outp[key] = typeConversion(tmpQuery[key]) | ||
}) | ||
} | ||
return outp | ||
} |
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,25 @@ | ||
'use strict' | ||
|
||
const { getEnv, typeConversion } = require('./utils') | ||
const urlgrey = require('urlgrey') | ||
|
||
module.exports = function (envVarName, defaultUri, options) { | ||
const envObj = urlgrey(getEnv(envVarName, defaultUri)) | ||
|
||
const outp = { | ||
host: envObj.hostname(), | ||
port: envObj.port() | ||
} | ||
|
||
if (typeof options === 'object') { | ||
Object.assign(outp, options) | ||
} | ||
|
||
if (typeof envObj.query === 'object') { | ||
var tmpQuery = envObj.query() | ||
Object.keys(tmpQuery).forEach((key) => { | ||
outp[key] = typeConversion(tmpQuery[key]) | ||
}) | ||
} | ||
return outp | ||
} |
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,19 @@ | ||
'use strict' | ||
|
||
module.exports.getEnv = function (name, defaultVal) { | ||
return process.env[name] || defaultVal | ||
} | ||
|
||
module.exports.typeConversion = function (inp) { | ||
if (parseInt(inp).toString === inp) { | ||
return parseInt(inp) | ||
} else if (inp === 'true') { | ||
return true | ||
} else if (inp === 'false') { | ||
return false | ||
} else if (inp === 'undefined') { | ||
return undefined | ||
} else { | ||
return inp | ||
} | ||
} |
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