use --http flag to run expose IPFS HTTP API
Brian Tiger Chow committed
Jan 22, 2015 at 01:29 UTC
a6eb39614c1c13ae4cddb04c0a16dd1e861caf8b
1 file changed
+41
-9
cmd/ipfswatch/main.go
+41
-9
@@ -6,17 +6,19 @@ import (
6
"os"
7
"path/filepath"
8
9
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
10
-)
11
-import (
12
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
13
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/gopkg.in/fsnotify.v1"
14
- "github.com/jbenet/go-ipfs/core"
15
- "github.com/jbenet/go-ipfs/core/coreunix"
16
- "github.com/jbenet/go-ipfs/repo/config"
17
- "github.com/jbenet/go-ipfs/repo/fsrepo"
9
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
10
+ ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
11
+ homedir "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
12
+ fsnotify "github.com/jbenet/go-ipfs/Godeps/_workspace/src/gopkg.in/fsnotify.v1"
13
+ commands "github.com/jbenet/go-ipfs/commands"
14
+ core "github.com/jbenet/go-ipfs/core"
15
+ corehttp "github.com/jbenet/go-ipfs/core/corehttp"
16
+ coreunix "github.com/jbenet/go-ipfs/core/coreunix"
17
+ config "github.com/jbenet/go-ipfs/repo/config"
18
+ fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
19
)
20
21
+var http = flag.Bool("http", false, "expose IPFS HTTP API")
22
var repoPath = flag.String("repo", os.Getenv("IPFS_PATH"), "IPFS_PATH to use")
23
var watchPath = flag.String("path", ".", "the path to watch")
24
@@ -66,6 +68,21 @@ func run(ipfsPath, watchPath string) error {
68
}
69
defer node.Close()
70
71
+ if *http {
72
+ maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5001")
73
+ if err != nil {
74
+ return err
75
+ }
76
+ var opts = []corehttp.ServeOption{
77
+ corehttp.GatewayOption,
78
+ corehttp.WebUIOption,
79
+ corehttp.CommandsOption(cmdCtx(node, ipfsPath)),
80
+ }
81
+ if err := corehttp.ListenAndServe(node, maddr, opts...); err != nil {
82
+ return nil
83
+ }
84
+ }
85
+
86
for {
87
select {
88
case e := <-watcher.Events:
@@ -152,3 +169,18 @@ func IsHidden(path string) bool {
169
}
170
return false
171
}
172
+
173
+func cmdCtx(node *core.IpfsNode, repoPath string) commands.Context {
174
+ return commands.Context{
175
+ // TODO deprecate this shit
176
+ Context: context.Background(),
177
+ Online: true,
178
+ ConfigRoot: repoPath,
179
+ LoadConfig: func(path string) (*config.Config, error) {
180
+ return node.Repo.Config(), nil
181
+ },
182
+ ConstructNode: func() (*core.IpfsNode, error) {
183
+ return node, nil
184
+ },
185
+ }
186
+}