Commit e8403b1f authored by Henry's avatar Henry

removed dead code

- old http server (superseeded by core/corehttp)
- unused makeDatastore() helpers
parent 92c827b5
package core
import (
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
fsds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/fs"
ktds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/keytransform"
lds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/leveldb"
syncds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
ldbopts "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/opt"
config "github.com/jbenet/go-ipfs/repo/config"
u "github.com/jbenet/go-ipfs/util"
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
"github.com/jbenet/go-ipfs/util/debugerror"
)
func makeDatastore(cfg config.Datastore) (ds2.ThreadSafeDatastoreCloser, error) {
if len(cfg.Type) == 0 {
return nil, debugerror.Errorf("config datastore.type required")
}
switch cfg.Type {
case "leveldb":
return makeLevelDBDatastore(cfg)
case "memory":
return ds2.CloserWrap(syncds.MutexWrap(ds.NewMapDatastore())), nil
case "fs":
log.Warning("using fs.Datastore at .datastore for testing.")
d, err := fsds.NewDatastore(".datastore") // for testing!!
if err != nil {
return nil, err
}
ktd := ktds.Wrap(d, u.B58KeyConverter)
return ds2.CloserWrap(syncds.MutexWrap(ktd)), nil
}
return nil, debugerror.Errorf("Unknown datastore type: %s", cfg.Type)
}
func makeLevelDBDatastore(cfg config.Datastore) (ds2.ThreadSafeDatastoreCloser, error) {
if len(cfg.Path) == 0 {
return nil, debugerror.Errorf("config datastore.path required for leveldb")
}
ds, err := lds.NewDatastore(cfg.Path, &lds.Options{
// TODO don't import ldbopts. Get from go-datastore.leveldb
Compression: ldbopts.NoCompression,
})
return ds, debugerror.Wrap(err)
}
// 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()))
}
package http
import (
"bytes"
"errors"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
dag "github.com/jbenet/go-ipfs/merkledag"
u "github.com/jbenet/go-ipfs/util"
)
type test struct {
url string
code int
reqbody string
respbody string
}
func TestServeHTTP(t *testing.T) {
testhandler := &handler{&testIpfsHandler{}}
tests := []test{
{"/ipfs/", http.StatusInternalServerError, "", ""},
{"/ipfs/hash", http.StatusOK, "", "some fine data"},
{"/ipfs/hash2", http.StatusInternalServerError, "", ""},
}
for _, test := range tests {
req, _ := http.NewRequest("GET", test.url, nil)
resp := httptest.NewRecorder()
testhandler.ServeHTTP(resp, req)
if resp.Code != test.code {
t.Error("expected status code", test.code, "received", resp.Code)
}
if resp.Body.String() != test.respbody {
t.Error("expected body:", test.respbody)
t.Error("received body:", resp.Body)
}
}
}
func TestPostHandler(t *testing.T) {
testhandler := &handler{&testIpfsHandler{}}
tests := []test{
{"/ifps/", http.StatusInternalServerError, "", ""},
{"/ipfs/", http.StatusInternalServerError, "something that causes an error in adding to DAG", ""},
{"/ipfs/", http.StatusCreated, "some fine data", "jSQBpNSebeYbPBjs1vp"},
}
for _, test := range tests {
req, _ := http.NewRequest("POST", test.url, strings.NewReader(test.reqbody))
resp := httptest.NewRecorder()
testhandler.postHandler(resp, req)
if resp.Code != test.code {
t.Error("expected status code", test.code, "received", resp.Code)
}
if resp.Body.String() != test.respbody {
t.Error("expected body:", test.respbody)
t.Error("received body:", resp.Body)
}
}
}
type testIpfsHandler struct{}
func (i *testIpfsHandler) ResolvePath(path string) (*dag.Node, error) {
if path == "/hash" {
return &dag.Node{Data: []byte("some fine data")}, nil
}
if path == "/hash2" {
return &dag.Node{Data: []byte("data that breaks dagreader")}, nil
}
return nil, errors.New("")
}
func (i *testIpfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
if data, err := ioutil.ReadAll(r); err == nil {
return &dag.Node{Data: data}, nil
}
return nil, errors.New("")
}
func (i *testIpfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
if len(nd.Data) != 0 && string(nd.Data) != "something that causes an error in adding to DAG" {
return u.Key(nd.Data), nil
}
return "", errors.New("")
}
func (i *testIpfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
if string(nd.Data) != "data that breaks dagreader" {
return bytes.NewReader(nd.Data), nil
}
return nil, errors.New("")
}
package http
import (
"io"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
core "github.com/jbenet/go-ipfs/core"
"github.com/jbenet/go-ipfs/importer"
chunk "github.com/jbenet/go-ipfs/importer/chunk"
dag "github.com/jbenet/go-ipfs/merkledag"
path "github.com/jbenet/go-ipfs/path"
uio "github.com/jbenet/go-ipfs/unixfs/io"
u "github.com/jbenet/go-ipfs/util"
)
type ipfs interface {
ResolvePath(string) (*dag.Node, error)
NewDagFromReader(io.Reader) (*dag.Node, error)
AddNodeToDAG(nd *dag.Node) (u.Key, error)
NewDagReader(nd *dag.Node) (io.Reader, error)
}
type ipfsHandler struct {
node *core.IpfsNode
}
func (i *ipfsHandler) ResolvePath(fpath string) (*dag.Node, error) {
return i.node.Resolver.ResolvePath(path.Path(fpath))
}
func (i *ipfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
return importer.BuildDagFromReader(
r, i.node.DAG, i.node.Pinning.GetManual(), chunk.DefaultSplitter)
}
func (i *ipfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
return i.node.DAG.Add(nd)
}
func (i *ipfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
return uio.NewDagReader(context.TODO(), nd, i.node.DAG)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment