quick implementation of middleware

This commit is contained in:
luke.rodham
2018-03-12 10:08:30 +00:00
parent 6740568afb
commit 1e787689aa
12 changed files with 187 additions and 84 deletions

View File

@@ -3,8 +3,6 @@ package tuu
import (
"net/http"
"github.com/gorilla/mux"
gcontext "github.com/gorilla/context"
)
@@ -12,16 +10,18 @@ type Route struct {
Method string
Path string
Handler Handler
MuxHandler mux.Route
Env string
Middleware MiddlewareStack
}
func (r *Route) ServeHTTP(res http.ResponseWriter, req *http.Request) {
defer gcontext.Clear(req)
c := NewContext(*r, res, req, r.Env)
c := newContext(*r, res, req)
if err := r.Handler(c); err != nil {
err := r.Middleware.handler(r)(c)
if err != nil {
c.Response().WriteHeader(500)
c.Response().Write([]byte(err.Error()))
}