mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
Middleware example
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
3b3137b31e
commit
40330ae7c0
@ -8,7 +8,7 @@ Bolt is a fast HTTP router (zero memory allocation) + micro web framework in Go.
|
||||
|
||||
### Benchmark
|
||||
Based on [julienschmidt/go-http-routing-benchmark] (https://github.com/vishr/go-http-routing-benchmark)
|
||||
#### [GitHub](http://developer.github.com/v3)
|
||||
##### [GitHub API](http://developer.github.com/v3)
|
||||
```
|
||||
BenchmarkAce_GithubAll 20000 70743 ns/op 13792 B/op 167 allocs/op
|
||||
BenchmarkBear_GithubAll 10000 251638 ns/op 79952 B/op 943 allocs/op
|
||||
|
@ -60,7 +60,6 @@ func (c *Context) JSON(n int, i interface{}) {
|
||||
}
|
||||
|
||||
func (c *Context) File(n int, file, name string) {
|
||||
|
||||
}
|
||||
|
||||
// Next executes the next handler in the chain.
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/bolt"
|
||||
mw "github.com/labstack/bolt/middleware"
|
||||
)
|
||||
|
||||
type user struct {
|
||||
@ -40,6 +41,7 @@ func getUser(c *bolt.Context) {
|
||||
|
||||
func main() {
|
||||
b := bolt.New()
|
||||
b.Use(mw.Logger())
|
||||
b.Index("public/index.html")
|
||||
b.Static("/js", "public/js")
|
||||
b.Post("/users", createUser)
|
||||
|
@ -13,20 +13,20 @@ func Logger() bolt.HandlerFunc {
|
||||
start := time.Now()
|
||||
c.Next()
|
||||
end := time.Now()
|
||||
co := color.Green
|
||||
col := color.Green
|
||||
m := c.Request.Method
|
||||
p := c.Request.URL.Path
|
||||
s := c.Response.Status()
|
||||
|
||||
switch {
|
||||
case s >= 500:
|
||||
co = color.Red
|
||||
col = color.Red
|
||||
case s >= 400:
|
||||
co = color.Yellow
|
||||
col = color.Yellow
|
||||
case s >= 300:
|
||||
co = color.Cyan
|
||||
col = color.Cyan
|
||||
}
|
||||
|
||||
log.Printf("%s %s %s %s", m, p, co(s), end.Sub(start))
|
||||
log.Printf("%s %s %s %s", m, p, col(s), end.Sub(start))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user