add example for simple http.Handler wrapper

This commit is contained in:
kataras 2022-06-19 10:01:18 +03:00
parent ef2643b046
commit 9dc356c1d8
No known key found for this signature in database
GPG Key ID: 403EEB7885C79503
4 changed files with 64 additions and 2 deletions

View File

@ -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> # 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>
[![build status](https://img.shields.io/github/workflow/status/kataras/iris/CI/master?style=for-the-badge)](https://github.com/kataras/iris/actions) [![view examples](https://img.shields.io/badge/examples%20-269-a83adf.svg?style=for-the-badge&logo=go)](https://github.com/kataras/iris/tree/master/_examples) [![chat](https://img.shields.io/gitter/room/iris_go/community.svg?color=cc2b5e&logo=gitter&style=for-the-badge)](https://gitter.im/iris_go/community) <!--[![FOSSA Status](https://img.shields.io/badge/LICENSE%20SCAN-PASSING❤-CD2956?style=for-the-badge&logo=fossa)](https://app.fossa.io/projects/git%2Bgithub.com%2Fkataras%2Firis?ref=badge_shield)--> [![donate](https://img.shields.io/badge/support-Iris-blue.svg?style=for-the-badge&logo=paypal)](https://iris-go.com/donate) <!--[![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/iris)--><!--[![godocs](https://img.shields.io/badge/go-%20docs-488AC7.svg?style=for-the-badge)](https://pkg.go.dev/github.com/kataras/iris/v12@v12.2.0)--> <!-- [![release](https://img.shields.io/badge/release%20-v12.0-0077b3.svg?style=for-the-badge)](https://github.com/kataras/iris/releases) --> [![build status](https://img.shields.io/github/workflow/status/kataras/iris/CI/master?style=for-the-badge)](https://github.com/kataras/iris/actions) [![view examples](https://img.shields.io/badge/examples%20-270-a83adf.svg?style=for-the-badge&logo=go)](https://github.com/kataras/iris/tree/master/_examples) [![chat](https://img.shields.io/gitter/room/iris_go/community.svg?color=cc2b5e&logo=gitter&style=for-the-badge)](https://gitter.im/iris_go/community) <!--[![FOSSA Status](https://img.shields.io/badge/LICENSE%20SCAN-PASSING❤-CD2956?style=for-the-badge&logo=fossa)](https://app.fossa.io/projects/git%2Bgithub.com%2Fkataras%2Firis?ref=badge_shield)--> [![donate](https://img.shields.io/badge/support-Iris-blue.svg?style=for-the-badge&logo=paypal)](https://iris-go.com/donate) <!--[![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/iris)--><!--[![godocs](https://img.shields.io/badge/go-%20docs-488AC7.svg?style=for-the-badge)](https://pkg.go.dev/github.com/kataras/iris/v12@v12.2.0)--> <!-- [![release](https://img.shields.io/badge/release%20-v12.0-0077b3.svg?style=for-the-badge)](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> --> <!-- <a href="https://iris-go.com"> <img align="right" src="https://iris-go.com/images/logo-w169.png"></a> -->

View File

@ -70,7 +70,8 @@
* Convert net/http Handlers * Convert net/http Handlers
* [From func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)](convert-handlers/negroni-like/main.go) * [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 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) * [Rewrite Middleware](routing/rewrite/main.go)
* [Route State](routing/route-state/main.go) * [Route State](routing/route-state/main.go)
* [Remove Route](routing/remove-route/main.go) * [Remove Route](routing/remove-route/main.go)

View 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)
})
}
}

View File

@ -32,6 +32,17 @@ func FromStd(handler interface{}) context.Handler {
// handlerFunc(w,r, http.HandlerFunc) // handlerFunc(w,r, http.HandlerFunc)
// //
return FromStdWithNext(h) 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: default:
// No valid handler passed // No valid handler passed
panic(fmt.Errorf(` panic(fmt.Errorf(`