@@ -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+
3250class 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' )
0 commit comments