Add an example on how to use third-party handlers that are not compatible to the "next pattern"

Former-commit-id: 39a57bd97542142cb61eac9b0ee9b40df2334019
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-10-13 06:59:31 +03:00
parent a9ac0aee2f
commit 21dc2cfbd8
6 changed files with 108 additions and 5 deletions

View File

@ -234,6 +234,7 @@ convert any custom type into a response dispatcher by implementing the `mvc.Resu
- [From func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)](convert-handlers/negroni-like/main.go)
- [From http.Handler or http.HandlerFunc](convert-handlers/nethttp/main.go)
- [From func(http.HandlerFunc) http.HandlerFunc](convert-handlers/real-usecase-raven/writing-middleware/main.go)
### View

View File

@ -8,8 +8,8 @@ import (
func main() {
app := iris.New()
ionMiddleware := iris.FromStd(negronilikeTestMiddleware)
app.Use(ionMiddleware)
irisMiddleware := iris.FromStd(negronilikeTestMiddleware)
app.Use(irisMiddleware)
// Method GET: http://localhost:8080/
app.Get("/", func(ctx iris.Context) {

View File

@ -8,8 +8,8 @@ import (
func main() {
app := iris.New()
ionMiddleware := iris.FromStd(nativeTestMiddleware)
app.Use(ionMiddleware)
irisMiddleware := iris.FromStd(nativeTestMiddleware)
app.Use(irisMiddleware)
// Method GET: http://localhost:8080/
app.Get("/", func(ctx iris.Context) {

View File

@ -0,0 +1,45 @@
package main
import (
"errors"
"fmt"
"net/http"
"runtime/debug"
"github.com/kataras/iris"
"github.com/getsentry/raven-go"
)
// https://docs.sentry.io/clients/go/integrations/http/
func init() {
raven.SetDSN("https://<key>:<secret>@sentry.io/<project>")
}
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.Writef("Hi")
})
// Example for WrapRouter is already here:
// https://github.com/kataras/iris/blob/master/_examples/routing/custom-wrapper/main.go#L53
app.WrapRouter(func(w http.ResponseWriter, r *http.Request, irisRouter http.HandlerFunc) {
// Exactly the same source code:
// https://github.com/getsentry/raven-go/blob/379f8d0a68ca237cf8893a1cdfd4f574125e2c51/http.go#L70
defer func() {
if rval := recover(); rval != nil {
debug.PrintStack()
rvalStr := fmt.Sprint(rval)
packet := raven.NewPacket(rvalStr, raven.NewException(errors.New(rvalStr), raven.NewStacktrace(2, 3, nil)), raven.NewHttp(r))
raven.Capture(packet, nil)
w.WriteHeader(http.StatusInternalServerError)
}
}()
irisRouter(w, r)
})
app.Run(iris.Addr(":8080"))
}

View File

@ -0,0 +1,57 @@
package main
import (
"errors"
"fmt"
"runtime/debug"
"github.com/kataras/iris"
"github.com/getsentry/raven-go"
)
// At this example you will see how to convert any net/http middleware
// that has the form of `(HandlerFunc) HandlerFunc`.
// If the `raven.RecoveryHandler` had the form of
// `(http.HandlerFunc)` or `(http.HandlerFunc, next http.HandlerFunc)`
// you could just use the `irisMiddleware := iris.FromStd(nativeHandler)`
// but it doesn't, however as you already know Iris can work with net/http directly
// because of the `ctx.ResponseWriter()` and `ctx.Request()` are the original
// http.ResponseWriter and *http.Request.
// (this one is a big advantage, as a result you can use Iris for ever :)).
//
// The source code of the native middleware does not change at all.
// https://github.com/getsentry/raven-go/blob/379f8d0a68ca237cf8893a1cdfd4f574125e2c51/http.go#L70
// The only addition is the Line 18 and Line 39 (instead of handler(w,r))
// and you have a new iris middleware ready to use!
func irisRavenMiddleware(ctx iris.Context) {
w, r := ctx.ResponseWriter(), ctx.Request()
defer func() {
if rval := recover(); rval != nil {
debug.PrintStack()
rvalStr := fmt.Sprint(rval)
packet := raven.NewPacket(rvalStr, raven.NewException(errors.New(rvalStr), raven.NewStacktrace(2, 3, nil)), raven.NewHttp(r))
raven.Capture(packet, nil)
w.WriteHeader(iris.StatusInternalServerError)
}
}()
ctx.Next()
}
// https://docs.sentry.io/clients/go/integrations/http/
func init() {
raven.SetDSN("https://<key>:<secret>@sentry.io/<project>")
}
func main() {
app := iris.New()
app.Use(irisRavenMiddleware)
app.Get("/", func(ctx iris.Context) {
ctx.Writef("Hi")
})
app.Run(iris.Addr(":8080"))
}

View File

@ -25,6 +25,7 @@ Most of the experimental handlers are ported to work with _iris_'s handler form,
| [new relic](https://github.com/iris-contrib/middleware/tree/master/newrelic) | Official [New Relic Go Agent](https://github.com/newrelic/go-agent). | [iris-contrib/middleware/newrelic/_example](https://github.com/iris-contrib/middleware/tree/master/newrelic/_example) |
| [prometheus](https://github.com/iris-contrib/middleware/tree/master/prometheus)| Easily create metrics endpoint for the [prometheus](http://prometheus.io) instrumentation tool | [iris-contrib/middleware/prometheus/_example](https://github.com/iris-contrib/middleware/tree/master/prometheus/_example) |
| [casbin](https://github.com/iris-contrib/middleware/tree/master/casbin)| An authorization library that supports access control models like ACL, RBAC, ABAC | [iris-contrib/middleware/casbin/_examples](https://github.com/iris-contrib/middleware/tree/master/casbin/_examples) |
| [raven](https://github.com/iris-contrib/middleware/tree/master/raven)| Sentry client in Go | [raven/_example](https://github.com/iris-contrib/middleware/blob/master/raven/_example/main.go) |
Third-Party Handlers
------------
@ -47,5 +48,4 @@ Here's a small list of useful third-party handlers:
| [xrequestid](https://github.com/pilu/xrequestid) | Middleware that assigns a random X-Request-Id header to each request |
| [digits](https://github.com/bamarni/digits) | Middleware that handles [Twitter Digits](https://get.digits.com/) authentication |
> Feel free to put up your own middleware in this list!