I write a router that takes the path of a request, match it against a regex and calls a WSGI handler, if the regex matches. The dict with the matching capturing groups is added to the envrion. Is it bad style to modify the environ with WSGI middleware?
But is that what WSGI middleware was invented for? I've just read WSGI Middleware Considered Harmful and wonder whether I should rewrite my router to be no longer a middleware. An application becomes dependend on my middleware, if it uses the dict with capturing groups. On the other hand no application has to use this additional dict. I could also forego the path param extraction and reduce the router to the routing, but then each application has to rerun the regex a second time for path parameter extraction.
So what to do:
environ
manipulationIf you add things to the environ and then use those things in applications, without any fallbacks, then you have to some degree bound the application to the middleware.
In this particular case there is a convention for how to add those captured values to the environ: wsgiorg.routing_args. So while you would be putting references to this capturing into your application, it's not an entirely ad hoc communication.
(Though you can certainly overuse middleware, I consider that particular article to overstate the case; middleware can be a good abstraction to consider, implement, and test different pieces of an application separately, even if initially those pieces are implemented for a singular goal by a single person)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With