How do I test multiple domains setup in AppEngine locally?
-
12-11-2019 - |
Question
In the question Working with subdomain in google app engine, the following code was suggested.
applications = {
'product.example.com': webapp.WSGIApplication([
('/', IndexHandler),
('/(.*)', ProductHandler)]),
'user.example.com': webapp.WSGIApplication([
('/', IndexHandler),
('/(.*)', UserHandler)]),
}
def main():
run_wsgi_app(applications[os.environ['HTTP_HOST']])
if __name__ == '__main__':
main()
My question is how do I test this locally? When I'm testing it locally, the host is "localhost:8080" and not any of the domains.
Solution
Create two new entries in your Hosts file:
127.0.0.1 product.example.com
127.0.0.1 user.example.com
and run your local GAE application on default Http port 80.
If, for some reason, you can't run GAE on port 80, you could try to modify your application.py
to match the local port number with something like this:
if os.environ['SERVER_SOFTWARE'].startswith('Dev'):
PORT=':8080'
else:
PORT=''
applications = {
'product.example.com%s' % PORT: webapp.WSGIApplication([
('/', IndexHandler),
('/(.*)', ProductHandler)]),
'user.example.com%s' % PORT: webapp.WSGIApplication([
('/', IndexHandler),
('/(.*)', UserHandler)]),
}
Or even better modifying the main function like this (Thanks to @Nick's comment):
def main():
run_wsgi_app(applications[os.environ['HTTP_HOST'].split(':')[0]])
You should be ready to test your local application with the following addresses:
http://product.example.com:8080
http://user.example.com:8080
Remember to switch back your Hosts
file to be able to reach the Production server.