forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
56 lines (47 loc) · 1.22 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package buffalo
import (
"net/http"
"github.com/gorilla/mux"
)
// Handler is the basis for all of Buffalo. A Handler
// will be given a Context interface that represents the
// give request/response. It is the responsibility of the
// Handler to handle the request/response correctly. This
// could mean rendering a template, JSON, etc... or it could
// mean returning an error.
/*
func (c Context) error {
return C.Render(200, render.String("Hello World!"))
}
func (c Context) error {
return C.Redirect(301, "http://github.com/markbates/buffalo")
}
func (c Context) error {
return c.Error(422, errors.New("oops!!"))
}
*/
type Handler func(Context) error
func (a *App) handlerToHandler(h Handler) http.Handler {
hf := func(res http.ResponseWriter, req *http.Request) {
ws := res.(*buffaloResponse)
params := req.URL.Query()
vars := mux.Vars(req)
for k, v := range vars {
params.Set(k, v)
}
c := &DefaultContext{
response: ws,
request: req,
params: params,
logger: a.Logger,
session: a.getSession(req, ws),
data: map[string]interface{}{},
}
err := a.Middleware.handler(h)(c)
if err != nil {
err := c.Error(500, err)
a.Logger.Error(err)
}
}
return http.HandlerFunc(hf)
}