Sam
Sam

Reputation: 6250

How to adding middleware to Appengine's webapp framework?

I'm using the appengine webapp framework (link). Is it possible to add Django middleware? I can't find any examples. I'm currently trying to get the FirePython middleware to work (link).

Upvotes: 3

Views: 3342

Answers (3)

zgoda
zgoda

Reputation: 12895

"Middleware" as understood by Django is a kind of request/response processor, quite different from what WSGI calls "middleware". Think: django-like middleware will add session attribute to request object basing on what Beaker (WSGI middleware) has put in environ['beaker.session']. While adding WSGI middleware to the stack should be straightforward (you already work on WSGI level in your main.py), adding request/response processor depends on how request and response are abstracted from WSGI.

How this can be done using Werkzeug (which is basic WSGI toolset) is described in Werkzeug's wiki and in one of its contrib modules.

Upvotes: 0

dar
dar

Reputation: 6530

The GAE webapp framework does not map one to one to the Django framework. It would be hard to do what you want without implementing some kind of adapter yourself, I do not know of any third party handler adapters to do this.

That said, I generally use the app-engine-patch so I can use the latest 1.0.2 Django release with AppEngine, and then you can just include the Django middleware the normal way with the setup.py file. If you needed to, you could probably look through the app-engine-patch's adapter to see how they do it, and start with that as a framework.

Upvotes: 0

Nick Johnson
Nick Johnson

Reputation: 101149

It's easy: You create the WSGI application as per normal, then wrap that application in your WSGI middleware before executing it.

See this code from Bloog to see how firepython is added as middleware.

Upvotes: 6

Related Questions