Tatsuhiko Miyagawa's blog to discuss mostly tech and nerdy stuff.

2009.09.18

Plack middleware

Middleware is some great (but sometimes abused) features in Python's WSGI and Ruby's Rack. HTTP::Engine also had this middleware concept and had some great "plugins". So it's time to write the new middleware framework for PSGI and Plack.

In PSGI, middleware is just a PSGI application thatdoes the both application and server side. It takes the $env hash like application does and then runs the original application like a server does, and then return the response. Middleware can do pre-processing on the $env hash or do post-processing on $env or $res. Example middleware would be doing some X-Sendfile work, normalizing proxy IP address using X-Forwarded-For, map static files to /static, catch the runtime error and display a beautiful stack trace or profile the runtime requests using profilers like NYTProf.