diff --git a/_examples/README.md b/_examples/README.md index b0058caa..09912183 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -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 diff --git a/_examples/convert-handlers/negroni-like/main.go b/_examples/convert-handlers/negroni-like/main.go index 4f65f46c..6de768ff 100644 --- a/_examples/convert-handlers/negroni-like/main.go +++ b/_examples/convert-handlers/negroni-like/main.go @@ -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) { diff --git a/_examples/convert-handlers/nethttp/main.go b/_examples/convert-handlers/nethttp/main.go index 641939a5..49fffabf 100644 --- a/_examples/convert-handlers/nethttp/main.go +++ b/_examples/convert-handlers/nethttp/main.go @@ -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) { diff --git a/_examples/convert-handlers/real-usecase-raven/wrapping-the-router/main.go b/_examples/convert-handlers/real-usecase-raven/wrapping-the-router/main.go new file mode 100644 index 00000000..1155dc1d --- /dev/null +++ b/_examples/convert-handlers/real-usecase-raven/wrapping-the-router/main.go @@ -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://:@sentry.io/") +} + +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")) +} diff --git a/_examples/convert-handlers/real-usecase-raven/writing-middleware/main.go b/_examples/convert-handlers/real-usecase-raven/writing-middleware/main.go new file mode 100644 index 00000000..5360c816 --- /dev/null +++ b/_examples/convert-handlers/real-usecase-raven/writing-middleware/main.go @@ -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://:@sentry.io/") +} + +func main() { + app := iris.New() + app.Use(irisRavenMiddleware) + + app.Get("/", func(ctx iris.Context) { + ctx.Writef("Hi") + }) + + app.Run(iris.Addr(":8080")) +} diff --git a/middleware/README.md b/middleware/README.md index 1dab4cc6..44e54423 100644 --- a/middleware/README.md +++ b/middleware/README.md @@ -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! \ No newline at end of file