@cryptotaxi247 / kubo / commits / 36bdc456d

commands/http: Moved HTTP RPC into commands/http package

Matt Bell committed Oct 24, 2014 at 14:16 UTC 36bdc456d2b0b46a6b4d1de80b931d72168c7d08
1 file changed +5 -67
cmd/ipfs/ipfs.go
+5 -67
@@ -1,16 +1,14 @@
1 package main
2
3 import (
4 - "encoding/json"
4 "fmt"
5 "io"
7 - "net/http"
6 "os"
7 "runtime/pprof"
10 - "strings"
8
9 cmds "github.com/jbenet/go-ipfs/commands"
13 - "github.com/jbenet/go-ipfs/commands/cli"
10 + cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
11 + cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
12 "github.com/jbenet/go-ipfs/core/commands"
13 u "github.com/jbenet/go-ipfs/util"
14 )
@@ -18,19 +16,17 @@ import (
16 // log is the command logger
17 var log = u.Logger("cmd/ipfs")
18
21 -const API_PATH = "/api/v0"
22 -
19 func main() {
20 args := os.Args[1:]
21
26 - req, err := cli.Parse(args, Root)
22 + req, err := cmdsCli.Parse(args, Root)
23 if err != nil {
24 fmt.Println(err)
25 os.Exit(1)
26 }
27
28 if len(req.Path()) == 0 {
33 - req, err = cli.Parse(args, commands.Root)
29 + req, err = cmdsCli.Parse(args, commands.Root)
30 if err != nil {
31 fmt.Println(err)
32 os.Exit(1)
@@ -82,7 +78,7 @@ func main() {
78 // TODO: spin up node
79 res = root.Call(req)
80 } else {
85 - res, err = sendRequest(req)
81 + res, err = cmdsHttp.Send(req)
82 if err != nil {
83 fmt.Println(err)
84 os.Exit(1)
@@ -105,61 +101,3 @@ func main() {
101 fmt.Println(err.Error())
102 }
103 }
108 -
109 -func sendRequest(req cmds.Request) (cmds.Response, error) {
110 - // TODO: load RPC host from config
111 - url := "http://localhost:8080" + API_PATH
112 - url += "/" + strings.Join(req.Path(), "/")
113 -
114 - // TODO: support other encodings once we have multicodec to decode response
115 - // (we shouldn't have to set this here)
116 - encoding := cmds.JSON
117 - req.SetOption(cmds.EncShort, encoding)
118 -
119 - query := "?"
120 - options := req.Options()
121 - for k, v := range options {
122 - query += "&" + k + "=" + v.(string)
123 - }
124 -
125 - httpRes, err := http.Post(url+query, "application/octet-stream", req.Stream())
126 - if err != nil {
127 - return nil, err
128 - }
129 -
130 - res := cmds.NewResponse(req)
131 -
132 - contentType := httpRes.Header["Content-Type"][0]
133 - contentType = strings.Split(contentType, ";")[0]
134 -
135 - if contentType == "application/octet-stream" {
136 - res.SetValue(httpRes.Body)
137 - return res, nil
138 - }
139 -
140 - // TODO: decode based on `encoding`, using multicodec
141 - dec := json.NewDecoder(httpRes.Body)
142 -
143 - if httpRes.StatusCode >= http.StatusBadRequest {
144 - e := cmds.Error{}
145 - err = dec.Decode(&e)
146 - if err != nil {
147 - fmt.Println(err)
148 - return nil, err
149 - }
150 -
151 - res.SetError(e, e.Code)
152 -
153 - } else {
154 - var v interface{}
155 - err = dec.Decode(&v)
156 - if err != nil {
157 - fmt.Println(err)
158 - return nil, err
159 - }
160 -
161 - res.SetValue(v)
162 - }
163 -
164 - return res, nil
165 -}