Search code examples
pythongoogle-app-engineroutessubdomainwebapp2

Using webapp2's DomainRoute on google app engine


I'm trying to use webapp2's DomainRoute to route requess to specific users. The definition of the routes looks like this:

app = webapp2.WSGIApplication([
                                DomainRoute("<subdomain>." + os.environ["HTTP_HOST"], [
                                    webapp2.Route('/',ClientHandler)]),
                            ('/', MainHandler)],
                            debug=True)

the handlers all exist, and currently, my ClientHandler should just spit out the current subdomain but when I currently go to nosub.localhost:8090 it doesn't even reach the server. Do I need to edit my hosts file? And if so, is it valid to add a wildcart like *.localhost so any subdomain will work?


Solution

  • Yes, you need to edit your hosts file - whatever.localhost does not automatically resolve to 127.0.0.1. Alternately, save yourself some time and use xip.io.

    Your code has a significant problem, though: you're using os.environ["HTTP_HOST"] in a context that only gets run on the first request. This means that you extract the hostname from the first request to your app and use that as a base name for it and all subsequent requests - which is pretty definitely not what you want. For instance, if the first user to your app instance comes from subdomain.myapp.com, you'll set up a route for subdomain.subdomain.myapp.com.