rudimentary HTTP POST support
verokarhu committed
Aug 6, 2014 at 20:03 UTC
8831427c82eacb86cb8c4093697529f407965f2d
2 files changed
+32
-9
cmd/ipfs/serve.go
+6
-1
@@ -2,9 +2,11 @@ package main
2
3
import (
4
"errors"
5
+ "fmt"
6
"github.com/gonuts/flag"
7
"github.com/jbenet/commander"
8
h "github.com/jbenet/go-ipfs/http"
9
+ "strconv"
10
)
11
12
var cmdIpfsServe = &commander.Command{
@@ -30,5 +32,8 @@ func serveCmd(c *commander.Command, _ []string) error {
32
return err
33
}
34
33
- return h.Serve("127.0.0.1", port, n)
35
+ address := "127.0.0.1" + ":" + strconv.FormatUint(uint64(port), 10)
36
+ fmt.Printf("Serving on %s\n", address)
37
+
38
+ return h.Serve(address, n)
39
}
http/http.go
+26
-8
@@ -4,14 +4,24 @@ import (
4
"fmt"
5
"github.com/gorilla/mux"
6
core "github.com/jbenet/go-ipfs/core"
7
+ "github.com/jbenet/go-ipfs/importer"
8
+ mh "github.com/jbenet/go-multihash"
9
"net/http"
8
- "strconv"
10
)
11
12
type ipfsHandler struct {
13
node *core.IpfsNode
14
}
15
16
+func Serve(address string, node *core.IpfsNode) error {
17
+ r := mux.NewRouter()
18
+ r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { ipfsPostHandler(w, r, node) }).Methods("POST")
19
+ r.PathPrefix("/").Handler(&ipfsHandler{node}).Methods("GET")
20
+ http.Handle("/", r)
21
+
22
+ return http.ListenAndServe(address, nil)
23
+}
24
+
25
func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
26
path := r.URL.Path
27
@@ -24,13 +34,21 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
34
fmt.Fprintf(w, "%s", nd.Data)
35
}
36
27
-func Serve(host string, port uint, node *core.IpfsNode) error {
28
- r := mux.NewRouter()
29
- r.PathPrefix("/").Handler(&ipfsHandler{node}).Methods("GET")
30
- http.Handle("/", r)
37
+func ipfsPostHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode) {
38
32
- address := host + ":" + strconv.FormatUint(uint64(port), 10)
33
- fmt.Printf("Serving on %s\n", address)
39
+ root, err := importer.NewDagFromReader(r.Body, 1)
40
+ if err != nil {
41
+ w.WriteHeader(http.StatusInternalServerError)
42
+ return
43
+ }
44
35
- return http.ListenAndServe(address, nil)
45
+ k, err := node.DAG.Put(root)
46
+ if err != nil {
47
+ w.WriteHeader(http.StatusInternalServerError)
48
+ return
49
+ }
50
+
51
+ //TODO: return json representation of list instead
52
+ w.WriteHeader(http.StatusCreated)
53
+ fmt.Fprintf(w, "%s", mh.Multihash(k).B58String())
54
}