This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for enviroment variables configuration and add Heroku Pro…
…cfile.
- Loading branch information
Showing
5 changed files
with
220 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 @@ | ||
worker: PORT=$LIMITD_PORT node bin/limitd |
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 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 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,67 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const schema = require('./config.schema'); | ||
const propertyNames = Object.keys(schema.properties); | ||
|
||
function parseString(name, value) { | ||
const subschema = schema.properties[name]; | ||
|
||
return parseStringToSpecificType(value, subschema.type); | ||
} | ||
|
||
function parseBucketString(name, value) { | ||
const objectName = Object.keys(schema.properties.buckets.patternProperties)[0]; | ||
const subschema = schema.properties.buckets.patternProperties[objectName].properties[name]; | ||
|
||
return parseStringToSpecificType(value, subschema.type); | ||
} | ||
|
||
function parseStringToSpecificType(value, type) { | ||
let parsed; | ||
if (type === 'string' || Array.isArray(type) && type.indexOf('string') > -1) { | ||
return value; | ||
} | ||
switch (type) { | ||
case 'integer': | ||
parsed = Number(value); | ||
break; | ||
case 'boolean': | ||
parsed = Boolean(value); | ||
break; | ||
} | ||
|
||
return parsed; | ||
} | ||
|
||
module.exports.parse = function (env) { | ||
const keys = Object.keys(env); | ||
const config = {}; | ||
const buckets = {}; | ||
|
||
keys.forEach(key => { | ||
if (key.indexOf('BUCKET') === 0) { | ||
const id = key.split('_')[1]; | ||
if (key !== `BUCKET_${id}_NAME`) { | ||
const bucketName = env[`BUCKET_${id}_NAME`]; | ||
let bucket = buckets[bucketName]; | ||
if (!bucket) { | ||
bucket = {}; | ||
buckets[bucketName] = bucket; | ||
} | ||
const propertyName = key.replace(`BUCKET_${id}_`, '').toLowerCase(); | ||
bucket[propertyName] = parseBucketString(propertyName, env[key]); | ||
} | ||
|
||
} else if(propertyNames.indexOf(key.toLowerCase()) > -1) { | ||
const propertyName = key.toLowerCase(); | ||
config[propertyName] = parseString(propertyName, env[key]); | ||
} | ||
}); | ||
|
||
if (!_.isEmpty(buckets)) { | ||
config.buckets = buckets; | ||
} | ||
|
||
return config; | ||
}; |
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,112 @@ | ||
const expect = require('chai').expect; | ||
const parser = require('../conf/config.env.parser'); | ||
|
||
describe('config env parser', function() { | ||
describe('with a complete well formed environment', function () { | ||
const env = { | ||
PORT: '9001', | ||
BUCKET_1_NAME: 'user', | ||
BUCKET_1_SIZE: '1', | ||
BUCKET_1_PER_MINUTE: '5', | ||
BUCKET_1_PURPOSE: 'user creation bucket', | ||
BUCKET_2_NAME: 'foo', | ||
BUCKET_2_SIZE: '10', | ||
BUCKET_2_PER_MINUTE: '15', | ||
BUCKET_2_MATCH: '^[a-zA-Z0-9]+$', | ||
BUCKET_3_NAME: 'bar', | ||
BUCKET_3_UNLIMITED: 'true', | ||
DB: './database' | ||
}; | ||
|
||
it('should parse it ok', function() { | ||
const config = parser.parse(env); | ||
expect(Object.keys(config).length).to.equal(3); | ||
expect(config.port).to.equal('9001'); | ||
expect(config.db).to.equal('./database'); | ||
expect(config.buckets).to.deep.equal({ | ||
user: { size: 1, per_minute: 5, purpose: 'user creation bucket' }, | ||
foo: { size: 10, per_minute: 15, match: '^[a-zA-Z0-9]+$' }, | ||
bar: { unlimited: true } | ||
}); | ||
}); | ||
}); | ||
|
||
describe('with just the buckets configuration in the environment', function () { | ||
const env = { | ||
BUCKET_1_NAME: 'user', | ||
BUCKET_1_SIZE: 1, | ||
BUCKET_1_PER_MINUTE: 5, | ||
BUCKET_2_NAME: 'foo', | ||
BUCKET_2_SIZE: 10, | ||
BUCKET_2_PER_MINUTE: 15, | ||
BUCKET_3_NAME: 'bar', | ||
BUCKET_3_SIZE: 13, | ||
BUCKET_3_PER_SECOND: 25, | ||
}; | ||
|
||
it('should parse it ok', function() { | ||
const config = parser.parse(env); | ||
expect(Object.keys(config).length).to.equal(1); | ||
expect(config.buckets).to.deep.equal({ | ||
user: { size: 1, per_minute: 5 }, | ||
foo: { size: 10, per_minute: 15 }, | ||
bar: { size: 13, per_second: 25 } | ||
}); | ||
}); | ||
}); | ||
|
||
describe('with just the buckets configuration in the environment in a random order', function () { | ||
const env = { | ||
BUCKET_3_PER_SECOND: 25, | ||
BUCKET_1_SIZE: 1, | ||
BUCKET_2_NAME: 'foo', | ||
BUCKET_1_PER_MINUTE: 5, | ||
BUCKET_2_SIZE: 10, | ||
BUCKET_3_NAME: 'bar', | ||
BUCKET_2_PER_MINUTE: 15, | ||
BUCKET_1_NAME: 'user', | ||
BUCKET_3_SIZE: 13, | ||
}; | ||
|
||
it('should parse it ok', function() { | ||
const config = parser.parse(env); | ||
expect(Object.keys(config).length).to.equal(1); | ||
expect(config.buckets).to.deep.equal({ | ||
user: { size: 1, per_minute: 5 }, | ||
foo: { size: 10, per_minute: 15 }, | ||
bar: { size: 13, per_second: 25 } | ||
}); | ||
}); | ||
}); | ||
|
||
describe('with not supported additional properties', function () { | ||
const env = { | ||
PORT: '9001', | ||
BUCKET_1_NAME: 'user', | ||
BUCKET_1_SIZE: '1', | ||
BUCKET_1_PER_MINUTE: '5', | ||
BUCKET_2_NAME: 'foo', | ||
BUCKET_2_SIZE: '10', | ||
BUCKET_2_PER_MINUTE: '15', | ||
BUCKET_3_NAME: 'bar', | ||
BUCKET_3_SIZE: '13', | ||
BUCKET_3_PER_SECOND: '25', | ||
DB: './database', | ||
|
||
ADDITIONAL_PROPERTY_1: 'baz', | ||
additional_property_2: 'taz' | ||
}; | ||
|
||
it('should not include additional properties', function() { | ||
const config = parser.parse(env); | ||
expect(Object.keys(config).length).to.equal(3); | ||
expect(config.port).to.equal('9001'); | ||
expect(config.db).to.equal('./database'); | ||
expect(config.buckets).to.deep.equal({ | ||
user: { size: 1, per_minute: 5 }, | ||
foo: { size: 10, per_minute: 15 }, | ||
bar: { size: 13, per_second: 25 } | ||
}); | ||
}); | ||
}); | ||
}); |