mirror of
https://github.com/kataras/iris.git
synced 2026-01-01 09:17:02 +00:00
add example for simple http.Handler wrapper
This commit is contained in:
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user