Skip to content

Commit

Permalink
Load configuration data from environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
apavamontri committed May 20, 2017
1 parent f898fd5 commit d962016
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
25 changes: 25 additions & 0 deletions domains/configuration-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class ConfigurationData {
get MongoDBUrl() {
return this.mongoDBUrl;
}
set MongoDBUrl(url) {
this.mongoDBUrl = url;
}
get NodeEnv() {
return this.nodeEnv;
}
set NodeEnv(environment) {
this.nodeEnv = environment;
}

toString() {
const output = {
MongoDBUrl: this.MongoDBUrl,
NodeEnv: this.NodeEnv,
};

return JSON.stringify(output, null, 2);
}
}

module.exports = ConfigurationData;
19 changes: 19 additions & 0 deletions infrastructures/environment-variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Environment variable class is responsible for loading environment variables
* from process and/or operating system so it is available to be used in
* this application
*/
const ConfigurationData = require('../domains/configuration-data');

class EnvironmentVariable {
load() {
const configurationData = new ConfigurationData();

configurationData.MongoDBUrl = process.env.MONGODB_URL;
configurationData.NodeEnv = process.env.NODE_ENV;

return configurationData;
}
}

module.exports = EnvironmentVariable;
11 changes: 11 additions & 0 deletions interfaces/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Configuration {
constructor(options) {
this.configurationAdapter = options.ConfigurationAdapter;
}

load() {
return this.configurationAdapter.load();
}
}

module.exports = Configuration;
22 changes: 20 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
const VersionInteractor = require('./usecases/version');
const WebServerInterface = require('./interfaces/webserver');
const EnvironmentVariables = require('./infrastructures/environment-variables');
const ExpressWebServer = require('./infrastructures/express-server');

const ConfigurationAdapter = require('./interfaces/configuration');
const WebServerInterface = require('./interfaces/webserver');

const ConfigurationInteractor = require('./usecases/configuration');
const VersionInteractor = require('./usecases/version');

const environmentVariable = new EnvironmentVariables();

const configurationAdapter = new ConfigurationAdapter({
ConfigurationAdapter: environmentVariable,
});

const configurationInteractor = new ConfigurationInteractor({
ConfigurationInterface: configurationAdapter,
});

const configuraionData = configurationInteractor.load();
console.log(configuraionData.toString());

const versionInteractor = new VersionInteractor();
const webserverInterface = new WebServerInterface({
VersionInteractor: versionInteractor,
Expand Down
10 changes: 10 additions & 0 deletions usecases/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Configuration {
constructor(options) {
this.configurationInterface = options.ConfigurationInterface;
}
load() {
return this.configurationInterface.load();
}
}

module.exports = Configuration;

0 comments on commit d962016

Please sign in to comment.