Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 0fe22f7

Browse files
feat(issue-1852): now supports multiple api and gateways
License: MIT Signed-off-by: Grant Herman [email protected]
1 parent bebce7f commit 0fe22f7

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

src/http/index.js

+44-17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ function hapiInfoToMultiaddr (info) {
2929
return toMultiaddr(uri)
3030
}
3131

32+
async function serverCreator (serverAddrsArr, createServerFunc, hapiInfoToMultiaddr, ipfs) {
33+
if (!serverAddrsArr.length) {
34+
debug(Error('There are no addresses'))
35+
}
36+
// just in case the address is just string
37+
let serversAddrs = [].concat(serverAddrsArr)
38+
const processServer = async (serverInstance, createServerFunc, hapiInfoToMultiaddr, ipfs) => {
39+
let addr = serverInstance.split('/')
40+
let _Server = await createServerFunc(addr[2], addr[4], ipfs)
41+
await _Server.start()
42+
_Server.info.ma = hapiInfoToMultiaddr(_Server.info)
43+
return _Server
44+
}
45+
return Promise.all(
46+
serversAddrs.map(server => processServer(server, createServerFunc, hapiInfoToMultiaddr, ipfs))
47+
).catch(err => debug(err))
48+
}
49+
3250
class HttpApi {
3351
constructor (options) {
3452
this._options = options || {}
@@ -89,24 +107,28 @@ class HttpApi {
89107

90108
const config = await ipfs.config.get()
91109

92-
const apiAddr = config.Addresses.API.split('/')
93-
const apiServer = await this._createApiServer(apiAddr[2], apiAddr[4], ipfs)
94-
await apiServer.start()
95-
apiServer.info.ma = hapiInfoToMultiaddr(apiServer.info)
96-
this._apiServer = apiServer
110+
const apiAddrs = config.Addresses.API
97111

112+
this._apiServers = await Promise.resolve(
113+
serverCreator.apply(this, [apiAddrs, this._createApiServer, hapiInfoToMultiaddr, ipfs])
114+
)
98115
// for the CLI to know the where abouts of the API
99-
await promisify(ipfs._repo.apiAddr.set)(apiServer.info.ma)
116+
await promisify(ipfs._repo.apiAddr.set)(this._apiServers[0].info.ma)
100117

101-
const gatewayAddr = config.Addresses.Gateway.split('/')
102-
const gatewayServer = await this._createGatewayServer(gatewayAddr[2], gatewayAddr[4], ipfs)
103-
await gatewayServer.start()
104-
gatewayServer.info.ma = hapiInfoToMultiaddr(gatewayServer.info)
105-
this._gatewayServer = gatewayServer
118+
const gatewayAddr = config.Addresses.Gateway
106119

107-
ipfs._print('API listening on %s', apiServer.info.ma)
108-
ipfs._print('Gateway (read only) listening on %s', gatewayServer.info.ma)
109-
ipfs._print('Web UI available at %s', toUri(apiServer.info.ma) + '/webui')
120+
this._gatewayServer = await Promise.resolve(
121+
serverCreator.apply(this, [gatewayAddr, this._createGatewayServer, hapiInfoToMultiaddr, ipfs])
122+
)
123+
this._apiServers.forEach(apiServer => {
124+
ipfs._print('API listening on %s', apiServer.info.ma)
125+
})
126+
this._gatewayServer.forEach(gatewayServer => {
127+
ipfs._print('Gateway (read only) listening on %s', gatewayServer.info.ma)
128+
})
129+
this._apiServers.forEach(apiServer => {
130+
ipfs._print('Web UI available at %s', toUri(apiServer.info.ma) + '/webui')
131+
})
110132
this._log('started')
111133
return this
112134
}
@@ -177,14 +199,19 @@ class HttpApi {
177199

178200
get apiAddr () {
179201
if (!this._apiServer) throw new Error('API address unavailable - server is not started')
180-
return multiaddr('/ip4/127.0.0.1/tcp/' + this._apiServer.info.port)
202+
return multiaddr('/ip4/127.0.0.1/tcp/' + this._apiServers[0].info.port)
181203
}
182204

183205
async stop () {
206+
function stopServer (serverArr) {
207+
for (let i = 0; i < serverArr.length; i++) {
208+
serverArr[i].stop()
209+
}
210+
}
184211
this._log('stopping')
185212
await Promise.all([
186-
this._apiServer && this._apiServer.stop(),
187-
this._gatewayServer && this._gatewayServer.stop(),
213+
this._apiServer && stopServer(this._apiServer),
214+
this._gatewayServer && stopServer(this._gatewayServer),
188215
this._ipfs && this._ipfs.stop()
189216
])
190217
this._log('stopped')

test/http-api/inject/dht.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ chai.use(dirtyChai)
1010
module.exports = (http) => {
1111
describe('/dht', () => {
1212
let api
13-
13+
console.log('HTTT', http)
1414
before(() => {
1515
api = http.api._apiServer
1616
})
@@ -21,7 +21,6 @@ module.exports = (http) => {
2121
method: 'GET',
2222
url: `/api/v0/dht/findpeer`
2323
})
24-
2524
expect(res.statusCode).to.equal(400)
2625
expect(res.result.Code).to.be.eql(1)
2726
})

0 commit comments

Comments
 (0)