@cryptotaxi247 / kubo / commits / 1e0cabd4d

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

Matt Bell committed Oct 30, 2014 at 20:44 UTC 1e0cabd4dbf4c187df8216f12085f8a70cc6098b
2 files changed +6 -9
commands/http/handler.go
+4 -4
@@ -6,11 +6,11 @@ import (
6 "net/http"
7
8 cmds "github.com/jbenet/go-ipfs/commands"
9 - commands "github.com/jbenet/go-ipfs/core/commands2"
9 )
10
11 type Handler struct {
13 - Ctx cmds.Context
12 + Ctx cmds.Context
13 + Root *cmds.Command
14 }
15
16 var ErrNotFound = errors.New("404 page not found")
@@ -22,7 +22,7 @@ var mimeTypes = map[string]string{
22 }
23
24 func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
25 - req, err := Parse(r)
25 + req, err := Parse(r, i.Root)
26 if err != nil {
27 if err == ErrNotFound {
28 w.WriteHeader(http.StatusNotFound)
@@ -35,7 +35,7 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
35 req.SetContext(i.Ctx)
36
37 // call the command
38 - res := commands.Root.Call(req)
38 + res := i.Root.Call(req)
39
40 // set the Content-Type based on res output
41 if _, ok := res.Value().(io.Reader); ok {
commands/http/parse.go
+2 -5
@@ -5,17 +5,14 @@ import (
5 "strings"
6
7 cmds "github.com/jbenet/go-ipfs/commands"
8 - commands "github.com/jbenet/go-ipfs/core/commands2"
8 )
9
10 // Parse parses the data in a http.Request and returns a command Request object
12 -func Parse(r *http.Request) (cmds.Request, error) {
13 - // TODO: take root cmd as a param, like the commands/cli Parse
14 -
11 +func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
12 path := strings.Split(r.URL.Path, "/")[3:]
13 args := make([]string, 0)
14
18 - cmd, err := commands.Root.Get(path[:len(path)-1])
15 + cmd, err := root.Get(path[:len(path)-1])
16 if err != nil {
17 // 404 if there is no command at that path
18 return nil, ErrNotFound