From 6abe7094904a7549b8f2094664774cc2f704155a Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Mon, 20 Apr 2015 23:24:34 -0700 Subject: [PATCH] Moved middleware in a package Signed-off-by: Vishal Rana --- README.md | 3 ++- examples/crud/server.go | 3 ++- examples/hello/server.go | 3 ++- examples/web/server.go | 3 ++- middleware.go | 34 ---------------------------------- website/docs/index.md | 7 ++++--- 6 files changed, 12 insertions(+), 41 deletions(-) delete mode 100644 middleware.go diff --git a/README.md b/README.md index c6d1a656..282da681 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ import ( "net/http" "github.com/labstack/echo" + mw "github.com/labstack/echo/middleware" ) // Handler @@ -92,7 +93,7 @@ func main() { e := echo.New() // Middleware - e.Use(echo.Logger) + e.Use(mw.Logger) // Routes e.Get("/", hello) diff --git a/examples/crud/server.go b/examples/crud/server.go index 653d85e0..188bec4c 100644 --- a/examples/crud/server.go +++ b/examples/crud/server.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/labstack/echo" + mw "github.com/labstack/echo/middleware" ) type ( @@ -60,7 +61,7 @@ func main() { e := echo.New() // Middleware - e.Use(echo.Logger) + e.Use(mw.Logger) // Routes e.Post("/users", createUser) diff --git a/examples/hello/server.go b/examples/hello/server.go index f6263ca9..1bf2b6e0 100644 --- a/examples/hello/server.go +++ b/examples/hello/server.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/labstack/echo" + mw "github.com/labstack/echo/middleware" ) // Handler @@ -15,7 +16,7 @@ func main() { e := echo.New() // Middleware - e.Use(echo.Logger) + e.Use(mw.Logger) // Routes e.Get("/", hello) diff --git a/examples/web/server.go b/examples/web/server.go index f31420df..be19dcbd 100644 --- a/examples/web/server.go +++ b/examples/web/server.go @@ -7,6 +7,7 @@ import ( "html/template" "github.com/labstack/echo" + mw "github.com/labstack/echo/middleware" "github.com/rs/cors" "github.com/thoas/stats" ) @@ -57,7 +58,7 @@ func main() { e := echo.New() // Middleware - e.Use(echo.Logger) + e.Use(mw.Logger) //------------------------ // Third-party middleware diff --git a/middleware.go b/middleware.go deleted file mode 100644 index 30eaf8bb..00000000 --- a/middleware.go +++ /dev/null @@ -1,34 +0,0 @@ -package echo - -import ( - "log" - "time" - - "github.com/labstack/gommon/color" -) - -func Logger(h HandlerFunc) HandlerFunc { - return func(c *Context) error { - start := time.Now() - if err := h(c); err != nil { - c.Error(err) - } - end := time.Now() - m := c.Request.Method - p := c.Request.URL.Path - n := c.Response.Status() - col := color.Green - - switch { - case n >= 500: - col = color.Red - case n >= 400: - col = color.Yellow - case n >= 300: - col = color.Cyan - } - - log.Printf("%s %s %s %s", m, p, col(n), end.Sub(start)) - return nil - } -} diff --git a/website/docs/index.md b/website/docs/index.md index a3e1826d..d0fbdbc0 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -52,6 +52,7 @@ import ( "net/http" "github.com/labstack/echo" + mw "github.com/labstack/echo/middleware" ) // Handler @@ -63,7 +64,7 @@ func main() { e := echo.New() // Middleware - e.Use(echo.Logger) + e.Use(mw.Logger) // Routes e.Get("/", hello) @@ -86,7 +87,6 @@ func main() { - Delete user ```curl -X DELETE http://localhost:4444/users/1``` - ```go package main @@ -95,6 +95,7 @@ import ( "strconv" "github.com/labstack/echo" + mw "github.com/labstack/echo/middleware" ) type ( @@ -150,7 +151,7 @@ func main() { e := echo.New() // Middleware - e.Use(echo.Logger) + e.Use(mw.Logger) // Routes e.Post("/users", createUser)