http.go 1.85 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
	mux "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"
)

Matt Bell's avatar
Matt Bell committed
17
type handler struct {
18 19 20
	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()
Matt Bell's avatar
Matt Bell committed
24
	handler := &handler{&ipfsHandler{node}}
25

Matt Bell's avatar
Matt Bell committed
26 27
	r.HandleFunc("/ipfs/", handler.postHandler).Methods("POST")
	r.PathPrefix("/ipfs/").Handler(handler).Methods("GET")
28

verokarhu's avatar
verokarhu committed
29 30
	http.Handle("/", r)

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

	return http.ListenAndServe(host, nil)
verokarhu's avatar
verokarhu committed
37 38
}

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

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

verokarhu's avatar
verokarhu committed
57
	io.Copy(w, dr)
verokarhu's avatar
verokarhu committed
58 59
}

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

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

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