Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3748bf3

Browse files
committedMay 24, 2015
tcp0: move socket Listen calls into 'ipfs daemon' so that we can print afterwards
1 parent 336a6d6 commit 3748bf3

File tree

2 files changed

+77
-53
lines changed

2 files changed

+77
-53
lines changed
 

‎cmd/ipfs/daemon.go

+64-36
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package main
33
import (
44
_ "expvar"
55
"fmt"
6-
_ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/codahale/metrics/runtime"
76
"net/http"
87
_ "net/http/pprof"
98
"os"
109
"strings"
1110

11+
_ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/codahale/metrics/runtime"
1212
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+
1315
cmds "github.com/ipfs/go-ipfs/commands"
1416
"github.com/ipfs/go-ipfs/core"
1517
commands "github.com/ipfs/go-ipfs/core/commands"
@@ -192,24 +194,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
192194
return node, nil
193195
}
194196

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
213198
mount, _, err := req.Option(mountKwd).Bool()
214199
if err != nil {
215200
res.SetError(err, cmds.ErrNormal)
@@ -243,6 +228,17 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
243228
fmt.Printf("IPNS mounted at: %s\n", nsdir)
244229
}
245230

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+
246242
var rootRedirect corehttp.ServeOption
247243
if len(cfg.Gateway.RootRedirect) > 0 {
248244
rootRedirect = corehttp.RedirectOption("", cfg.Gateway.RootRedirect)
@@ -258,28 +254,59 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
258254
}
259255

260256
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+
}
261279
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...)
276281
if err != nil {
277282
log.Error(err)
278283
}
279284
}()
280285
}
281286

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{
283310
Writable: true,
284311
BlockList: &corehttp.BlockList{
285312
Decider: func(s string) bool {
@@ -300,7 +327,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
300327
var opts = []corehttp.ServeOption{
301328
corehttp.CommandsOption(*req.Context()),
302329
corehttp.WebUIOption,
303-
gateway.ServeOption(),
330+
apiGw.ServeOption(),
304331
corehttp.VersionOption(),
305332
defaultMux("/debug/vars"),
306333
defaultMux("/debug/pprof/"),
@@ -310,8 +337,9 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
310337
opts = append(opts, rootRedirect)
311338
}
312339

313-
if err := corehttp.ListenAndServe(node, apiMaddr.String(), opts...); err != nil {
340+
if err := corehttp.Serve(node, apiLis.NetListener(), opts...); err != nil {
314341
res.SetError(err, cmds.ErrNormal)
315342
return
316343
}
344+
317345
}

‎core/corehttp/corehttp.go

+13-17
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,29 @@ func ListenAndServe(n *core.IpfsNode, listeningMultiAddr string, options ...Serv
5050
if err != nil {
5151
return err
5252
}
53-
handler, err := makeHandler(n, options...)
54-
if err != nil {
55-
return err
56-
}
57-
return listenAndServe(n, addr, handler)
58-
}
5953

60-
func listenAndServe(node *core.IpfsNode, addr ma.Multiaddr, handler http.Handler) error {
61-
netarg, host, err := manet.DialArgs(addr)
54+
list, err := manet.Listen(addr)
6255
if err != nil {
6356
return err
6457
}
6558

66-
list, err := net.Listen(netarg, host)
59+
// we might have listened to /tcp/0 - lets see what we are listing on
60+
addr = list.Multiaddr()
61+
fmt.Printf("API server listening on %s\n", addr)
62+
63+
return Serve(n, list.NetListener(), options...)
64+
}
65+
66+
func Serve(node *core.IpfsNode, lis net.Listener, options ...ServeOption) error {
67+
handler, err := makeHandler(node, options...)
6768
if err != nil {
6869
return err
6970
}
7071

71-
listenMaAddr, err := manet.FromNetAddr(list.Addr())
72+
addr, err := manet.FromNetAddr(lis.Addr())
7273
if err != nil {
7374
return err
7475
}
75-
if err := node.Repo.SetConfigKey("Addresses.API", listenMaAddr); err != nil {
76-
return err
77-
}
78-
79-
fmt.Printf("API server listening on %s\n", listenMaAddr)
8076

8177
// if the server exits beforehand
8278
var serverError error
@@ -86,7 +82,7 @@ func listenAndServe(node *core.IpfsNode, addr ma.Multiaddr, handler http.Handler
8682
defer node.Children().Done()
8783

8884
go func() {
89-
serverError = http.Serve(list, handler)
85+
serverError = http.Serve(lis, handler)
9086
close(serverExited)
9187
}()
9288

@@ -98,7 +94,7 @@ func listenAndServe(node *core.IpfsNode, addr ma.Multiaddr, handler http.Handler
9894
case <-node.Closing():
9995
log.Infof("server at %s terminating...", addr)
10096

101-
list.Close()
97+
lis.Close()
10298

10399
outer:
104100
for {

0 commit comments

Comments
 (0)
Please sign in to comment.