@cryptotaxi247 / kubo / commits / 897e0f869

cmds/ipfs2: Added '/ipfs' HTTP handling

Matt Bell committed Nov 8, 2014 at 21:09 UTC 897e0f869f9ec6300a5615a061cdee26402bd654
2 files changed +91 -2
cmd/ipfs2/daemon.go
+5 -2
@@ -50,8 +50,11 @@ func daemonFunc(res cmds.Response, req cmds.Request) {
50 return
51 }
52
53 - handler := cmdsHttp.NewHandler(*ctx, commands.Root)
54 - http.Handle(cmdsHttp.ApiPath+"/", handler)
53 + cmdHandler := cmdsHttp.NewHandler(*ctx, commands.Root)
54 + http.Handle(cmdsHttp.ApiPath+"/", cmdHandler)
55 +
56 + ifpsHandler := &ipfsHandler{node}
57 + http.Handle("/ipfs/", ifpsHandler)
58
59 fmt.Printf("API server listening on '%s'\n", host)
60
cmd/ipfs2/ipfsHandler.go new
+86
@@ -0,0 +1,86 @@
1 +package main
2 +
3 +import (
4 + "io"
5 + "net/http"
6 +
7 + mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
8 +
9 + core "github.com/jbenet/go-ipfs/core"
10 + "github.com/jbenet/go-ipfs/importer"
11 + dag "github.com/jbenet/go-ipfs/merkledag"
12 + uio "github.com/jbenet/go-ipfs/unixfs/io"
13 + u "github.com/jbenet/go-ipfs/util"
14 +)
15 +
16 +type ipfs interface {
17 + ResolvePath(string) (*dag.Node, error)
18 + NewDagFromReader(io.Reader) (*dag.Node, error)
19 + AddNodeToDAG(nd *dag.Node) (u.Key, error)
20 + NewDagReader(nd *dag.Node) (io.Reader, error)
21 +}
22 +
23 +type ipfsHandler struct {
24 + node *core.IpfsNode
25 +}
26 +
27 +func (i *ipfsHandler) ResolvePath(path string) (*dag.Node, error) {
28 + return i.node.Resolver.ResolvePath(path)
29 +}
30 +
31 +func (i *ipfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
32 + return importer.NewDagFromReader(r)
33 +}
34 +
35 +func (i *ipfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
36 + return i.node.DAG.Add(nd)
37 +}
38 +
39 +func (i *ipfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
40 + return uio.NewDagReader(nd, i.node.DAG)
41 +}
42 +
43 +func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
44 + path := r.URL.Path[5:]
45 +
46 + nd, err := i.ResolvePath(path)
47 + if err != nil {
48 + w.WriteHeader(http.StatusInternalServerError)
49 + log.Error(err)
50 + w.Write([]byte(err.Error()))
51 + return
52 + }
53 +
54 + dr, err := i.NewDagReader(nd)
55 + if err != nil {
56 + // TODO: return json object containing the tree data if it's a directory (err == ErrIsDir)
57 + w.WriteHeader(http.StatusInternalServerError)
58 + log.Error(err)
59 + w.Write([]byte(err.Error()))
60 + return
61 + }
62 +
63 + io.Copy(w, dr)
64 +}
65 +
66 +func (i *ipfsHandler) postHandler(w http.ResponseWriter, r *http.Request) {
67 + nd, err := i.NewDagFromReader(r.Body)
68 + if err != nil {
69 + w.WriteHeader(http.StatusInternalServerError)
70 + log.Error(err)
71 + w.Write([]byte(err.Error()))
72 + return
73 + }
74 +
75 + k, err := i.AddNodeToDAG(nd)
76 + if err != nil {
77 + w.WriteHeader(http.StatusInternalServerError)
78 + log.Error(err)
79 + w.Write([]byte(err.Error()))
80 + return
81 + }
82 +
83 + //TODO: return json representation of list instead
84 + w.WriteHeader(http.StatusCreated)
85 + w.Write([]byte(mh.Multihash(k).B58String()))
86 +}