-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.go
61 lines (52 loc) · 1.33 KB
/
route.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
package otto
import (
"net/http"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
// HandlerFunc defines the interface for r Route HandlerFunc
type HandlerFunc func(Context) error
// Route has information about the route
type Route struct {
mux *mux.Route
Path string
Method string
HandlerFunc HandlerFunc
router *Router
charset string
}
func (r Route) ServeHTTP(res http.ResponseWriter, req *http.Request) {
ctx := &context{
res: &Response{
ResponseWriter: res,
size: 0,
},
req: req,
charset: r.charset,
bindFunc: r.router.bindFunc,
}
if err := r.router.middleware.Handle(r)(ctx); err != nil {
r.renderError(err, ctx)
}
}
func (r Route) renderError(err error, ctx Context) {
code := 500
// check if err has underlying error of type HTTPError
if httpError, ok := errors.Cause(err).(HTTPError); ok {
code = httpError.Code
}
h := r.router.errorHandlers.Get(code)
if err = h(code, err, ctx); err != nil {
// ErrorHandler returned error
http.Error(ctx.Response(), err.Error(), 500)
}
}
// Routes alias for slice of routes
type Routes []*Route
func (r Routes) Len() int { return len(r) }
func (r Routes) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r Routes) Less(i, j int) bool {
x := r[i].Path // + r[i].Method
y := r[j].Path // + r[j].Method
return x < y
}