-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(issue-1852): now supports multiple api and gateways #1903
Changes from 1 commit
b5c7628
1b17688
e57d0e9
1c38202
d5215c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -29,6 +29,24 @@ function hapiInfoToMultiaddr (info) { | |||||
return toMultiaddr(uri) | ||||||
} | ||||||
|
||||||
async function serverCreator (serverAddrsArr, createServerFunc, hapiInfoToMultiaddr, ipfs) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please can we rename
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please can we rename |
||||||
if (!serverAddrsArr.length) { | ||||||
debug(Error('There are no addresses')) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please just |
||||||
} | ||||||
// just in case the address is just string | ||||||
let serversAddrs = [].concat(serverAddrsArr) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be removed if we add the check as suggested above. |
||||||
const processServer = async (serverInstance, createServerFunc, hapiInfoToMultiaddr, ipfs) => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename |
||||||
let addr = serverInstance.split('/') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be |
||||||
let _Server = await createServerFunc(addr[2], addr[4], ipfs) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be |
||||||
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)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not catch this error - it is fatal. This can become: return Promise.all(serverAddrs.map(processServer)) |
||||||
} | ||||||
|
||||||
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( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename this to |
||||||
serverCreator.apply(this, [apiAddrs, this._createApiServer, hapiInfoToMultiaddr, ipfs]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this._apiServers = await serverCreator(apiAddrs, this._createApiServer, 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename |
||||||
|
||||||
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]) | ||||||
) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, please rename to this._gatewayServer = await serverCreator(gatewayAddrs, this._createGatewayServer, 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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename |
||||||
for (let i = 0; i < serverArr.length; i++) { | ||||||
serverArr[i].stop() | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to return a promise from this function: function stopServers (servers) {
return Promise.all(servers.map(server => server.stop()))
} |
||||||
} | ||||||
this._log('stopping') | ||||||
await Promise.all([ | ||||||
this._apiServer && this._apiServer.stop(), | ||||||
this._gatewayServer && this._gatewayServer.stop(), | ||||||
this._apiServer && stopServer(this._apiServer), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
this._gatewayServer && stopServer(this._gatewayServer), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
this._ipfs && this._ipfs.stop() | ||||||
]) | ||||||
this._log('stopped') | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to pass
hapiInfoToMultiaddr
, it is in scope