-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.go
43 lines (38 loc) · 897 Bytes
/
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
package h2o
import (
"net/http"
"reflect"
"runtime"
"time"
log "github.com/Sirupsen/logrus"
)
type route struct {
path string
method string
handlers []HandlerFunc
}
func (p *route) call(c *Context) error {
for _, h := range p.handlers {
log.Debugf("call %s", runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name())
if e := h(c); e != nil {
return e
}
}
return nil
}
func (p *route) handle(f func(http.ResponseWriter, *http.Request) *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
begin := time.Now()
ctx := f(w, r)
log.Infof("%s %s %s %s", r.Proto, r.Method, r.RequestURI, ctx.ClientIP())
if e := p.call(ctx); e != nil {
log.Error(e)
s := http.StatusInternalServerError
if he, ok := e.(*HTTPError); ok {
s = he.Status
}
http.Error(w, e.Error(), s)
}
log.Infof("done %s", time.Now().Sub(begin))
}
}