how to add middleware

56 views
Skip to first unread message

Jason

unread,
Jun 17, 2011, 11:00:55 AM6/17/11
to pylons-...@googlegroups.com
How do I add custom middleware to Pyramid. I have a function add_auth(app) in myapp.lib.auth and I have tried setting my [pipline:main] section to:
[pipeline:main]
pipeline =
    egg:WebError#evalerror
    egg:myapp.lib.auth.add_auth
    sales

I also tried egg:myapp.lib.auth:add_auth, egg:myapp.lib.auth#add_auth. I have also tried the same making add_auth a class that accepts the app argument to __init__. I tried those combinations without the egg: as well. Nothing seems to work to add custom middleware. I always get distribution not found error. 

Is there any documentation on how to add custom middleware?

I did a search on google for "pipeline site:docs.pylonsproject.org" but none of the pages show how to add custom middleware to an application.

Thomas G. Willis

unread,
Jun 17, 2011, 12:17:58 PM6/17/11
to pylons-...@googlegroups.com
If you want to do it through the ini file, then you should read up on PasteDeploy. 



Jason

unread,
Jun 17, 2011, 1:00:51 PM6/17/11
to pylons-...@googlegroups.com
PasteDeploy's documentation helped, but it was still a little confusing. To add a custom middleware you have to write it like:
def auth_filter_factory(global_conf):
    def add_auth(app):
        # make wsgi app
        return app
    return add_auth


and then in the INI file:
[pipeline:main]
pipeline = egg:WebError#evalerror auth sales

[filter:auth]
paste.filter_factory = sales.lib.auth:auth_filter_factory

Any additional keys under [filter:auth] will be sent to auth_filter_factory as keyword arguments

--
Jason
Reply all
Reply to author
Forward
0 new messages