FromStd converts native http.Handler & http.HandlerFunc to context.Handler. Supported form types: .FromStd(h http.Handler) .FromStd(func(w http.ResponseWriter, r *http.Request)) .FromStd(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc))
(handler interface{})
| 15 | // .FromStd(func(w http.ResponseWriter, r *http.Request)) |
| 16 | // .FromStd(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)) |
| 17 | func FromStd(handler interface{}) context.Handler { |
| 18 | switch h := handler.(type) { |
| 19 | case context.Handler: |
| 20 | return h |
| 21 | // case func(*context.Context): |
| 22 | // return h |
| 23 | case http.Handler: |
| 24 | // handlerFunc.ServeHTTP(w,r) |
| 25 | return func(ctx *context.Context) { |
| 26 | h.ServeHTTP(ctx.ResponseWriter(), ctx.Request()) |
| 27 | } |
| 28 | case func(http.ResponseWriter, *http.Request): |
| 29 | // handlerFunc(w,r) |
| 30 | return FromStd(http.HandlerFunc(h)) |
| 31 | case func(http.ResponseWriter, *http.Request, http.HandlerFunc): |
| 32 | // handlerFunc(w,r, http.HandlerFunc) |
| 33 | // |
| 34 | return FromStdWithNext(h) |
| 35 | case func(http.Handler) http.Handler: |
| 36 | panic(fmt.Errorf(` |
| 37 | Passed handler cannot be converted directly: |
| 38 | - http.Handler(http.Handler) |
| 39 | --------------------------------------------------------------------- |
| 40 | Please use the Application.WrapRouter method instead, example code: |
| 41 | app := iris.New() |
| 42 | // ... |
| 43 | app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) { |
| 44 | httpThirdPartyHandler(router).ServeHTTP(w, r) |
| 45 | })`)) |
| 46 | default: |
| 47 | // No valid handler passed |
| 48 | panic(fmt.Errorf(` |
| 49 | Passed argument is not a func(iris.Context) neither one of these types: |
| 50 | - http.Handler |
| 51 | - func(w http.ResponseWriter, r *http.Request) |
| 52 | - func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) |
| 53 | --------------------------------------------------------------------- |
| 54 | It seems to be a %T points to: %v`, handler, handler)) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // FromStdWithNext receives a standar handler - middleware form - and returns a |
| 59 | // compatible context.Handler wrapper. |
searching dependent graphs…