1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-12 16:11:24 +08:00
Files
kubo/server/http/http.go
Juan Batiz-Benet 43452fdeb7 updated multiaddr + multiaddr-net
I broke out multiaddr/net to its own package so
that multiaddr parsing could remain lean. multiaddr-net
will vendor special implementations of protocols (like utp)
2014-11-20 00:58:48 -08:00

79 lines
1.8 KiB
Go

// package http implements an http server that serves static content from ipfs
package http
import (
"fmt"
"io"
"net/http"
mux "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gorilla/mux"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
core "github.com/jbenet/go-ipfs/core"
)
type handler struct {
ipfs
}
// Serve starts the http server
func Serve(address ma.Multiaddr, node *core.IpfsNode) error {
r := mux.NewRouter()
handler := &handler{&ipfsHandler{node}}
r.HandleFunc("/ipfs/", handler.postHandler).Methods("POST")
r.PathPrefix("/ipfs/").Handler(handler).Methods("GET")
http.Handle("/", r)
_, host, err := manet.DialArgs(address)
if err != nil {
return err
}
return http.ListenAndServe(host, nil)
}
func (i *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[5:]
nd, err := i.ResolvePath(path)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
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)
return
}
io.Copy(w, dr)
}
func (i *handler) postHandler(w http.ResponseWriter, r *http.Request) {
nd, err := i.NewDagFromReader(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println(err)
return
}
k, err := i.AddNodeToDAG(nd)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println(err)
return
}
//TODO: return json representation of list instead
w.WriteHeader(http.StatusCreated)
w.Write([]byte(mh.Multihash(k).B58String()))
}