commands/http: Unexported Handler fields and created constructor
Matt Bell committed
Nov 3, 2014 at 20:08 UTC
f76048f74b4aa1da8d52f86f04742a478110f4e6
1 file changed
+9
-5
commands/http/handler.go
+9
-5
@@ -9,8 +9,8 @@ import (
9
)
10
11
type Handler struct {
12
- Ctx cmds.Context
13
- Root *cmds.Command
12
+ ctx cmds.Context
13
+ root *cmds.Command
14
}
15
16
var ErrNotFound = errors.New("404 page not found")
@@ -21,8 +21,12 @@ var mimeTypes = map[string]string{
21
cmds.Text: "text/plain",
22
}
23
24
+func NewHandler(ctx cmds.Context, root *cmds.Command) *Handler {
25
+ return &Handler{ctx, root}
26
+}
27
+
28
func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
25
- req, err := Parse(r, i.Root)
29
+ req, err := Parse(r, i.root)
30
if err != nil {
31
if err == ErrNotFound {
32
w.WriteHeader(http.StatusNotFound)
@@ -32,10 +36,10 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
36
w.Write([]byte(err.Error()))
37
return
38
}
35
- req.SetContext(i.Ctx)
39
+ req.SetContext(i.ctx)
40
41
// call the command
38
- res := i.Root.Call(req)
42
+ res := i.root.Call(req)
43
44
// set the Content-Type based on res output
45
if _, ok := res.Output().(io.Reader); ok {