@@ -3,13 +3,15 @@ package main
3
3
import (
4
4
_ "expvar"
5
5
"fmt"
6
- _ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/codahale/metrics/runtime"
7
6
"net/http"
8
7
_ "net/http/pprof"
9
8
"os"
10
9
"strings"
11
10
11
+ _ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/codahale/metrics/runtime"
12
12
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
13
+ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
14
+
13
15
cmds "github.com/ipfs/go-ipfs/commands"
14
16
"github.com/ipfs/go-ipfs/core"
15
17
commands "github.com/ipfs/go-ipfs/core/commands"
@@ -192,24 +194,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
192
194
return node , nil
193
195
}
194
196
195
- // verify api address is valid multiaddr
196
- apiMaddr , err := ma .NewMultiaddr (cfg .Addresses .API )
197
- if err != nil {
198
- res .SetError (err , cmds .ErrNormal )
199
- return
200
- }
201
-
202
- var gatewayMaddr ma.Multiaddr
203
- if len (cfg .Addresses .Gateway ) > 0 {
204
- // ignore error for gateway address
205
- // if there is an error (invalid address), then don't run the gateway
206
- gatewayMaddr , _ = ma .NewMultiaddr (cfg .Addresses .Gateway )
207
- if gatewayMaddr == nil {
208
- log .Errorf ("Invalid gateway address: %s" , cfg .Addresses .Gateway )
209
- }
210
- }
211
-
212
- // mount if the user provided the --mount flag
197
+ // mount fuse if the user provided the --mount flag
213
198
mount , _ , err := req .Option (mountKwd ).Bool ()
214
199
if err != nil {
215
200
res .SetError (err , cmds .ErrNormal )
@@ -243,6 +228,17 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
243
228
fmt .Printf ("IPNS mounted at: %s\n " , nsdir )
244
229
}
245
230
231
+ // construct http gateway
232
+ var gatewayMaddr ma.Multiaddr
233
+ if len (cfg .Addresses .Gateway ) > 0 {
234
+ // ignore error for gateway address
235
+ // if there is an error (invalid address), then don't run the gateway
236
+ gatewayMaddr , _ = ma .NewMultiaddr (cfg .Addresses .Gateway )
237
+ if gatewayMaddr == nil {
238
+ log .Errorf ("Invalid gateway address: %s" , cfg .Addresses .Gateway )
239
+ }
240
+ }
241
+
246
242
var rootRedirect corehttp.ServeOption
247
243
if len (cfg .Gateway .RootRedirect ) > 0 {
248
244
rootRedirect = corehttp .RedirectOption ("" , cfg .Gateway .RootRedirect )
@@ -258,28 +254,59 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
258
254
}
259
255
260
256
if gatewayMaddr != nil {
257
+
258
+ gwLis , err := manet .Listen (gatewayMaddr )
259
+ if err != nil {
260
+ res .SetError (err , cmds .ErrNormal )
261
+ return
262
+ }
263
+ // we might have listened to /tcp/0 - lets see what we are listing on
264
+ gatewayMaddr = gwLis .Multiaddr ()
265
+
266
+ if writable {
267
+ fmt .Printf ("Gateway (writable) server listening on %s\n " , gatewayMaddr )
268
+ } else {
269
+ fmt .Printf ("Gateway (readonly) server listening on %s\n " , gatewayMaddr )
270
+ }
271
+ var opts = []corehttp.ServeOption {
272
+ corehttp .VersionOption (),
273
+ corehttp .IPNSHostnameOption (),
274
+ corehttp .GatewayOption (writable ),
275
+ }
276
+ if rootRedirect != nil {
277
+ opts = append (opts , rootRedirect )
278
+ }
261
279
go func () {
262
- var opts = []corehttp.ServeOption {
263
- corehttp .VersionOption (),
264
- corehttp .IPNSHostnameOption (),
265
- corehttp .GatewayOption (writable ),
266
- }
267
- if rootRedirect != nil {
268
- opts = append (opts , rootRedirect )
269
- }
270
- if writable {
271
- fmt .Printf ("Gateway (writable) server listening on %s\n " , gatewayMaddr )
272
- } else {
273
- fmt .Printf ("Gateway (readonly) server listening on %s\n " , gatewayMaddr )
274
- }
275
- err := corehttp .ListenAndServe (node , gatewayMaddr .String (), opts ... )
280
+ err := corehttp .Serve (node , gwLis .NetListener (), opts ... )
276
281
if err != nil {
277
282
log .Error (err )
278
283
}
279
284
}()
280
285
}
281
286
282
- gateway := corehttp .NewGateway (corehttp.GatewayConfig {
287
+ // construct api endpoint
288
+ apiMaddr , err := ma .NewMultiaddr (cfg .Addresses .API )
289
+ if err != nil {
290
+ res .SetError (err , cmds .ErrNormal )
291
+ return
292
+ }
293
+
294
+ apiLis , err := manet .Listen (apiMaddr )
295
+ if err != nil {
296
+ res .SetError (err , cmds .ErrNormal )
297
+ return
298
+ }
299
+ // we might have listened to /tcp/0 - lets see what we are listing on
300
+ apiMaddr = apiLis .Multiaddr ()
301
+ fmt .Printf ("API server listening on %s\n " , apiMaddr )
302
+
303
+ // TODO: dont persist in config - write to lock file..
304
+ if err := node .Repo .SetConfigKey ("Addresses.API" , apiMaddr .String ()); err != nil {
305
+ res .SetError (err , cmds .ErrNormal )
306
+ return
307
+ }
308
+
309
+ apiGw := corehttp .NewGateway (corehttp.GatewayConfig {
283
310
Writable : true ,
284
311
BlockList : & corehttp.BlockList {
285
312
Decider : func (s string ) bool {
@@ -300,7 +327,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
300
327
var opts = []corehttp.ServeOption {
301
328
corehttp .CommandsOption (* req .Context ()),
302
329
corehttp .WebUIOption ,
303
- gateway .ServeOption (),
330
+ apiGw .ServeOption (),
304
331
corehttp .VersionOption (),
305
332
defaultMux ("/debug/vars" ),
306
333
defaultMux ("/debug/pprof/" ),
@@ -310,8 +337,9 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
310
337
opts = append (opts , rootRedirect )
311
338
}
312
339
313
- if err := corehttp .ListenAndServe (node , apiMaddr . String (), opts ... ); err != nil {
340
+ if err := corehttp .Serve (node , apiLis . NetListener (), opts ... ); err != nil {
314
341
res .SetError (err , cmds .ErrNormal )
315
342
return
316
343
}
344
+
317
345
}
0 commit comments