Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
73353824
Commit
73353824
authored
Oct 22, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Nov 14, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
server/http: Added HTTP API handler
parent
e64ffb9a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
6 deletions
+88
-6
server/http/http.go
server/http/http.go
+88
-6
No files found.
server/http/http.go
View file @
73353824
...
...
@@ -5,25 +5,35 @@ import (
"fmt"
"io"
"net/http"
"strings"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gorilla/mux"
ma
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
mh
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
cmds
"github.com/jbenet/go-ipfs/commands"
core
"github.com/jbenet/go-ipfs/core"
"github.com/jbenet/go-ipfs/core/commands"
)
type
h
andler
struct
{
type
objectH
andler
struct
{
ipfs
}
type
apiHandler
struct
{}
// Serve starts the http server
func
Serve
(
address
ma
.
Multiaddr
,
node
*
core
.
IpfsNode
)
error
{
r
:=
mux
.
NewRouter
()
handler
:=
&
handler
{
&
ipfsHandler
{
node
}}
r
.
HandleFunc
(
"/ipfs/"
,
handler
.
postHandler
)
.
Methods
(
"POST"
)
r
.
PathPrefix
(
"/ipfs/"
)
.
Handler
(
handler
)
.
Methods
(
"GET"
)
objectHandler
:=
&
objectHandler
{
&
ipfsHandler
{
node
}}
apiHandler
:=
&
apiHandler
{}
r
.
PathPrefix
(
"/api/v0/"
)
.
Handler
(
apiHandler
)
.
Methods
(
"GET"
,
"POST"
)
r
.
HandleFunc
(
"/ipfs/"
,
objectHandler
.
postHandler
)
.
Methods
(
"POST"
)
r
.
PathPrefix
(
"/ipfs/"
)
.
Handler
(
objectHandler
)
.
Methods
(
"GET"
)
http
.
Handle
(
"/"
,
r
)
_
,
host
,
err
:=
manet
.
DialArgs
(
address
)
...
...
@@ -34,7 +44,7 @@ func Serve(address ma.Multiaddr, node *core.IpfsNode) error {
return
http
.
ListenAndServe
(
host
,
nil
)
}
func
(
i
*
h
andler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
i
*
objectH
andler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
path
:=
r
.
URL
.
Path
[
5
:
]
nd
,
err
:=
i
.
ResolvePath
(
path
)
...
...
@@ -55,7 +65,7 @@ func (i *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
io
.
Copy
(
w
,
dr
)
}
func
(
i
*
h
andler
)
postHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
i
*
objectH
andler
)
postHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
nd
,
err
:=
i
.
NewDagFromReader
(
r
.
Body
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
...
...
@@ -74,3 +84,75 @@ func (i *handler) postHandler(w http.ResponseWriter, r *http.Request) {
w
.
WriteHeader
(
http
.
StatusCreated
)
w
.
Write
([]
byte
(
mh
.
Multihash
(
k
)
.
B58String
()))
}
func
(
i
*
apiHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
path
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)[
3
:
]
opts
:=
getOptions
(
r
)
// TODO: get args
// ensure the requested command exists, otherwise 404
_
,
err
:=
commands
.
Root
.
Get
(
path
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusNotFound
)
w
.
Write
([]
byte
(
"404 page not found"
))
return
}
// build the Request and call the command
req
:=
cmds
.
NewRequest
(
path
,
opts
,
nil
,
nil
)
res
:=
commands
.
Root
.
Call
(
req
)
// if response contains an error, write an HTTP error status code
if
err
=
res
.
Error
();
err
!=
nil
{
e
:=
err
.
(
cmds
.
Error
)
if
e
.
Code
==
cmds
.
ErrClient
{
w
.
WriteHeader
(
http
.
StatusBadRequest
)
}
else
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
}
}
val
:=
res
.
Value
()
// if the output value is a io.Reader, stream its output in the request body
if
stream
,
ok
:=
val
.
(
io
.
Reader
);
ok
{
io
.
Copy
(
w
,
stream
)
return
}
// otherwise, marshall and output the response value or error
if
val
!=
nil
||
res
.
Error
()
!=
nil
{
output
,
err
:=
res
.
Marshal
()
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
fmt
.
Println
(
err
)
return
}
if
output
!=
nil
{
w
.
Write
(
output
)
}
}
}
// getOptions returns the command options in the given HTTP request
// (from the querystring and request body)
func
getOptions
(
r
*
http
.
Request
)
map
[
string
]
interface
{}
{
opts
:=
make
(
map
[
string
]
interface
{})
query
:=
r
.
URL
.
Query
()
for
k
,
v
:=
range
query
{
opts
[
k
]
=
v
[
0
]
}
// TODO: get more options from request body (formdata, json, etc)
if
_
,
exists
:=
opts
[
cmds
.
EncShort
];
!
exists
{
opts
[
cmds
.
EncShort
]
=
cmds
.
JSON
}
return
opts
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment