1

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?

BillyBBone
  • 3,144
  • 3
  • 23
  • 27
  • what does get_hostname() return when running on google app engine? Does it always return the version, or if it's the main version, does it not just return .appspott.com? If so, a quick easy way to do this would be to search your string for "dot". If it has "dot" in it, then you know its not the main version. If matches your URL pattern of www..com, then you wouldn't even need to hardcode the URL. (I'm posting this as a comment and not an answer since I do not know what get_hostname() returns. If it always returns the version, just ignore this comment). – KevinG Jun 23 '17 at 00:04
  • LOL, I just clicked your link https://v150-dot-my-awesome-app.appspot.com/, and as of 6/22/2017, your "project" is black and blue. (Haha, its just a photo of that famous black/blue white/gold dress, that is DEFINITELY black and blue.) – KevinG Jun 23 '17 at 00:07
  • Maybe the example in this answer can help: https://stackoverflow.com/questions/31141247/resolve-discovery-path-on-app-engine-module/31145647#31145647 – Dan Cornilescu Jun 23 '17 at 05:42

0 Answers0