iris-contrib / middleware

Community Middleware List for the Iris Web Framework.

Home Page:https://github.com/kataras/iris

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example CORS doesn't work (experimental-handlers/cors/simple)

corebreaker opened this issue · comments

That's follows what i did on Fallback handlers, i noticed a malfunction.

The cause of problem is not in example, so i didn't posted issue in Iris Project or in Examples (iris-contrib/examples).

Fallback handlers in a Party is not called. Only Fallback handlers in Application scope are called.
Therefore, in CORS example which use CORS middleware, the Fallback handler defined in CORS middleware is not called.
The fallback handler is created in file iris-contris/middleware/cors/cors.go at line 22 and is set in Party in the same file at line 71 (in function NewPartyMiddleware called in CORS example).

The router, when no route is found, call the fallback handlers defined and registered in Application data structure (via Fallback stack), and even if the fallback stack is inherited in Party structure (APIBuilder), this stay in Application scope.

The fallback handler is pushed in fallback stack that is in Party, but the router don't use it cause the fallback stack passed to the router (routerHandler type ou RequestHandler) is those which is in Application, not the Party, via RoutesProvider interface instance (the app object but indeed APIBuilder in application).

Wow, I've just encountered the problem today. Nice catch man. 👍

Thank you.
We have just to wait for the merges.
If you can't wait, and if you can do that, you can add the CORS middleware in Application scope like that:

app := iris.New()

crs := cors.NewAppMiddleware(cors.Options{
	/* CORS Options */
})

app.Configure(crs)

The middleware is put to Application instead of Party. That should work.

Check the above commits and verify that the issue was solved with simply .Use, all "fallback handlers" and NewPartyMiddleware and things like that removed (revert the changes maden by @ZaniaDeveloper for a solution on that problem, unfortunately it didn't solve it, see : kataras/iris#922 )