@@ -29,6 +29,22 @@ function hapiInfoToMultiaddr (info) {
29
29
return toMultiaddr ( uri )
30
30
}
31
31
32
+ function serverCreator ( serverAddrs , createServer , ipfs ) {
33
+ serverAddrs = serverAddrs || [ ]
34
+ // just in case the address is just string
35
+ serverAddrs = Array . isArray ( serverAddrs ) ? serverAddrs : [ serverAddrs ]
36
+
37
+ const processServer = async address => {
38
+ const addrParts = address . split ( '/' )
39
+ const server = await createServer ( addrParts [ 2 ] , addrParts [ 4 ] , ipfs )
40
+ await server . start ( )
41
+ server . info . ma = hapiInfoToMultiaddr ( server . info )
42
+ return server
43
+ }
44
+
45
+ return Promise . all ( serverAddrs . map ( processServer ) )
46
+ }
47
+
32
48
class HttpApi {
33
49
constructor ( options ) {
34
50
this . _options = options || { }
@@ -88,25 +104,28 @@ class HttpApi {
88
104
this . _ipfs = ipfs
89
105
90
106
const config = await ipfs . config . get ( )
107
+ config . Addresses = config . Addresses || { }
91
108
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
109
+ const apiAddrs = config . Addresses . API
110
+ this . _apiServers = await serverCreator ( apiAddrs , this . _createApiServer , ipfs )
97
111
98
112
// for the CLI to know the where abouts of the API
99
- await promisify ( ipfs . _repo . apiAddr . set ) ( apiServer . info . ma )
113
+ if ( this . _apiServers . length ) {
114
+ await promisify ( ipfs . _repo . apiAddr . set ) ( this . _apiServers [ 0 ] . info . ma )
115
+ }
100
116
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
117
+ const gatewayAddrs = config . Addresses . Gateway
118
+ this . _gatewayServers = await serverCreator ( gatewayAddrs , this . _createGatewayServer , ipfs )
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 . _apiServers . forEach ( apiServer => {
121
+ ipfs . _print ( 'API listening on %s' , apiServer . info . ma )
122
+ } )
123
+ this . _gatewayServers . forEach ( gatewayServer => {
124
+ ipfs . _print ( 'Gateway (read only) listening on %s' , gatewayServer . info . ma )
125
+ } )
126
+ this . _apiServers . forEach ( apiServer => {
127
+ ipfs . _print ( 'Web UI available at %s' , toUri ( apiServer . info . ma ) + '/webui' )
128
+ } )
110
129
this . _log ( 'started' )
111
130
return this
112
131
}
@@ -176,15 +195,18 @@ class HttpApi {
176
195
}
177
196
178
197
get apiAddr ( ) {
179
- 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 )
198
+ if ( ! this . _apiServers || ! this . _apiServers . length ) {
199
+ throw new Error ( 'API address unavailable - server is not started' )
200
+ }
201
+ return multiaddr ( '/ip4/127.0.0.1/tcp/' + this . _apiServers [ 0 ] . info . port )
181
202
}
182
203
183
204
async stop ( ) {
184
205
this . _log ( 'stopping' )
206
+ const stopServers = servers => Promise . all ( ( servers || [ ] ) . map ( s => s . stop ( ) ) )
185
207
await Promise . all ( [
186
- this . _apiServer && this . _apiServer . stop ( ) ,
187
- this . _gatewayServer && this . _gatewayServer . stop ( ) ,
208
+ stopServers ( this . _apiServers ) ,
209
+ stopServers ( this . _gatewayServers ) ,
188
210
this . _ipfs && this . _ipfs . stop ( )
189
211
] )
190
212
this . _log ( 'stopped' )
0 commit comments