From b5c762848fb472afba99b3570cdcab3d363cb18b Mon Sep 17 00:00:00 2001 From: Grant Herman Date: Wed, 27 Feb 2019 17:34:22 -0800 Subject: [PATCH] feat(issue-1852): now supports multiple api and gateways License: MIT Signed-off-by: Grant Herman grantlouisherman041@gmail.com --- src/http/index.js | 61 ++++++++++++++++++++++--------- test/gateway/index.js | 2 +- test/http-api/inject/bitswap.js | 2 +- test/http-api/inject/block.js | 2 +- test/http-api/inject/bootstrap.js | 2 +- test/http-api/inject/config.js | 2 +- test/http-api/inject/dht.js | 2 +- test/http-api/inject/dns.js | 2 +- test/http-api/inject/files.js | 2 +- test/http-api/inject/id.js | 2 +- test/http-api/inject/name.js | 2 +- test/http-api/inject/object.js | 2 +- test/http-api/inject/pin.js | 2 +- test/http-api/inject/ping.js | 2 +- test/http-api/inject/pubsub.js | 2 +- test/http-api/inject/resolve.js | 2 +- test/http-api/inject/version.js | 2 +- 17 files changed, 60 insertions(+), 33 deletions(-) diff --git a/src/http/index.js b/src/http/index.js index c0de7a642e..4a2fb531c9 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -29,6 +29,24 @@ function hapiInfoToMultiaddr (info) { return toMultiaddr(uri) } +async function serverCreator (serverAddrsArr, createServerFunc, hapiInfoToMultiaddr, ipfs) { + if (!serverAddrsArr.length) { + debug(Error('There are no addresses')) + } + // just in case the address is just string + let serversAddrs = [].concat(serverAddrsArr) + const processServer = async (serverInstance, createServerFunc, hapiInfoToMultiaddr, ipfs) => { + let addr = serverInstance.split('/') + let _Server = await createServerFunc(addr[2], addr[4], ipfs) + await _Server.start() + _Server.info.ma = hapiInfoToMultiaddr(_Server.info) + return _Server + } + return Promise.all( + serversAddrs.map(server => processServer(server, createServerFunc, hapiInfoToMultiaddr, ipfs)) + ).catch(err => debug(err)) +} + class HttpApi { constructor (options) { this._options = options || {} @@ -89,24 +107,28 @@ class HttpApi { const config = await ipfs.config.get() - const apiAddr = config.Addresses.API.split('/') - const apiServer = await this._createApiServer(apiAddr[2], apiAddr[4], ipfs) - await apiServer.start() - apiServer.info.ma = hapiInfoToMultiaddr(apiServer.info) - this._apiServer = apiServer + const apiAddrs = config.Addresses.API + this._apiServer = await Promise.resolve( + serverCreator.apply(this, [apiAddrs, this._createApiServer, hapiInfoToMultiaddr, ipfs]) + ) // for the CLI to know the where abouts of the API - await promisify(ipfs._repo.apiAddr.set)(apiServer.info.ma) + await promisify(ipfs._repo.apiAddr.set)(this._apiServer[0].info.ma) - const gatewayAddr = config.Addresses.Gateway.split('/') - const gatewayServer = await this._createGatewayServer(gatewayAddr[2], gatewayAddr[4], ipfs) - await gatewayServer.start() - gatewayServer.info.ma = hapiInfoToMultiaddr(gatewayServer.info) - this._gatewayServer = gatewayServer + const gatewayAddr = config.Addresses.Gateway - ipfs._print('API listening on %s', apiServer.info.ma) - ipfs._print('Gateway (read only) listening on %s', gatewayServer.info.ma) - ipfs._print('Web UI available at %s', toUri(apiServer.info.ma) + '/webui') + this._gatewayServer = await Promise.resolve( + serverCreator.apply(this, [gatewayAddr, this._createGatewayServer, hapiInfoToMultiaddr, ipfs]) + ) + this._apiServer.forEach(apiServer => { + ipfs._print('API listening on %s', apiServer.info.ma) + }) + this._gatewayServer.forEach(gatewayServer => { + ipfs._print('Gateway (read only) listening on %s', gatewayServer.info.ma) + }) + this._apiServer.forEach(apiServer => { + ipfs._print('Web UI available at %s', toUri(apiServer.info.ma) + '/webui') + }) this._log('started') return this } @@ -177,14 +199,19 @@ class HttpApi { get apiAddr () { if (!this._apiServer) throw new Error('API address unavailable - server is not started') - return multiaddr('/ip4/127.0.0.1/tcp/' + this._apiServer.info.port) + return multiaddr('/ip4/127.0.0.1/tcp/' + this._apiServer[0].info.port) } async stop () { + function stopServer (serverArr) { + for (let i = 0; i < serverArr.length; i++) { + serverArr[i].stop() + } + } this._log('stopping') await Promise.all([ - this._apiServer && this._apiServer.stop(), - this._gatewayServer && this._gatewayServer.stop(), + this._apiServer && stopServer(this._apiServer), + this._gatewayServer && stopServer(this._gatewayServer), this._ipfs && this._ipfs.stop() ]) this._log('stopped') diff --git a/test/gateway/index.js b/test/gateway/index.js index 0f9ac1812b..847e8b5846 100644 --- a/test/gateway/index.js +++ b/test/gateway/index.js @@ -60,7 +60,7 @@ describe('HTTP Gateway', function () { await http.api.start() - gateway = http.api._gatewayServer + gateway = http.api._gatewayServer[0] // QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi await http.api._ipfs.add([ diff --git a/test/http-api/inject/bitswap.js b/test/http-api/inject/bitswap.js index c8a1d0568c..7e2fa0a16f 100644 --- a/test/http-api/inject/bitswap.js +++ b/test/http-api/inject/bitswap.js @@ -12,7 +12,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) before(async function () { diff --git a/test/http-api/inject/block.js b/test/http-api/inject/block.js index 1cf8eb52e4..8d4ddc0b49 100644 --- a/test/http-api/inject/block.js +++ b/test/http-api/inject/block.js @@ -13,7 +13,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) describe('/block/put', () => { diff --git a/test/http-api/inject/bootstrap.js b/test/http-api/inject/bootstrap.js index 7708390cfe..22e19160e7 100644 --- a/test/http-api/inject/bootstrap.js +++ b/test/http-api/inject/bootstrap.js @@ -11,7 +11,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] return api.inject({ method: 'GET', url: '/api/v0/bootstrap/add/default' diff --git a/test/http-api/inject/config.js b/test/http-api/inject/config.js index 3e647505ff..aa75e1f861 100644 --- a/test/http-api/inject/config.js +++ b/test/http-api/inject/config.js @@ -17,7 +17,7 @@ module.exports = (http) => { before(() => { updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8')) - api = http.api._apiServer + api = http.api._apiServer[0] }) after(() => { diff --git a/test/http-api/inject/dht.js b/test/http-api/inject/dht.js index f614ee50dc..949645836b 100644 --- a/test/http-api/inject/dht.js +++ b/test/http-api/inject/dht.js @@ -12,7 +12,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) describe('/findpeer', () => { diff --git a/test/http-api/inject/dns.js b/test/http-api/inject/dns.js index 163f2dfd4f..51ac1e3218 100644 --- a/test/http-api/inject/dns.js +++ b/test/http-api/inject/dns.js @@ -8,7 +8,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) it('resolve ipfs.io dns', async () => { diff --git a/test/http-api/inject/files.js b/test/http-api/inject/files.js index 06d2e02597..b6a03ff5a2 100644 --- a/test/http-api/inject/files.js +++ b/test/http-api/inject/files.js @@ -13,7 +13,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) describe('/add', () => { diff --git a/test/http-api/inject/id.js b/test/http-api/inject/id.js index eb646be893..79aeccbc5c 100644 --- a/test/http-api/inject/id.js +++ b/test/http-api/inject/id.js @@ -8,7 +8,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) it('get the id', async () => { diff --git a/test/http-api/inject/name.js b/test/http-api/inject/name.js index 95a51eb5c2..bfd693e3cb 100644 --- a/test/http-api/inject/name.js +++ b/test/http-api/inject/name.js @@ -16,7 +16,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) it('should publish a record', async function () { diff --git a/test/http-api/inject/object.js b/test/http-api/inject/object.js index e329e98c64..2505718852 100644 --- a/test/http-api/inject/object.js +++ b/test/http-api/inject/object.js @@ -17,7 +17,7 @@ module.exports = (http) => { let api before('api', () => { - api = http.api._apiServer + api = http.api._apiServer[0] }) describe('/new', () => { diff --git a/test/http-api/inject/pin.js b/test/http-api/inject/pin.js index d70547a876..c950a68d24 100644 --- a/test/http-api/inject/pin.js +++ b/test/http-api/inject/pin.js @@ -37,7 +37,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) describe('rm', () => { diff --git a/test/http-api/inject/ping.js b/test/http-api/inject/ping.js index 0ed460bba3..3982b9f09b 100644 --- a/test/http-api/inject/ping.js +++ b/test/http-api/inject/ping.js @@ -13,7 +13,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) it('returns 400 if both n and count are provided', async () => { diff --git a/test/http-api/inject/pubsub.js b/test/http-api/inject/pubsub.js index e91df8b3a3..5b613929f3 100644 --- a/test/http-api/inject/pubsub.js +++ b/test/http-api/inject/pubsub.js @@ -16,7 +16,7 @@ module.exports = (http) => { const topicNotSubscribed = 'somethingRandom' before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) describe('/sub', () => { diff --git a/test/http-api/inject/resolve.js b/test/http-api/inject/resolve.js index edb7eb94ab..39f3b28f39 100644 --- a/test/http-api/inject/resolve.js +++ b/test/http-api/inject/resolve.js @@ -12,7 +12,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) it('should resolve a path and return a base2 encoded CID', async () => { diff --git a/test/http-api/inject/version.js b/test/http-api/inject/version.js index 2ab6b18bf9..07645d942c 100644 --- a/test/http-api/inject/version.js +++ b/test/http-api/inject/version.js @@ -9,7 +9,7 @@ module.exports = (http) => { let api before(() => { - api = http.api._apiServer + api = http.api._apiServer[0] }) it('get the version', async () => {