perform multiaddr conversion in the function
Brian Tiger Chow committed
Jan 23, 2015 at 21:28 UTC
e9d3c9828c40b8afc1ca8257176ab9b71e4202e5
3 files changed
+15
-9
cmd/ipfs/daemon.go
+2
-2
@@ -154,7 +154,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
154
155
if gatewayMaddr != nil {
156
go func() {
157
- err := corehttp.ListenAndServe(node, gatewayMaddr, corehttp.GatewayOption)
157
+ err := corehttp.ListenAndServe(node, gatewayMaddr.String(), corehttp.GatewayOption)
158
if err != nil {
159
log.Error(err)
160
}
@@ -166,7 +166,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
166
corehttp.WebUIOption,
167
corehttp.GatewayOption,
168
}
169
- err = corehttp.ListenAndServe(node, apiMaddr, opts...)
169
+ err = corehttp.ListenAndServe(node, apiMaddr.String(), opts...)
170
if err != nil {
171
res.SetError(err, cmds.ErrNormal)
172
return
cmd/ipfswatch/main.go
+2
-6
@@ -8,7 +8,6 @@ import (
8
"path/filepath"
9
10
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
11
- ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
11
process "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
12
homedir "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
13
fsnotify "github.com/jbenet/go-ipfs/Godeps/_workspace/src/gopkg.in/fsnotify.v1"
@@ -73,17 +72,14 @@ func run(ipfsPath, watchPath string) error {
72
defer node.Close()
73
74
if *http {
76
- maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5001")
77
- if err != nil {
78
- return err
79
- }
75
+ addr := "/ip4/127.0.0.1/tcp/5001"
76
var opts = []corehttp.ServeOption{
77
corehttp.GatewayOption,
78
corehttp.WebUIOption,
79
corehttp.CommandsOption(cmdCtx(node, ipfsPath)),
80
}
81
proc.Go(func(p process.Process) {
86
- if err := corehttp.ListenAndServe(node, maddr, opts...); err != nil {
82
+ if err := corehttp.ListenAndServe(node, addr, opts...); err != nil {
83
return
84
}
85
})
core/corehttp/corehttp.go
+11
-1
@@ -19,7 +19,17 @@ const (
19
20
type ServeOption func(*core.IpfsNode, *http.ServeMux) error
21
22
-func ListenAndServe(n *core.IpfsNode, addr ma.Multiaddr, options ...ServeOption) error {
22
+// ListenAndServe runs an HTTP server listening at |listeningMultiAddr| with
23
+// the given serve options. The address must be provided in multiaddr format.
24
+//
25
+// TODO intelligently parse address strings in other formats so long as they
26
+// unambiguously map to a valid multiaddr. e.g. for convenience, ":8080" should
27
+// map to "/ip4/0.0.0.0/tcp/8080".
28
+func ListenAndServe(n *core.IpfsNode, listeningMultiAddr string, options ...ServeOption) error {
29
+ addr, err := ma.NewMultiaddr(listeningMultiAddr)
30
+ if err != nil {
31
+ return err
32
+ }
33
mux := http.NewServeMux()
34
for _, option := range options {
35
if err := option(n, mux); err != nil {