commands/http: Pass root command in as field instead of statically depending on core/commands

This commit is contained in:
Matt Bell 2014-10-30 20:44:31 -07:00 committed by Juan Batiz-Benet
parent 460387fc3b
commit 1e0cabd4db
2 changed files with 6 additions and 9 deletions

View File

@ -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 {

View File

@ -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