views:

25

answers:

1

I'm trying to figure out how to change url routing based on namespace

Say I have myapp.x.com/apage and myapp.y.com/apage, I tried the code below but it doesn't work because I presume the main.py is being cached

ns = namespace_manager.google_apps_namespace()
if ns == 'x.com'
    app = WSGIApplication([
        (r'/apage', 'my.module.XHandler'),
    ])
else:
    app = WSGIApplication([
        (r'/apage', 'my.module.YHandler'),
    ])

Is there any other way to do it besides of course turning each handler into a url router?

+1  A: 

You need to write your own middleware that routes to different apps based on domain. See DomainMiddleware in this blog post for an example.

Nick Johnson
Thanks, actually after seeing the way you did it and realizing that it was possible, I moved the routing code into the body of the main() function and it works. I guess appengine does not cache main.py but caches the main() function between requests.
molicule
App Engine keeps loaded modules loaded between requests. It doesn't re-execute the body of the module if your module has a main() function - instead, it just executes main(). I'd recommend writing middleware instead, though - it's more versatile and less kludgy.
Nick Johnson