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

Commit b5c7628

Browse files
feat(issue-1852): now supports multiple api and gateways
License: MIT Signed-off-by: Grant Herman grantlouisherman041@gmail.com
1 parent bebce7f commit b5c7628

File tree

17 files changed

+60
-33
lines changed

17 files changed

+60
-33
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._apiServer = 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._apiServer[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._apiServer.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._apiServer.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._apiServer[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/gateway/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('HTTP Gateway', function () {
6060

6161
await http.api.start()
6262

63-
gateway = http.api._gatewayServer
63+
gateway = http.api._gatewayServer[0]
6464

6565
// QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi
6666
await http.api._ipfs.add([

test/http-api/inject/bitswap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = (http) => {
1212
let api
1313

1414
before(() => {
15-
api = http.api._apiServer
15+
api = http.api._apiServer[0]
1616
})
1717

1818
before(async function () {

test/http-api/inject/block.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (http) => {
1313
let api
1414

1515
before(() => {
16-
api = http.api._apiServer
16+
api = http.api._apiServer[0]
1717
})
1818

1919
describe('/block/put', () => {

test/http-api/inject/bootstrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = (http) => {
1111
let api
1212

1313
before(() => {
14-
api = http.api._apiServer
14+
api = http.api._apiServer[0]
1515
return api.inject({
1616
method: 'GET',
1717
url: '/api/v0/bootstrap/add/default'

test/http-api/inject/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = (http) => {
1717

1818
before(() => {
1919
updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8'))
20-
api = http.api._apiServer
20+
api = http.api._apiServer[0]
2121
})
2222

2323
after(() => {

test/http-api/inject/dht.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = (http) => {
1212
let api
1313

1414
before(() => {
15-
api = http.api._apiServer
15+
api = http.api._apiServer[0]
1616
})
1717

1818
describe('/findpeer', () => {

test/http-api/inject/dns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = (http) => {
88
let api
99

1010
before(() => {
11-
api = http.api._apiServer
11+
api = http.api._apiServer[0]
1212
})
1313

1414
it('resolve ipfs.io dns', async () => {

test/http-api/inject/files.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (http) => {
1313
let api
1414

1515
before(() => {
16-
api = http.api._apiServer
16+
api = http.api._apiServer[0]
1717
})
1818

1919
describe('/add', () => {

test/http-api/inject/id.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = (http) => {
88
let api
99

1010
before(() => {
11-
api = http.api._apiServer
11+
api = http.api._apiServer[0]
1212
})
1313

1414
it('get the id', async () => {

test/http-api/inject/name.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = (http) => {
1616
let api
1717

1818
before(() => {
19-
api = http.api._apiServer
19+
api = http.api._apiServer[0]
2020
})
2121

2222
it('should publish a record', async function () {

test/http-api/inject/object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = (http) => {
1717
let api
1818

1919
before('api', () => {
20-
api = http.api._apiServer
20+
api = http.api._apiServer[0]
2121
})
2222

2323
describe('/new', () => {

test/http-api/inject/pin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = (http) => {
3737
let api
3838

3939
before(() => {
40-
api = http.api._apiServer
40+
api = http.api._apiServer[0]
4141
})
4242

4343
describe('rm', () => {

test/http-api/inject/ping.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (http) => {
1313
let api
1414

1515
before(() => {
16-
api = http.api._apiServer
16+
api = http.api._apiServer[0]
1717
})
1818

1919
it('returns 400 if both n and count are provided', async () => {

test/http-api/inject/pubsub.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = (http) => {
1616
const topicNotSubscribed = 'somethingRandom'
1717

1818
before(() => {
19-
api = http.api._apiServer
19+
api = http.api._apiServer[0]
2020
})
2121

2222
describe('/sub', () => {

test/http-api/inject/resolve.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = (http) => {
1212
let api
1313

1414
before(() => {
15-
api = http.api._apiServer
15+
api = http.api._apiServer[0]
1616
})
1717

1818
it('should resolve a path and return a base2 encoded CID', async () => {

test/http-api/inject/version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = (http) => {
99
let api
1010

1111
before(() => {
12-
api = http.api._apiServer
12+
api = http.api._apiServer[0]
1313
})
1414

1515
it('get the version', async () => {

0 commit comments

Comments
 (0)