mirror of
https://github.com/kataras/iris.git
synced 2025-03-21 23:16:25 +01:00
add example for simple http.Handler wrapper
This commit is contained in:
parent
ef2643b046
commit
9dc356c1d8
|
@ -13,7 +13,7 @@
|
|||
|
||||
# Iris Web Framework <a href="README_GR.md"><img width="20px" src="https://iris-go.com/images/flag-greece.svg" /></a> <a href="README_FR.md"><img width="20px" src="https://iris-go.com/images/flag-france.svg" /></a> <a href="README_ZH.md"><img width="20px" src="https://iris-go.com/images/flag-china.svg" /></a> <a href="README_ES.md"><img width="20px" src="https://iris-go.com/images/flag-spain.png" /></a> <a href="README_FA.md"><img width="20px" src="https://iris-go.com/images/flag-iran.svg" /></a> <a href="README_RU.md"><img width="20px" src="https://iris-go.com/images/flag-russia.svg" /></a> <a href="README_KO.md"><img width="20px" src="https://iris-go.com/images/flag-south-korea.svg?v=12" /></a>
|
||||
|
||||
[](https://github.com/kataras/iris/actions) [](https://github.com/kataras/iris/tree/master/_examples) [](https://gitter.im/iris_go/community) <!--[](https://app.fossa.io/projects/git%2Bgithub.com%2Fkataras%2Firis?ref=badge_shield)--> [](https://iris-go.com/donate) <!--[](https://goreportcard.com/report/github.com/kataras/iris)--><!--[](https://pkg.go.dev/github.com/kataras/iris/v12@v12.2.0)--> <!-- [](https://github.com/kataras/iris/releases) -->
|
||||
[](https://github.com/kataras/iris/actions) [](https://github.com/kataras/iris/tree/master/_examples) [](https://gitter.im/iris_go/community) <!--[](https://app.fossa.io/projects/git%2Bgithub.com%2Fkataras%2Firis?ref=badge_shield)--> [](https://iris-go.com/donate) <!--[](https://goreportcard.com/report/github.com/kataras/iris)--><!--[](https://pkg.go.dev/github.com/kataras/iris/v12@v12.2.0)--> <!-- [](https://github.com/kataras/iris/releases) -->
|
||||
|
||||
<!-- <a href="https://iris-go.com"> <img align="right" src="https://iris-go.com/images/logo-w169.png"></a> -->
|
||||
|
||||
|
|
|
@ -70,7 +70,8 @@
|
|||
* Convert net/http Handlers
|
||||
* [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)
|
||||
* [From func(http.Handler) http.Handler](convert-handlers/wrapper/main.go)
|
||||
* [Convert by your own: sentry/raven middleware](convert-handlers/real-usecase-raven/writing-middleware/main.go)
|
||||
* [Rewrite Middleware](routing/rewrite/main.go)
|
||||
* [Route State](routing/route-state/main.go)
|
||||
* [Remove Route](routing/remove-route/main.go)
|
||||
|
|
50
_examples/convert-handlers/nethttp/wrapper/main.go
Normal file
50
_examples/convert-handlers/nethttp/wrapper/main.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
httpThirdPartyWrapper := StandardWrapper(Options{
|
||||
Message: "test_value",
|
||||
})
|
||||
|
||||
// This case
|
||||
app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
|
||||
httpThirdPartyWrapper(router).ServeHTTP(w, r)
|
||||
// If was func(http.HandlerFunc) http.HandlerFunc:
|
||||
// httpThirdPartyWrapper(router.ServeHTTP).ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
app.Get("/", index)
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func index(ctx iris.Context) {
|
||||
ctx.Writef("Message: %s\n", ctx.Value(msgContextKey))
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
type contextKey uint8
|
||||
|
||||
var (
|
||||
msgContextKey contextKey = 1
|
||||
)
|
||||
|
||||
func StandardWrapper(opts Options) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// ...
|
||||
req := r.WithContext(context.WithValue(r.Context(), msgContextKey, opts.Message))
|
||||
next.ServeHTTP(w, req)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -32,6 +32,17 @@ func FromStd(handler interface{}) context.Handler {
|
|||
// handlerFunc(w,r, http.HandlerFunc)
|
||||
//
|
||||
return FromStdWithNext(h)
|
||||
case func(http.Handler) http.Handler:
|
||||
panic(fmt.Errorf(`
|
||||
Passed handler cannot be converted directly:
|
||||
- http.Handler(http.Handler)
|
||||
---------------------------------------------------------------------
|
||||
Please use the Application.WrapRouter method instead, example code:
|
||||
app := iris.New()
|
||||
// ...
|
||||
app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
|
||||
httpThirdPartyHandler(router).ServeHTTP(w, r)
|
||||
})`))
|
||||
default:
|
||||
// No valid handler passed
|
||||
panic(fmt.Errorf(`
|
||||
|
|
Loading…
Reference in New Issue
Block a user