diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index 24eb1d485..8cfd28102 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -3,6 +3,7 @@ const utils = require('../../utils/utils.js'); const fs = require('../../core/fs.js'); const IpfsApi = require('ipfs-api'); const _ = require('underscore'); +const StorageProcessesLauncher = require('../../processes/storageProcesses/storageProcessesLauncher'); class IPFS { @@ -15,6 +16,15 @@ class IPFS { this.port = options.port || this.storageConfig.upload.port; this.protocol = options.protocol || this.storageConfig.upload.protocol; this.embark = embark; + + this.webServerConfig = embark.config.webServerConfig, + this.blockchainConfig = embark.config.blockchainConfig + + this.commandlineDeploy(); + this.setServiceCheck(); + this.addProviderToEmbarkJS(); + this.addObjectToConsole(); + this.startProcess(() => {}); } commandlineDeploy() { @@ -102,6 +112,24 @@ class IPFS { this.events.emit("runcode:register", "ipfs", ipfs); } + startProcess(callback) { + let self = this; + const storageProcessesLauncher = new StorageProcessesLauncher({ + logger: self.logger, + events: self.events, + storageConfig: self.storageConfig, + webServerConfig: self.webServerConfig, + blockchainConfig: self.blockchainConfig + }); + self.logger.trace(`Storage module: Launching ipfs process...`); + return storageProcessesLauncher.launchProcess('ipfs', (err) => { + if (err) { + return callback(err); + } + callback(); + }); + } + } module.exports = IPFS; diff --git a/lib/modules/storage/index.js b/lib/modules/storage/index.js index d0c110ce7..48eaf9a2c 100644 --- a/lib/modules/storage/index.js +++ b/lib/modules/storage/index.js @@ -6,6 +6,9 @@ const async = require('async'); const StorageProcessesLauncher = require('../../processes/storageProcesses/storageProcessesLauncher'); const constants = require('../../constants'); +const IpfsModule = require('../ipfs'); +const SwarmModule = require('../swarm'); + class Storage { constructor(embark, options){ const self = this; @@ -18,22 +21,25 @@ class Storage { this._events = options.events; this._logger = options.logger; - if(!this._storageConfig.enabled) return; + if (!this._storageConfig.enabled) return; - // filter list of dapp connections based on available_providers set in config - let hasSwarm = _.contains(this._storageConfig.available_providers, 'swarm'); // don't need to eval this in every loop iteration - // contains valid dapp storage providers - this._validDappProviders = _.filter(this._storageConfig.dappConnection, (conn) => { - return _.contains(this._storageConfig.available_providers, conn.provider) || (conn === '$BZZ' && hasSwarm); - }); + // // filter list of dapp connections based on available_providers set in config + // let hasSwarm = _.contains(this._storageConfig.available_providers, 'swarm'); // don't need to eval this in every loop iteration + // // contains valid dapp storage providers + // this._validDappProviders = _.filter(this._storageConfig.dappConnection, (conn) => { + // return _.contains(this._storageConfig.available_providers, conn.provider) || (conn === '$BZZ' && hasSwarm); + // }); - this.initStorageForEmbark(); - this.initStorageForDapp(); + // this.initStorageForEmbark(); + // this.initStorageForDapp(); - // don't start storage processes on build command, only on upload or run - if(_.contains(options.context, constants.contexts.upload) || _.contains(options.context, constants.contexts.run)){ - this.startStorageProcesses(); - } + // // don't start storage processes on build command, only on upload or run + // if(_.contains(options.context, constants.contexts.upload) || _.contains(options.context, constants.contexts.run)){ + // this.startStorageProcesses(); + // } + + new IpfsModule(embark, options); /*eslint no-new: "off"*/ + new SwarmModule(embark, options); /*eslint no-new: "off"*/ embark.events.setCommandHandler('storage:upload', (cb) => { let platform = options.storageConfig.upload.provider; @@ -67,7 +73,7 @@ class Storage { callback(); }); } - + _startStorageNode(platform, callback) { let self = this; const storageProcessesLauncher = new StorageProcessesLauncher({ @@ -86,16 +92,6 @@ class Storage { }); } - /// Initializes a storage provider for Embark upload - initStorageForEmbark(){ - let storageProviderCls = require(`../${this._storageConfig.upload.provider}/index.js`); - let uploadProvider = new storageProviderCls(this._embark, this._options); /*eslint no-new: "off"*/ - - if(typeof uploadProvider.commandlineDeploy == 'function') uploadProvider.commandlineDeploy(); - if(typeof uploadProvider.setServiceCheck == 'function') uploadProvider.setServiceCheck(); - if(typeof uploadProvider.addObjectToConsole == 'function') uploadProvider.addObjectToConsole(); - } - /** * Initializes a storage provider for EmbarkJS * @@ -137,7 +133,6 @@ class Storage { * @returns {void} */ addSetProviders() { - let code = `\nEmbarkJS.Storage.setProviders(${JSON.stringify(this._validDappProviders)});`; let shouldInit = (storageConfig) => { return (this._validDappProviders !== undefined && this._validDappProviders.length > 0 && storageConfig.enabled === true); diff --git a/lib/modules/swarm/index.js b/lib/modules/swarm/index.js index fb62b9ad7..fd7c3c54f 100644 --- a/lib/modules/swarm/index.js +++ b/lib/modules/swarm/index.js @@ -3,6 +3,7 @@ const utils = require('../../utils/utils.js'); const fs = require('../../core/fs.js'); const Web3Bzz = require('web3-bzz'); const _ = require('underscore'); +const StorageProcessesLauncher = require('../../processes/storageProcesses/storageProcessesLauncher'); class Swarm { @@ -15,11 +16,19 @@ class Swarm { this.port = options.port || this.storageConfig.port; this.embark = embark; + this.webServerConfig = embark.config.webServerConfig, + this.blockchainConfig = embark.config.blockchainConfig + this.providerUrl = utils.buildUrl(options.protocol || options.storageConfig.upload.protocol, options.host || options.storageConfig.upload.host, options.port || options.storageConfig.upload.port); this.getUrl = options.storageConfig.upload.getUrl || this.providerUrl + '/bzz:/'; this.bzz = new Web3Bzz(this.providerUrl); + + this.commandlineDeploy(); + this.setServiceCheck(); + this.addProviderToEmbarkJS(); + this.startProcess(() => {}); } commandlineDeploy() { @@ -81,6 +90,25 @@ class Swarm { this.embark.addCodeToEmbarkJS(code); } + + startProcess(callback) { + let self = this; + const storageProcessesLauncher = new StorageProcessesLauncher({ + logger: self.logger, + events: self.events, + storageConfig: self.storageConfig, + webServerConfig: self.webServerConfig, + blockchainConfig: self.blockchainConfig + }); + self.logger.trace(`Storage module: Launching swarm process...`); + return storageProcessesLauncher.launchProcess('swarm', (err) => { + if (err) { + return callback(err); + } + callback(); + }); + } + } module.exports = Swarm;