Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use WSGI middleware?

Tags:

python

wsgi

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:

  • leaving as is; with routing, path param extraction and environ manipulation
  • make the router a WSGI application and the current WSGI applications framework specific handlers
  • 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
like image 926
deamon Avatar asked Sep 05 '10 13:09

deamon


1 Answers

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)

like image 147
Ian Bicking Avatar answered Sep 24 '22 14:09

Ian Bicking