@@ -29,6 +29,24 @@ function hapiInfoToMultiaddr (info) {
29
29
return toMultiaddr ( uri )
30
30
}
31
31
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
+
32
50
class HttpApi {
33
51
constructor ( options ) {
34
52
this . _options = options || { }
@@ -89,24 +107,28 @@ class HttpApi {
89
107
90
108
const config = await ipfs . config . get ( )
91
109
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
97
111
112
+ this . _apiServers = await Promise . resolve (
113
+ serverCreator . apply ( this , [ apiAddrs , this . _createApiServer , hapiInfoToMultiaddr , ipfs ] )
114
+ )
98
115
// 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 )
100
117
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
106
119
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
+ } )
110
132
this . _log ( 'started' )
111
133
return this
112
134
}
@@ -177,14 +199,19 @@ class HttpApi {
177
199
178
200
get apiAddr ( ) {
179
201
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 )
181
203
}
182
204
183
205
async stop ( ) {
206
+ function stopServer ( serverArr ) {
207
+ for ( let i = 0 ; i < serverArr . length ; i ++ ) {
208
+ serverArr [ i ] . stop ( )
209
+ }
210
+ }
184
211
this . _log ( 'stopping' )
185
212
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 ) ,
188
215
this . _ipfs && this . _ipfs . stop ( )
189
216
] )
190
217
this . _log ( 'stopped' )
0 commit comments