mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
fix https://github.com/kataras/iris/issues/1713 and add a simple usage example of the 'RemoveHandler'
This commit is contained in:
parent
197df1ef64
commit
f7757c0793
|
@ -48,6 +48,7 @@
|
||||||
* Middleware
|
* Middleware
|
||||||
* [Per Route](routing/writing-a-middleware/per-route/main.go)
|
* [Per Route](routing/writing-a-middleware/per-route/main.go)
|
||||||
* [Globally](routing/writing-a-middleware/globally/main.go)
|
* [Globally](routing/writing-a-middleware/globally/main.go)
|
||||||
|
* [Remove a Handler](routing/remove-handler/main.go)
|
||||||
* Share Values
|
* Share Values
|
||||||
* [Share Services](routing/writing-a-middleware/share-services/main.go)
|
* [Share Services](routing/writing-a-middleware/share-services/main.go)
|
||||||
* [Share Functions](routing/writing-a-middleware/share-funcs/main.go)
|
* [Share Functions](routing/writing-a-middleware/share-funcs/main.go)
|
||||||
|
|
29
_examples/routing/remove-handler/main.go
Normal file
29
_examples/routing/remove-handler/main.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/kataras/iris/v12"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := newApp()
|
||||||
|
app.Listen(":8080")
|
||||||
|
}
|
||||||
|
|
||||||
|
func newApp() *iris.Application {
|
||||||
|
app := iris.New()
|
||||||
|
|
||||||
|
api := app.Party("/api")
|
||||||
|
api.Use(myMiddleware)
|
||||||
|
users := api.Party("/users")
|
||||||
|
users.Get("/", usersIndex).RemoveHandler(myMiddleware)
|
||||||
|
// OR for all routes under a Party (or Application):
|
||||||
|
// users.RemoveHandler(...)
|
||||||
|
|
||||||
|
return app
|
||||||
|
}
|
||||||
|
|
||||||
|
func myMiddleware(ctx iris.Context) {
|
||||||
|
ctx.WriteString("Middleware\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func usersIndex(ctx iris.Context) {
|
||||||
|
ctx.WriteString("OK")
|
||||||
|
}
|
14
_examples/routing/remove-handler/main_test.go
Normal file
14
_examples/routing/remove-handler/main_test.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kataras/iris/v12/httptest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSimpleRouteRemoveHandler(t *testing.T) {
|
||||||
|
app := newApp()
|
||||||
|
e := httptest.New(t, app)
|
||||||
|
|
||||||
|
e.GET("/api/users").Expect().Status(httptest.StatusOK).Body().Equal("OK")
|
||||||
|
}
|
|
@ -2280,7 +2280,7 @@ type internalJSONDecoder interface {
|
||||||
|
|
||||||
func (cfg JSONReader) getDecoder(r io.Reader, globalShouldOptimize bool) (decoder internalJSONDecoder) {
|
func (cfg JSONReader) getDecoder(r io.Reader, globalShouldOptimize bool) (decoder internalJSONDecoder) {
|
||||||
if cfg.Optimize || globalShouldOptimize {
|
if cfg.Optimize || globalShouldOptimize {
|
||||||
decoder = jsoniter.NewDecoder(r)
|
decoder = jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(r)
|
||||||
} else {
|
} else {
|
||||||
decoder = json.NewDecoder(r)
|
decoder = json.NewDecoder(r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,40 +16,23 @@ import (
|
||||||
func FromStd(handler interface{}) context.Handler {
|
func FromStd(handler interface{}) context.Handler {
|
||||||
switch h := handler.(type) {
|
switch h := handler.(type) {
|
||||||
case context.Handler:
|
case context.Handler:
|
||||||
{
|
|
||||||
//
|
|
||||||
// it's already an Iris Handler
|
|
||||||
//
|
|
||||||
return h
|
return h
|
||||||
}
|
case func(*context.Context):
|
||||||
|
return h
|
||||||
case http.Handler:
|
case http.Handler:
|
||||||
{
|
|
||||||
//
|
|
||||||
// handlerFunc.ServeHTTP(w,r)
|
// handlerFunc.ServeHTTP(w,r)
|
||||||
//
|
|
||||||
return func(ctx *context.Context) {
|
return func(ctx *context.Context) {
|
||||||
h.ServeHTTP(ctx.ResponseWriter(), ctx.Request())
|
h.ServeHTTP(ctx.ResponseWriter(), ctx.Request())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
case func(http.ResponseWriter, *http.Request):
|
case func(http.ResponseWriter, *http.Request):
|
||||||
{
|
|
||||||
//
|
|
||||||
// handlerFunc(w,r)
|
// handlerFunc(w,r)
|
||||||
//
|
|
||||||
return FromStd(http.HandlerFunc(h))
|
return FromStd(http.HandlerFunc(h))
|
||||||
}
|
|
||||||
case func(http.ResponseWriter, *http.Request, http.HandlerFunc):
|
case func(http.ResponseWriter, *http.Request, http.HandlerFunc):
|
||||||
{
|
|
||||||
//
|
|
||||||
// handlerFunc(w,r, http.HandlerFunc)
|
// handlerFunc(w,r, http.HandlerFunc)
|
||||||
//
|
//
|
||||||
return FromStdWithNext(h)
|
return FromStdWithNext(h)
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
{
|
|
||||||
//
|
|
||||||
// No valid handler passed
|
// No valid handler passed
|
||||||
//
|
|
||||||
panic(fmt.Errorf(`
|
panic(fmt.Errorf(`
|
||||||
Passed argument is not a func(iris.Context) neither one of these types:
|
Passed argument is not a func(iris.Context) neither one of these types:
|
||||||
- http.Handler
|
- http.Handler
|
||||||
|
@ -59,7 +42,6 @@ func FromStd(handler interface{}) context.Handler {
|
||||||
It seems to be a %T points to: %v`, handler, handler))
|
It seems to be a %T points to: %v`, handler, handler))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// FromStdWithNext receives a standar handler - middleware form - and returns a
|
// FromStdWithNext receives a standar handler - middleware form - and returns a
|
||||||
// compatible context.Handler wrapper.
|
// compatible context.Handler wrapper.
|
||||||
|
|
|
@ -1210,7 +1210,7 @@ func (api *APIBuilder) RemoveHandler(namesOrHandlers ...interface{}) Party {
|
||||||
switch h := nameOrHandler.(type) {
|
switch h := nameOrHandler.(type) {
|
||||||
case string:
|
case string:
|
||||||
handlerName = h
|
handlerName = h
|
||||||
case context.Handler:
|
case context.Handler, func(*context.Context):
|
||||||
handlerName = context.HandlerName(h)
|
handlerName = context.HandlerName(h)
|
||||||
case *int:
|
case *int:
|
||||||
counter = h
|
counter = h
|
||||||
|
|
|
@ -159,7 +159,7 @@ func (r *Route) RemoveHandler(namesOrHandlers ...interface{}) (count int) {
|
||||||
switch h := nameOrHandler.(type) {
|
switch h := nameOrHandler.(type) {
|
||||||
case string:
|
case string:
|
||||||
handlerName = h
|
handlerName = h
|
||||||
case context.Handler:
|
case context.Handler, func(*context.Context):
|
||||||
handlerName = context.HandlerName(h)
|
handlerName = context.HandlerName(h)
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("remove handler: unexpected type of %T", h))
|
panic(fmt.Sprintf("remove handler: unexpected type of %T", h))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user