@cryptotaxi247 / kubo / commits / b499c90db

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

Matt Bell committed Oct 24, 2014 at 14:32 UTC b499c90db3d51524b91b9eea4d6d1addc7bb2d56
1 file changed -72
server/http/http.go
-72
@@ -12,25 +12,19 @@ import (
12 manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
13 mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
14
15 - cmds "github.com/jbenet/go-ipfs/commands"
15 core "github.com/jbenet/go-ipfs/core"
17 - "github.com/jbenet/go-ipfs/core/commands"
16 )
17
18 type objectHandler struct {
19 ipfs
20 }
21
24 -type apiHandler struct{}
25 -
22 // Serve starts the http server
23 func Serve(address ma.Multiaddr, node *core.IpfsNode) error {
24 r := mux.NewRouter()
25 objectHandler := &objectHandler{&ipfsHandler{node}}
26 apiHandler := &apiHandler{}
27
32 - r.PathPrefix("/api/v0/").Handler(apiHandler).Methods("GET", "POST")
33 -
28 r.HandleFunc("/ipfs/", objectHandler.postHandler).Methods("POST")
29 r.PathPrefix("/ipfs/").Handler(objectHandler).Methods("GET")
30
@@ -84,69 +78,3 @@ func (i *objectHandler) postHandler(w http.ResponseWriter, r *http.Request) {
78 w.WriteHeader(http.StatusCreated)
79 w.Write([]byte(mh.Multihash(k).B58String()))
80 }
87 -
88 -func (i *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
89 - path := strings.Split(r.URL.Path, "/")[3:]
90 - opts := getOptions(r)
91 -
92 - // TODO: get args
93 -
94 - // ensure the requested command exists, otherwise 404
95 - _, err := commands.Root.Get(path)
96 - if err != nil {
97 - w.WriteHeader(http.StatusNotFound)
98 - w.Write([]byte("404 page not found"))
99 - return
100 - }
101 -
102 - // build the Request and call the command
103 - req := cmds.NewRequest(path, opts, nil, nil)
104 - res := commands.Root.Call(req)
105 -
106 - // set the Content-Type based on res output
107 - if _, ok := res.Value().(io.Reader); ok {
108 - // TODO: set based on actual Content-Type of file
109 - w.Header().Set("Content-Type", "application/octet-stream")
110 - } else {
111 - // TODO: get proper MIME type for encoding from multicodec lib
112 - enc, _ := req.Option(cmds.EncShort)
113 - w.Header().Set("Content-Type", "application/"+enc.(string))
114 - }
115 -
116 - // if response contains an error, write an HTTP error status code
117 - if e := res.Error(); e != nil {
118 - if e.Code == cmds.ErrClient {
119 - w.WriteHeader(http.StatusBadRequest)
120 - } else {
121 - w.WriteHeader(http.StatusInternalServerError)
122 - }
123 - }
124 -
125 - _, err = io.Copy(w, res)
126 - if err != nil {
127 - w.WriteHeader(http.StatusInternalServerError)
128 - w.Header().Set("Content-Type", "text/plain")
129 - w.Write([]byte(err.Error()))
130 - }
131 -}
132 -
133 -// getOptions returns the command options in the given HTTP request
134 -// (from the querystring and request body)
135 -func getOptions(r *http.Request) map[string]interface{} {
136 - opts := make(map[string]interface{})
137 -
138 - query := r.URL.Query()
139 - for k, v := range query {
140 - opts[k] = v[0]
141 - }
142 -
143 - // TODO: get more options from request body (formdata, json, etc)
144 -
145 - _, short := opts[cmds.EncShort]
146 - _, long := opts[cmds.EncLong]
147 - if !short && !long {
148 - opts[cmds.EncShort] = cmds.JSON
149 - }
150 -
151 - return opts
152 -}