http.go 1.84 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
// package http implements an http server that serves static content from ipfs
verokarhu's avatar
verokarhu committed
2 3 4
package http

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

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

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

17 18 19 20
type handler struct {
	ipfs
}

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

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

	return http.ListenAndServe(host, nil)
verokarhu's avatar
verokarhu committed
35 36
}

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

40
	nd, err := i.ResolvePath(path)
verokarhu's avatar
verokarhu committed
41 42
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
verokarhu's avatar
verokarhu committed
43 44 45 46 47 48 49 50 51
		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
52 53 54
		return
	}

verokarhu's avatar
verokarhu committed
55
	io.Copy(w, dr)
verokarhu's avatar
verokarhu committed
56 57
}

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

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

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