Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This handler supports the following frameworks at the moment:
* `martini`_
* `gocraft/web <https://github.com/gocraft/web>`_
* `Gin <https://github.com/gin-gonic/gin>`_
* `echo <https://github.com/labstack/echo>`_
* `Goji <https://github.com/zenazn/goji>`_
* `Beego <https://github.com/astaxie/beego>`_
* `HTTPRouter <https://github.com/julienschmidt/httprouter>`_
Expand Down Expand Up @@ -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
.......

Expand Down
33 changes: 33 additions & 0 deletions examples/echo/server.go
Original file line number Diff line number Diff line change
@@ -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")
}