diff --git a/README.rst b/README.rst index c6cb29d..0a18a90 100644 --- a/README.rst +++ b/README.rst @@ -20,6 +20,7 @@ This handler supports the following frameworks at the moment: * `martini`_ * `gocraft/web `_ * `Gin `_ +* `echo `_ * `Goji `_ * `Beego `_ * `HTTPRouter `_ @@ -112,6 +113,49 @@ a simple middleware in ``server.go``: n.Run(":3000") } +echo +....... + +If you are using echo_ you can implement the handler as +a simple middleware in ``server.go``: + +.. code-block:: go + + package main + + import ( + "github.com/labstack/echo/v4" + "github.com/thoas/stats" + "net/http" + ) + + // Stats provides response time, status code count, etc. + var Stats = stats.New() + + func main() { + r := echo.New() + + r.Use(func(next echo.HandlerFunc) echo.HandlerFunc { + return func(ctx echo.Context) error { + beginning, recorder := Stats.Begin(ctx.Response().Writer) + err := next(ctx) + Stats.End(beginning, stats.WithRecorder(recorder)) + return err + } + }) + + r.GET("/stats", func(ctx echo.Context) error { + return ctx.JSON(http.StatusOK, Stats.Data()) + }) + + r.GET("/", func(ctx echo.Context) error { + return ctx.JSON(http.StatusOK, map[string]string{"hello": "world"}) + }) + + r.Start("0.0.0.0:8080") + } + + HTTPRouter ....... diff --git a/examples/echo/server.go b/examples/echo/server.go new file mode 100644 index 0000000..7195dee --- /dev/null +++ b/examples/echo/server.go @@ -0,0 +1,33 @@ +package main + +import ( + "github.com/labstack/echo/v4" + "github.com/thoas/stats" + "net/http" +) + +// Stats provides response time, status code count, etc. +var Stats = stats.New() + +func main() { + r := echo.New() + + r.Use(func(next echo.HandlerFunc) echo.HandlerFunc { + return func(ctx echo.Context) error { + beginning, recorder := Stats.Begin(ctx.Response().Writer) + err := next(ctx) + Stats.End(beginning, stats.WithRecorder(recorder)) + return err + } + }) + + r.GET("/stats", func(ctx echo.Context) error { + return ctx.JSON(http.StatusOK, Stats.Data()) + }) + + r.GET("/", func(ctx echo.Context) error { + return ctx.JSON(http.StatusOK, map[string]string{"hello": "world"}) + }) + + r.Start("0.0.0.0:8080") +}