mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
Moved middleware in a package
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
2e5d09d225
commit
6abe709490
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user