Skip to content

Commit

Permalink
Merge pull request #66 from labstack/middleware
Browse files Browse the repository at this point in the history
Fixed #65
  • Loading branch information
vishr committed May 18, 2015
2 parents ac908b4 + aa5e552 commit 3809c0e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 33 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,9 @@ func main() {
// Echo instance
e := echo.New()

//------------
// Middleware
//------------

// Recover
e.Use(mw.Recover())

// Logger
e.Use(mw.Logger())
e.Use(mw.Recover())

// Routes
e.Get("/", hello)
Expand Down
27 changes: 16 additions & 11 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
prefix string
middleware []MiddlewareFunc
maxParam byte
notFoundHandler HandlerFunc
httpErrorHandler HTTPErrorHandler
binder BindFunc
renderer Renderer
Expand Down Expand Up @@ -134,13 +135,16 @@ func New() (e *Echo) {
//----------

e.MaxParam(5)
e.notFoundHandler = func(c *Context) *HTTPError {
return &HTTPError{Code: http.StatusNotFound}
}
e.HTTPErrorHandler(func(he *HTTPError, c *Context) {
if he.Code == 0 {
he.Code = http.StatusInternalServerError
}
if he.Message == "" {
he.Message = http.StatusText(he.Code)
if e.debug {
if e.debug && he.Error != nil {
he.Message = he.Error.Error()
}
}
Expand Down Expand Up @@ -321,18 +325,19 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
c.reset(w, r, e)
if h == nil {
c.Error(&HTTPError{Code: http.StatusNotFound})
} else {
// Chain middleware with handler in the end
for i := len(e.middleware) - 1; i >= 0; i-- {
h = e.middleware[i](h)
}
h = e.notFoundHandler
}

// Execute chain
if he := h(c); he != nil {
e.httpErrorHandler(he, c)
}
// Chain middleware with handler in the end
for i := len(e.middleware) - 1; i >= 0; i-- {
h = e.middleware[i](h)
}

// Execute chain
if he := h(c); he != nil {
e.httpErrorHandler(he, c)
}

e.pool.Put(c)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/crud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func main() {
e := echo.New()

// Middleware
e.Use(mw.Recover())
e.Use(mw.Logger())
e.Use(mw.Recover())

// Routes
e.Post("/users", createUser)
Expand Down
8 changes: 1 addition & 7 deletions examples/hello/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@ func main() {
// Echo instance
e := echo.New()

//------------
// Middleware
//------------

// Recover
e.Use(mw.Recover())

// Logger
e.Use(mw.Logger())
e.Use(mw.Recover())

// Routes
e.Get("/", hello)
Expand Down
6 changes: 3 additions & 3 deletions examples/middleware/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func main() {
// Middleware
//------------

// Recover
e.Use(mw.Recover())

// Logger
e.Use(mw.Logger())

// Recover
e.Use(mw.Recover())

// Basic auth
e.Use(mw.BasicAuth(func(u, p string) bool {
if u == "joe" && p == "secret" {
Expand Down
2 changes: 1 addition & 1 deletion examples/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func main() {
e := echo.New()

// Middleware
e.Use(mw.Recover())
e.Use(mw.Logger())
e.Use(mw.Recover())

//------------------------
// Third-party middleware
Expand Down
5 changes: 2 additions & 3 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,8 @@ func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h, _ := r.Find(req.Method, req.URL.Path, c)
c.reset(w, req, r.echo)
if h == nil {
c.Error(&HTTPError{Code: http.StatusNotFound})
} else {
h(c)
h = r.echo.notFoundHandler
}
h(c)
r.echo.pool.Put(c)
}
1 change: 1 addition & 0 deletions website/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func main() {

// Middleware
e.Use(mw.Logger())
e.Use(mw.Recover())

// Routes
e.Get("/", hello)
Expand Down

0 comments on commit 3809c0e

Please sign in to comment.