From 40330ae7c003af926dbfad5244516650571223a5 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Thu, 12 Mar 2015 20:50:33 -0700 Subject: [PATCH] Middleware example Signed-off-by: Vishal Rana --- README.md | 2 +- context.go | 1 - example/main.go | 2 ++ middleware/logger.go | 10 +++++----- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 45a87217..476f50ee 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/context.go b/context.go index 5d570a35..90f13a54 100644 --- a/context.go +++ b/context.go @@ -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. diff --git a/example/main.go b/example/main.go index 70aec9e1..d10553b1 100644 --- a/example/main.go +++ b/example/main.go @@ -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) diff --git a/middleware/logger.go b/middleware/logger.go index 3b14e925..b114f2c4 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -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)) } }