forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
75 lines (66 loc) · 1.76 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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/gobuffalo/buffalo")
}
func (c Context) error {
return c.Error(422, errors.New("oops!!"))
}
*/
type Handler func(Context) error
func (a *App) newContext(info RouteInfo, res http.ResponseWriter, req *http.Request) Context {
ws := res.(*buffaloResponse)
params := req.URL.Query()
vars := mux.Vars(req)
for k, v := range vars {
params.Set(k, v)
}
return &DefaultContext{
response: ws,
request: req,
params: params,
logger: a.Logger,
session: a.getSession(req, ws),
data: map[string]interface{}{
"env": a.Env,
"routes": a.Routes(),
"current_route": info,
},
}
}
func (a *App) handlerToHandler(info RouteInfo, h Handler) http.Handler {
hf := func(res http.ResponseWriter, req *http.Request) {
c := a.newContext(info, res, req)
err := a.Middleware.handler(h)(c)
if err != nil {
status := 500
if e, ok := err.(httpError); ok {
status = e.Status
}
eh := a.ErrorHandlers.Get(status)
err = eh(status, err, c)
if err != nil {
// things have really hit the fan if we're here!!
a.Logger.Error(err)
c.Response().WriteHeader(500)
c.Response().Write([]byte(err.Error()))
}
// err := c.Error(500, err)
// a.Logger.Error(err)
}
}
return http.HandlerFunc(hf)
}