http.go 1.67 KB
Newer Older
verokarhu's avatar
verokarhu committed
1 2 3
package http

import (
verokarhu's avatar
verokarhu committed
4
	"fmt"
verokarhu's avatar
verokarhu committed
5
	"io"
verokarhu's avatar
verokarhu committed
6 7
	"net/http"

8
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gorilla/mux"
9
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
verokarhu's avatar
verokarhu committed
10
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
11

verokarhu's avatar
verokarhu committed
12 13 14
	core "github.com/jbenet/go-ipfs/core"
)

15 16 17 18
type handler struct {
	ipfs
}

verokarhu's avatar
verokarhu committed
19
// Serve starts the http server
20
func Serve(address *ma.Multiaddr, node *core.IpfsNode) error {
verokarhu's avatar
verokarhu committed
21
	r := mux.NewRouter()
verokarhu's avatar
verokarhu committed
22
	handler := &handler{&ipfsHandler{node}}
23 24
	r.HandleFunc("/ipfs/", handler.postHandler).Methods("POST")
	r.PathPrefix("/ipfs/").Handler(handler).Methods("GET")
verokarhu's avatar
verokarhu committed
25 26
	http.Handle("/", r)

27 28 29 30 31 32
	_, host, err := address.DialArgs()
	if err != nil {
		return err
	}

	return http.ListenAndServe(host, nil)
verokarhu's avatar
verokarhu committed
33 34
}

35
func (i *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
36
	path := r.URL.Path[5:]
verokarhu's avatar
verokarhu committed
37

38
	nd, err := i.ResolvePath(path)
verokarhu's avatar
verokarhu committed
39 40
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
verokarhu's avatar
verokarhu committed
41 42 43 44 45 46 47 48 49
		fmt.Println(err)
		return
	}

	dr, err := i.NewDagReader(nd)
	if err != nil {
		// TODO: return json object containing the tree data if it's a directory (err == ErrIsDir)
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Println(err)
verokarhu's avatar
verokarhu committed
50 51 52
		return
	}

verokarhu's avatar
verokarhu committed
53
	io.Copy(w, dr)
verokarhu's avatar
verokarhu committed
54 55
}

verokarhu's avatar
verokarhu committed
56 57
func (i *handler) postHandler(w http.ResponseWriter, r *http.Request) {
	nd, err := i.NewDagFromReader(r.Body)
verokarhu's avatar
verokarhu committed
58 59
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
verokarhu's avatar
verokarhu committed
60
		fmt.Println(err)
verokarhu's avatar
verokarhu committed
61 62
		return
	}
verokarhu's avatar
verokarhu committed
63

verokarhu's avatar
verokarhu committed
64
	k, err := i.AddNodeToDAG(nd)
verokarhu's avatar
verokarhu committed
65 66
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
verokarhu's avatar
verokarhu committed
67
		fmt.Println(err)
verokarhu's avatar
verokarhu committed
68 69 70 71 72
		return
	}

	//TODO: return json representation of list instead
	w.WriteHeader(http.StatusCreated)
verokarhu's avatar
verokarhu committed
73
	w.Write([]byte(mh.Multihash(k).B58String()))
verokarhu's avatar
verokarhu committed
74
}