Middlewares are for all routes or not ?
I think that there is a behavior that can be improved.
First, i'd like to known if that behavior is wanted.
Consider the following code:
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
func main() {
app := iris.New()
app.Get("/path1.txt", func(ctx context.Context) {
ctx.Text("PATH1")
})
app.Use(func(ctx context.Context) {
ctx.Text("MIDDLEWARE\n")
ctx.Next()
})
app.Get("/path2.txt", func(ctx context.Context) {
ctx.Text("PATH2")
})
app.Run(iris.Addr(":8080"))
}
With curl http://localhost:8080/path2.txt ; echo, we have:
MIDDLEWARE
PATH2
Okay.
But, with curl http://localhost:8080/path1.txt ; echo, we have:
PATH1
I understand why. That's cause the middleware is defined after "/path1.txt" route. But even if it could be weird, i expected
MIDDLEWARE
PATH1
Was it wanted by you? I can understand this way for configuring the server.
The reason why i expected
MIDDLEWARE
PATH1
is a question of design.
The configurators are powerful to configure the server cause thet can be considered as plugin for example by making: app.Configure(plugin1, plugin2, ...), we have the choice to add securities, redirections, authentications, etc. or not.
It can arrive that in a configurator we can add routes but these routes must have managements defined in another configurator, even if it appears after in the configurator list.
I know how to implements this way to use middleware. Even more, i can add easily a parameter somewhere to activate this behavior. So the developer have the choice to use or not and then we have the old behavior.
I can send a PR for that. Cause the solution is simple. Today, when we make a route, the handler list is copied in the new route, so every route has its own copy of handler list. Instead of doing a copy, we should have a reference to a common handler list in the route.
Hello, you use the wrong function: when we don't care about order and/or a middleware needed to be registered to all routes (incl parties and subdomains) then we have to use the UseGlobal instead of Use function. Give it a try and inform us, thank you.
Ah, ok it works, UseGlobal is the right function, thank you.
I knew it, but i thought that it have the same behavior, but i should test it before posting that issue.
It's true that with UseGlobal the "/path1.txt" route call the middleware but cause the prepend, the order is inversed:
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
func main() {
app := iris.New()
app.UseGlobal(func(ctx context.Context) {
ctx.Text("MIDDLEWARE 1\n")
ctx.Next()
})
app.UseGlobal(func(ctx context.Context) {
ctx.Text("MIDDLEWARE 2\n")
ctx.Next()
})
app.Get("/path.txt", func(ctx context.Context) {
ctx.Text("PATH")
})
app.Run(iris.Addr(":8080"))
}
With curl http://localhost:8080/path2.txt ; echo, we have:
MIDDLEWARE2
MIDDLEWARE1
PATH
We can conserve the order, i'll post a PR, you'll see if you want merge it.
@corebreaker thanks for your interest but it was an easy and quick fix, no need to wait more time for that. Could you please upgrade and confirm if that was the behavior you wanted to or not?
Great! It works! Now designing a server is really powerful. I like Django and Iris is now the best alternative in Go.