From 1e0cabd4dbf4c187df8216f12085f8a70cc6098b Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Thu, 30 Oct 2014 20:44:31 -0700 Subject: [PATCH] commands/http: Pass root command in as field instead of statically depending on core/commands --- commands/http/handler.go | 8 ++++---- commands/http/parse.go | 7 ++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/commands/http/handler.go b/commands/http/handler.go index 5888172b1..aa636a3af 100644 --- a/commands/http/handler.go +++ b/commands/http/handler.go @@ -6,11 +6,11 @@ import ( "net/http" cmds "github.com/jbenet/go-ipfs/commands" - commands "github.com/jbenet/go-ipfs/core/commands2" ) type Handler struct { - Ctx cmds.Context + Ctx cmds.Context + Root *cmds.Command } var ErrNotFound = errors.New("404 page not found") @@ -22,7 +22,7 @@ var mimeTypes = map[string]string{ } func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - req, err := Parse(r) + req, err := Parse(r, i.Root) if err != nil { if err == ErrNotFound { w.WriteHeader(http.StatusNotFound) @@ -35,7 +35,7 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { req.SetContext(i.Ctx) // call the command - res := commands.Root.Call(req) + res := i.Root.Call(req) // set the Content-Type based on res output if _, ok := res.Value().(io.Reader); ok { diff --git a/commands/http/parse.go b/commands/http/parse.go index a84315e80..3176361a8 100644 --- a/commands/http/parse.go +++ b/commands/http/parse.go @@ -5,17 +5,14 @@ import ( "strings" cmds "github.com/jbenet/go-ipfs/commands" - commands "github.com/jbenet/go-ipfs/core/commands2" ) // Parse parses the data in a http.Request and returns a command Request object -func Parse(r *http.Request) (cmds.Request, error) { - // TODO: take root cmd as a param, like the commands/cli Parse - +func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) { path := strings.Split(r.URL.Path, "/")[3:] args := make([]string, 0) - cmd, err := commands.Root.Get(path[:len(path)-1]) + cmd, err := root.Get(path[:len(path)-1]) if err != nil { // 404 if there is no command at that path return nil, ErrNotFound