I have found a very handy function, google.appengine.api.modules.modules.get_hostname()
, which allows my Python app to retrieve its own hostname, when running on Google App Engine.
I have constructed a helper function in my app, that constructs absolute URLs with the help of this function. These URLs are really useful in testing, as they allow me to have things like OAuth flows return to the right place.
import six
from google.appengine.api.modules.modules import get_hostname
INSECURE_HOST_WHITELIST = ('localhost',)
def get_redirect_url(path, secure=None):
assert isinstance(path, six.string_types), "The path argument must be a string."
if path:
path = '/' + path.lstrip('/')
hostname = get_hostname()
port = None
try:
hostname, port = hostname.split(':', 1)
except ValueError:
pass
if secure is None:
if hostname.lower() in INSECURE_HOST_WHITELIST:
secure = False
else:
secure = True
schema = 'http' + ('s' if secure else '')
return schema + '://' + hostname + (':' + port if port is not None else '') + path
In production, however, this function is much less useful, because if my users are accessing my app via something like https://www.myawesomeapp.com/, I don't want the OAuth flow to return them to something like https://v150-dot-my-awesome-app.appspot.com/
My question is: is there any way for my helper function to discover whether it is running off of the promoted module (i.e. the module receiving the traffic), and return a canonical server hostname accordingly?