1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-06 03:19:47 +08:00

rudimentary HTTP POST support

This commit is contained in:
verokarhu
2014-08-06 20:03:45 +03:00
parent bb924cab5b
commit 8831427c82
2 changed files with 32 additions and 9 deletions

View File

@ -2,9 +2,11 @@ package main
import (
"errors"
"fmt"
"github.com/gonuts/flag"
"github.com/jbenet/commander"
h "github.com/jbenet/go-ipfs/http"
"strconv"
)
var cmdIpfsServe = &commander.Command{
@ -30,5 +32,8 @@ func serveCmd(c *commander.Command, _ []string) error {
return err
}
return h.Serve("127.0.0.1", port, n)
address := "127.0.0.1" + ":" + strconv.FormatUint(uint64(port), 10)
fmt.Printf("Serving on %s\n", address)
return h.Serve(address, n)
}

View File

@ -4,14 +4,24 @@ import (
"fmt"
"github.com/gorilla/mux"
core "github.com/jbenet/go-ipfs/core"
"github.com/jbenet/go-ipfs/importer"
mh "github.com/jbenet/go-multihash"
"net/http"
"strconv"
)
type ipfsHandler struct {
node *core.IpfsNode
}
func Serve(address string, node *core.IpfsNode) error {
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { ipfsPostHandler(w, r, node) }).Methods("POST")
r.PathPrefix("/").Handler(&ipfsHandler{node}).Methods("GET")
http.Handle("/", r)
return http.ListenAndServe(address, nil)
}
func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
@ -24,13 +34,21 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", nd.Data)
}
func Serve(host string, port uint, node *core.IpfsNode) error {
r := mux.NewRouter()
r.PathPrefix("/").Handler(&ipfsHandler{node}).Methods("GET")
http.Handle("/", r)
func ipfsPostHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode) {
address := host + ":" + strconv.FormatUint(uint64(port), 10)
fmt.Printf("Serving on %s\n", address)
root, err := importer.NewDagFromReader(r.Body, 1)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
return http.ListenAndServe(address, nil)
k, err := node.DAG.Put(root)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
//TODO: return json representation of list instead
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, "%s", mh.Multihash(k).B58String())
}