1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-05-18 11:33:11 -07:00
parent 73fa05f826
commit aa5e552282
8 changed files with 27 additions and 34 deletions

View File

@ -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)

13
echo.go
View File

@ -22,6 +22,7 @@ type (
prefix string
middleware []MiddlewareFunc
maxParam byte
notFoundHandler HandlerFunc
httpErrorHandler HTTPErrorHandler
binder BindFunc
renderer Renderer
@ -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()
}
}
@ -321,8 +325,9 @@ 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 {
h = e.notFoundHandler
}
// Chain middleware with handler in the end
for i := len(e.middleware) - 1; i >= 0; i-- {
h = e.middleware[i](h)
@ -332,7 +337,7 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if he := h(c); he != nil {
e.httpErrorHandler(he, c)
}
}
e.pool.Put(c)
}

View File

@ -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)

View File

@ -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)

View File

@ -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" {

View File

@ -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

View File

@ -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)
}

View File

@ -67,6 +67,7 @@ func main() {
// Middleware
e.Use(mw.Logger())
e.Use(mw.Recover())
// Routes
e.Get("/", hello)