views:

42

answers:

1

I write a router that takes the path of a requests, 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:

  1. leaving as is; with routing, path param extraction and environ manipulation
  2. make the router a WSGI application and the current WSGI applications framework specific handlers
  3. reduce the router to routing and extract perform regex matching a second time for path parameter extraction in the application to which the request was routed
+2  A: 

If 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)

Ian Bicking