On Jun 4, 2:54 am, Gustavo Narea <[email protected]> wrote:
> Hi, Wyatt.
>
> Wyatt said:
>
> > I want to get access to the "root" WSGI middleware, the one that is
> > hit first when a request is made. In this example:
>
> >     [pipeline:main]
> >     pipeline = somefilter auth urlmap
>
> > ...I want access to `somefilter`. I couldn't find a reference to the
> > root app anywhere in the environ. Does anyone know how to access this?
>
> You can't, because it works like this:
> """
> class Middleware1(object):
>     def __init__(self, app):
>         self.app = app
>     def __call__(self, environ, start_response):
>         # ... do something ...
>         return self.app(environ, start_response)
>
> class Middleware2(object):
>     def __init__(self, app):
>         self.app = app
>     def __call__(self, environ, start_response):
>         # ... do something ...
>         return self.app(environ, start_response)
>
> class Middleware3(object):
>     foo = "some value"
>     def __init__(self, app):
>         self.app = app
>     def __call__(self, environ, start_response):
>         # ... do something ...
>         return self.app(environ, start_response)
>
> pylonsapp = make_app(...)
> pylonsapp = Middleware1(pylonsapp)
> pylonsapp = Middleware2(pylonsapp)
> pylonsapp = Middleware3(pylonsapp)
> """
>
> So in Middleware1, for example, you cannot access Middleware3 and vice versa.

Right. In your example, I want access to the `Middleware3` instance--
the WSGI app that is directly served by Paste (or your favorite WSGI
server, X). What I was thinking was that the server machinery could
place a reference to this "root" app in the WSGI environ. I haven't
really thought it through, so perhaps this is a bad idea.


> > For now, I created a wrapper app that saves a reference to itself in
> > the environ.
>
> If you really want it, the standard way to do it is to put in the environ
> whatever you need from that middleware, not the whole middleware.

Well, since it's just a reference, and the object it refers to lives
until the app server dies anyway, I don't think there should be any
issue with sticking it in the environ.

What I'm trying to *do* is a subrequest, based on the original
request:

    req = request.copy()
    # ...modify req.environ here, with a different PATH_INFO, say...
    response = req.get_response(request.environ['wsgi.root_app'])  #
root app gets __call__'ed here with new environ
    return json.loads(response.body)

Maybe our discussion is moot because there's a better way to do
subrequests? I might just need to RTFM a bit more. In the meantime, my
simple wrapper app seems to work fine.


> For example, if what you need is the "foo" argument of Middleware3 in
> Middleware1, you can use this:
> """
> class Middleware1(object):
>     def __init__(self, app):
>         self.app = app
>     def __call__(self, environ, start_response):
>         # ... do something ...
>         if "middleware3.foo" in environ:
>             # here it is!
>         return self.app(environ, start_response)
>
> class Middleware2(object):
>     def __init__(self, app):
>         self.app = app
>     def __call__(self, environ, start_response):
>         # ... do something ...
>         return self.app(environ, start_response)
>
> class Middleware3(object):
>     foo = "some value"
>     def __init__(self, app):
>         self.app = app
>     def __call__(self, environ, start_response):
>         # ... do something ...
>         environ["middleware3.foo"] = self.foo
>         return self.app(environ, start_response)
>
> pylonsapp = make_app(...)
> pylonsapp = Middleware1(pylonsapp)
> pylonsapp = Middleware2(pylonsapp)
> pylonsapp = Middleware3(pylonsapp)
> """
>
> Then this value will be available in any middleware* and the application
> itself.
>
> HTH,
>
> PS: Not exactly "any middleware"; it will be available for any middleware
> under it... So if we had a fourth middleware, it wouldn't be able to access
> environ["middleware3.foo"].
> --
> Gustavo Narea <xri://=Gustavo>.
> | Tech blog: =Gustavo/(+blog)/tech  ~  About me: =Gustavo/about |
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to