Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:Storj/bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon Hall committed Oct 17, 2016
2 parents 6f53fc7 + f5998cf commit 5dfefab
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 51 deletions.
13 changes: 11 additions & 2 deletions bin/storj-bridge.js
Expand Up @@ -2,13 +2,22 @@

'use strict';

const program = require('commander');
const Config = require('../lib/config');
const Engine = require('../lib/engine');

module.exports = Engine(Config(process.env.NODE_ENV || 'develop'));
program.version(require('../package').version);
program.option('-c, --config <path_to_config_file>', 'path to the config file');
program.option('-d, --datadir <path_to_datadir>', 'path to the data directory');
program.parse(process.argv);

module.exports.start(function(err) {
var config = new Config(process.env.NODE_ENV || 'develop', program.config, program.datadir);
var engine = new Engine(config);

engine.start(function(err) {
if (err) {
console.log(err);
}
});

module.exports = engine;
129 changes: 80 additions & 49 deletions lib/config.js
@@ -1,66 +1,20 @@
'use strict';

const assert = require('assert');
const os = require('os');
const fs = require('fs');
const path = require('path');
const merge = require('merge');

const ENV = process.env;
const PLATFORM = os.platform();
const DIRNAME = '.storj-bridge';
const HOME = PLATFORM === 'win32' ? ENV.USERPROFILE : ENV.HOME;
const STORJ_BRIDGE_PATH = ENV.STORJ_BRIDGE_DIR || HOME;
const DATADIR = path.join(STORJ_BRIDGE_PATH, DIRNAME);
const CONFDIR = path.join(DATADIR, 'config');
const ITEMDIR = path.join(DATADIR, 'items');
const CONSTANTS = require('./constants');

if (!fs.existsSync(DATADIR)) {
fs.mkdirSync(DATADIR);
}

if (!fs.existsSync(CONFDIR)) {
fs.mkdirSync(CONFDIR);
}

if (!fs.existsSync(ITEMDIR)) {
fs.mkdirSync(ITEMDIR);
}

/**
* Represents a configuration
* @constructor
* @param {String|Object} env
*/
function Config(env) {
if (!(this instanceof Config)) {
return new Config(env);
}

var config;

if (typeof env === 'string') {
var envConfigPath = path.join(CONFDIR, env);

if (!fs.existsSync(envConfigPath)) {
fs.writeFileSync(envConfigPath, JSON.stringify(Config.DEFAULTS, null, 2));
}

config = merge(
Object.create(Config.DEFAULTS),
JSON.parse(fs.readFileSync(envConfigPath))
);
} else {
config = merge(Object.create(Config.DEFAULTS), env);
}

for (let prop in config) {
if (config.hasOwnProperty(prop)) {
this[prop] = config[prop];
}
}
}

Config.DEFAULTS = {
const DEFAULTS = {
application: {
mirrors: 6,
privateKey: null
Expand Down Expand Up @@ -109,4 +63,81 @@ Config.DEFAULTS = {
}
};

function getPaths(env, confpath, datadir) {
var paths = {};
if (datadir) {
assert(path.isAbsolute(datadir), 'datadir is expected to be absolute');
paths.datadir = datadir;
} else {
paths.datadir = DATADIR;
}
if (confpath) {
assert(path.isAbsolute(confpath), 'confpath is expected to be absolute');
paths.confdir = path.dirname(confpath);
paths.confpath = confpath;
} else {
paths.confdir = path.join(paths.datadir, 'config');
assert(env, 'env is expected without config path');
paths.confpath = path.join(paths.confdir, env);
}
return paths;
}

function setupConfig(paths) {
if (!fs.existsSync(paths.confdir)) {
fs.mkdirSync(paths.confdir);
}
if (!fs.existsSync(paths.confpath)) {
fs.writeFileSync(paths.confpath, JSON.stringify(DEFAULTS, null, 2));
}
}

function setupDataDirectory(paths) {
if (!fs.existsSync(paths.datadir)) {
fs.mkdirSync(paths.datadir);
}
var itemdir = path.join(paths.datadir, 'items');
if (!fs.existsSync(itemdir)) {
fs.mkdirSync(itemdir);
}
}

/**
* Represents a configuration
* @constructor
* @param {String|Object} arg
*/
function Config(env, confpath, datadir) {
if (!(this instanceof Config)) {
return new Config(env, confpath, datadir);
}

var config;

if (typeof env === 'string') {

var paths = Config.getPaths(env, confpath, datadir);
Config.setupDataDirectory(paths);
Config.setupConfig(paths);

config = merge(
Object.create(DEFAULTS),
JSON.parse(fs.readFileSync(paths.confpath))
);

} else {
config = merge(Object.create(DEFAULTS), env);
}

for (let prop in config) {
this[prop] = config[prop];
}

}

Config.DEFAULTS = DEFAULTS;
Config.setupDataDirectory = setupDataDirectory;
Config.setupConfig = setupConfig;
Config.getPaths = getPaths;

module.exports = Config;
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -54,6 +54,7 @@
},
"dependencies": {
"async": "^1.5.2",
"commander": "^2.9.0",
"cors": "^2.7.1",
"csv-write-stream": "^2.0.0",
"elliptic": "^6.0.2",
Expand Down
102 changes: 102 additions & 0 deletions test/config.unit.js
@@ -1,9 +1,11 @@
'use strict';

const fs = require('fs');
const crypto = require('crypto');
const path = require('path');
const os = require('os');
const expect = require('chai').expect;
const sinon = require('sinon');

const Config = require('..').Config;

Expand All @@ -27,13 +29,18 @@ describe('Config', function() {
});

describe('@constructor', function() {
var sandbox = sinon.sandbox.create();

before(function() {
if (fs.existsSync(path.join(CONFDIR, '__tmptest'))) {
fs.unlinkSync(path.join(CONFDIR, '__tmptest'));
}
});

afterEach(function() {
sandbox.restore();
});

it('should create a config instance with the defaults', function() {
var config = new Config('__tmptest');
expect(JSON.stringify(config)).to.equal(JSON.stringify(Config.DEFAULTS));
Expand All @@ -44,6 +51,101 @@ describe('Config', function() {
expect(fs.existsSync(path.join(CONFDIR, '__tmptest'))).to.equal(true);
});

it('will create from an object', function() {
sandbox.stub(Config, 'getPaths');
sandbox.stub(Config, 'setupDataDirectory');
sandbox.stub(Config, 'setupConfig');
var options = {
hello: 'world'
};
var config = new Config(options);
expect(config.hello).to.equal('world');
expect(config.application);
expect(config.storage);
expect(config.server);
expect(config.complex);
expect(config.logger);
expect(config.mailer);
});

});

describe('@setupDataDirectory', function() {
function runTest() {
var paths = {
datadir: '/tmp/storj-test-' + crypto.randomBytes(4).toString('hex')
};
Config.setupDataDirectory(paths);
expect(fs.existsSync(paths.datadir)).to.equal(true);
expect(fs.existsSync(paths.datadir + '/items')).to.equal(true);
}
it('will make directory if it does not exist', function() {
runTest();
});
it('will NOT make directory if it already exists', function() {
runTest();
});
});

describe('@setupConfig', function() {
function runTest() {
var confdir = '/tmp/storj-testconf-' + crypto.randomBytes(4).toString('hex');
var paths = {
confdir: confdir,
confpath: confdir + '/develop'
};
Config.setupConfig(paths);
expect(fs.existsSync(paths.confdir)).to.equal(true);
expect(fs.existsSync(paths.confpath)).to.equal(true);
}
it('will create config directory and file if does not exist', function() {
runTest();
});
it('will NOT create config directory and file exists', function() {
runTest();
});
});

describe('@getPaths', function() {
it('it will use defaults if confpath and datadir are undefined', function() {
var paths = Config.getPaths('development');
expect(paths.datadir).to.equal(process.env.HOME + '/.storj-bridge');
expect(paths.confdir).to.equal(process.env.HOME + '/.storj-bridge/config');
expect(paths.confpath).to.equal(process.env.HOME + '/.storj-bridge/config/development');
});
it('it will use confpath and datadir if defined', function() {
var paths = Config.getPaths('development', '/tmp/etc/storj/bridge', '/tmp/var/storj/bridge');
expect(paths.datadir).to.equal('/tmp/var/storj/bridge');
expect(paths.confdir).to.equal('/tmp/etc/storj');
expect(paths.confpath).to.equal('/tmp/etc/storj/bridge');
});
it('it will use datadir and default config directory', function() {
var paths = Config.getPaths('development', null, '/tmp/var/storj/bridge');
expect(paths.datadir).to.equal('/tmp/var/storj/bridge');
expect(paths.confdir).to.equal('/tmp/var/storj/bridge/config');
expect(paths.confpath).to.equal('/tmp/var/storj/bridge/config/development');
});
it('it will use confpath and default datadir', function() {
var paths = Config.getPaths('development', '/tmp/etc/storj/bridge', null);
expect(paths.datadir).to.equal(process.env.HOME + '/.storj-bridge');
expect(paths.confdir).to.equal('/tmp/etc/storj');
expect(paths.confpath).to.equal('/tmp/etc/storj/bridge');
});
it('will throw if datadir and missing env', function() {
expect(function() {
Config.getPaths(null, null, '/tmp/var/storj/bridge');
}).to.throw('env is expected without config path');
});
it('will throw datadir is not absolute', function() {
expect(function() {
Config.getPaths(null, null, 'tmp/var/storj/bridge');
}).to.throw('datadir is expected to be absolute');
});
it('will throw confpath is not absolute', function() {
expect(function() {
Config.getPaths(null, 'tmp/etc/storj/bridge', null);
}).to.throw('confpath is expected to be absolute');
});
});

});

0 comments on commit 5dfefab

Please sign in to comment.