1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 19:09:50 +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 { type Handler struct {
Ctx cmds.Context ctx cmds.Context
Root *cmds.Command root *cmds.Command
} }
var ErrNotFound = errors.New("404 page not found") var ErrNotFound = errors.New("404 page not found")
@ -21,8 +21,12 @@ var mimeTypes = map[string]string{
cmds.Text: "text/plain", 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) { 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 != nil {
if err == ErrNotFound { if err == ErrNotFound {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
@ -32,10 +36,10 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
} }
req.SetContext(i.Ctx) req.SetContext(i.ctx)
// call the command // call the command
res := i.Root.Call(req) res := i.root.Call(req)
// set the Content-Type based on res output // set the Content-Type based on res output
if _, ok := res.Output().(io.Reader); ok { if _, ok := res.Output().(io.Reader); ok {