1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 10:49:24 +08:00

commands/http: Unexported Handler fields and created constructor

This commit is contained in:
Matt Bell
2014-11-03 20:08:08 -08:00
committed by Juan Batiz-Benet
parent 4552fce517
commit f76048f74b

View File

@ -9,8 +9,8 @@ import (
)
type Handler struct {
Ctx cmds.Context
Root *cmds.Command
ctx cmds.Context
root *cmds.Command
}
var ErrNotFound = errors.New("404 page not found")
@ -21,8 +21,12 @@ var mimeTypes = map[string]string{
cmds.Text: "text/plain",
}
func NewHandler(ctx cmds.Context, root *cmds.Command) *Handler {
return &Handler{ctx, root}
}
func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req, err := Parse(r, i.Root)
req, err := Parse(r, i.root)
if err != nil {
if err == ErrNotFound {
w.WriteHeader(http.StatusNotFound)
@ -32,10 +36,10 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(err.Error()))
return
}
req.SetContext(i.Ctx)
req.SetContext(i.ctx)
// call the command
res := i.Root.Call(req)
res := i.root.Call(req)
// set the Content-Type based on res output
if _, ok := res.Output().(io.Reader); ok {