2018-05-23 19:10:53 +00:00
|
|
|
const fs = require('../../core/fs');
|
|
|
|
const utils = require('../../utils/utils');
|
2018-05-28 15:50:39 +00:00
|
|
|
const ProcessLauncher = require('../../process/processLauncher');
|
2018-05-23 19:10:53 +00:00
|
|
|
const constants = require('../../constants');
|
|
|
|
|
|
|
|
class StorageProcessesLauncher {
|
|
|
|
constructor(options) {
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.events = options.events;
|
2018-05-28 15:50:01 +00:00
|
|
|
this.storageConfig = options.storageConfig;
|
2018-05-24 16:47:10 +00:00
|
|
|
this.webServerConfig = options.webServerConfig;
|
2018-05-31 10:18:25 +00:00
|
|
|
this.blockchainConfig = options.blockchainConfig;
|
2018-05-23 19:10:53 +00:00
|
|
|
this.processes = {};
|
2018-05-30 14:52:15 +00:00
|
|
|
|
2018-05-31 10:18:25 +00:00
|
|
|
this.cors = this.buildCors();
|
|
|
|
|
2018-05-30 14:52:15 +00:00
|
|
|
this.events.on('exit', () => {
|
|
|
|
Object.keys(this.processes).forEach(processName => {
|
|
|
|
this.processes[processName].send('exit');
|
|
|
|
});
|
|
|
|
});
|
2018-05-23 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 10:18:25 +00:00
|
|
|
buildCors()
|
|
|
|
{
|
|
|
|
let corsParts = [];
|
|
|
|
// add our webserver CORS
|
|
|
|
if(this.webServerConfig.enabled){
|
|
|
|
if (this.webServerConfig && this.webServerConfig.host) {
|
|
|
|
let port = this.webServerConfig.port ? `:${this.webServerConfig.port}` : '';
|
|
|
|
corsParts.push(`${this.webServerConfig.protocol || 'http'}://${this.webServerConfig.host}${port}`);
|
|
|
|
}
|
|
|
|
else corsParts.push('http://localhost:8000');
|
|
|
|
}
|
|
|
|
|
|
|
|
// add all dapp connection storage
|
|
|
|
if(this.storageConfig.enabled) {
|
|
|
|
this.storageConfig.dappConnection.forEach(dappConn => {
|
|
|
|
if(dappConn.getUrl || dappConn.host){
|
|
|
|
|
|
|
|
// if getUrl is specified in the config, that needs to be included in cors
|
|
|
|
// instead of the concatenated protocol://host:port
|
|
|
|
if(dappConn.getUrl) {
|
|
|
|
// remove /ipfs or /bzz: from getUrl if it's there
|
|
|
|
let getUrlParts = dappConn.getUrl.split('/');
|
|
|
|
getUrlParts = getUrlParts.slice(0, 3);
|
|
|
|
corsParts.push(getUrlParts.join('/'));
|
|
|
|
}
|
|
|
|
// use our modified getUrl or in case it wasn't specified, use a built url
|
|
|
|
else{
|
|
|
|
let port = dappConn.port ? `:${dappConn.port}` : '';
|
|
|
|
corsParts.push(`${dappConn.protocol || 'http'}://${dappConn.host}${port}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.blockchainConfig.enabled) {
|
|
|
|
// add our rpc endpoints to CORS
|
|
|
|
if(this.blockchainConfig.rpcHost && this.blockchainConfig.rpcPort){
|
|
|
|
corsParts.push(`http://${this.blockchainConfig.rpcHost}:${this.blockchainConfig.rpcPort}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add our ws endpoints to CORS
|
|
|
|
if(this.blockchainConfig.wsRPC && this.blockchainConfig.wsHost && this.blockchainConfig.wsPort){
|
|
|
|
corsParts.push(`ws://${this.blockchainConfig.wsHost}:${this.blockchainConfig.wsPort}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return corsParts;
|
|
|
|
}
|
|
|
|
|
2018-05-23 19:10:53 +00:00
|
|
|
processExited(storageName, code) {
|
|
|
|
this.logger.error(__(`Storage process for ${storageName} ended before the end of this process. Code: ${code}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
launchProcess(storageName, callback) {
|
|
|
|
const self = this;
|
|
|
|
if (self.processes[storageName]) {
|
|
|
|
return callback(__('Storage process already started'));
|
|
|
|
}
|
|
|
|
const filePath = utils.joinPath(__dirname, `./${storageName}.js`);
|
|
|
|
fs.access(filePath, (err) => {
|
|
|
|
if (err) {
|
2018-05-24 14:51:05 +00:00
|
|
|
return callback(__('No process file for this storage type exists. Please start the process locally.'));
|
2018-05-23 19:10:53 +00:00
|
|
|
}
|
|
|
|
self.logger.info(__(`Starting ${storageName} process`).cyan);
|
|
|
|
self.processes[storageName] = new ProcessLauncher({
|
|
|
|
modulePath: filePath,
|
|
|
|
logger: self.logger,
|
|
|
|
events: self.events,
|
2018-05-31 10:18:25 +00:00
|
|
|
silent: self.logger.logLevel !== 'trace',
|
2018-05-23 19:10:53 +00:00
|
|
|
exitCallback: self.processExited.bind(this, storageName)
|
|
|
|
});
|
2018-05-24 16:47:10 +00:00
|
|
|
self.processes[storageName].send({
|
|
|
|
action: constants.blockchain.init, options: {
|
|
|
|
storageConfig: self.storageConfig,
|
2018-05-31 10:18:25 +00:00
|
|
|
cors: self.cors
|
2018-05-24 16:47:10 +00:00
|
|
|
}
|
|
|
|
});
|
2018-05-23 19:10:53 +00:00
|
|
|
|
2018-05-28 15:50:01 +00:00
|
|
|
self.processes[storageName].on('result', constants.storage.initiated, (msg) => {
|
|
|
|
if (msg.error) {
|
|
|
|
self.processes[storageName].disconnect();
|
|
|
|
delete self.processes[storageName];
|
|
|
|
return callback(msg.error);
|
|
|
|
}
|
2018-05-24 14:51:05 +00:00
|
|
|
self.logger.info(__(`${storageName} process started`).cyan);
|
2018-05-28 15:50:01 +00:00
|
|
|
callback();
|
|
|
|
});
|
2018-05-23 19:10:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = StorageProcessesLauncher;
|