解决 App Engine 模块上的发现路径 [英] Resolve Discovery path on App Engine Module

查看:24
本文介绍了解决 App Engine 模块上的发现路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 python 为我的云端点构建一个客户端,如中所述文档.

I want to build a client for my cloud endpoints in python like described in the Documentation.

我想从托管 VM 构建 API,因此我通过调用获取 API 的路径

I want to build the api from a Managed VM, so i get the path to the API by calling

modules.get_hostname(module="default")

这适用于开发服务器,我可以创建发现端点的完整路径,但是在实时系统上,这会将 url 返回到某个版本,例如:

This works fine for the devserver and i can create the complete path to the discovery endpoint, however on the live system this returns the url to a certain version like:

20150628t110011.default.guestbook.appspot.com

因此 API(默认模块)的完整路径是

Thus the complete path to the API (default module) would be

https://20150628t110011.default.guestbook.appspot.com/_ah/api/discovery/v1/apis/guestbook/v1/rest?userIp=182.177.0.4"

但是没有发现文档,可能是因为证书不匹配那么长的url并且https失败.

But there is no discovery document, maybe due to the fact, that the certificate does not match a url that long and the https fails.

是否有正确的方法来接收默认模块的基本 url?像这样:

Is there a proper way to receive the base url to the default module? like so:

default.guestbook.appspot.com

因为这会导致一个有效的发现端点:

because that would result in a working discovery endpoint:

https://default.guestbook.appspot.com/_ah/api/discovery/v1/apis/guestbook/v1/rest?userIp=182.177.0.4"

我想避免在这里进行字符串操作,因为在本地开发服务器上这将不起作用,因为模块 url 解析为类似 localhost:1234 的内容.

I would like to avoid doing string operations here, because on the local devserver this would not work as the module url resolves to something like localhost:1234.

推荐答案

您可能想要浏览 GAE URl 路由文档:https://cloud.google.com/appengine/docs/python/modules/routing#routing_via_url

关键点:

  • Google 不会为双通配符域颁发 SSL 证书托管在 appspot.com,证书不适用于 https://20150628t110011.default.guestbook.appspot.com
  • 您可以使用 -dot- 分隔符使证书生效;特别是 default 版本的 default模块可以直接访问 guestbook.appspot.com
  • Google does not issue SSL certificates for double-wildcard domains hosted at appspot.com, the certificate won't work for https://20150628t110011.default.guestbook.appspot.com
  • You can get the certificate to work using the -dot- delimiters; in particular the default version of the default module can be accessed directly at guestbook.appspot.com

如果您的应用有多个模块并且映射到自定义域,问题就会变得更加复杂.

The problem gets even more complicated if your app has multiple modules and also if it's mapped to a custom domain.

在尝试解决此类复杂问题时,我意识到 modules.get_hostname() 现在不再能够执行其名称所暗示的原始功能(我猜是因为有多个可能的路径访问相同的实体).这可能解释了为什么他们不会尝试 修复 api 以返回一个正确的主机名:(请参阅此问答)

While trying to address such complications I realized that modules.get_hostname() is nowadays no longer able to perform the original function that its name implies (I guess because of the multiple possible paths for accessing the same entity). Which probably explains why they won't attempt to fix the api to return a proper hostname: (see this Q&A)

但是它可以返回的信息(视调用参数和执行上下文而定)是非常有用的,恕我直言,它允许以编程方式获取所有 3 个可能的应用程序使用上下文的正确主机名/URL:在开发服务器、.appspot.com 域和自定义域映射(包括基于主机名的映射)上:

But the info it can return (as applicable depending on the invocation arguments and the execution context) is IMHO extremely useful, allowing one to programatically obtain proper hostnames/URLs for all 3 possible app usage contextes: on the development server, on the .appspot.com domain and on custom domains mapping (including with hostname-based mapping):

<instance_id>.<module_version>.<module_name>.<app_name>.(appspot.com|<devserver_hostname>:<port#>)

这将是,例如,我的应用程序对模块名称下方的任何内容不感兴趣并使用基于主机名的自定义域调度路由的方法 - 模块映射到不同的主机名):

This would be, for example, my approach for an app not interested in anything below the module name and using hostname-based custom domain dispatch routing - modules mapped to different hostnames):

def get_module_url(self, module_name='default'):
    host_name = modules.get_hostname(module=module_name)
    if os.environ.get('SERVER_SOFTWARE').startswith('Development'):
        return 'http://' + host_name
    app_name = app_identity.get_application_id()
    split_name = self.request.host.split(':')[0].split('.')
    if split_name[-2] == 'appspot':
        new_host_name = app_name if module_name == 'default' else module_name + '-dot-' + app_name
    else:
        # custom hostname-based domain mapping, default module goes to `www`.mydomain.com
        new_host_name = 'www' if module_name == 'default' else module_name
        if app_name.endswith('-staging'):
            #  copy of the GAE app for staging purposes on mydomain.com
            new_host_name += '-staging'
    return '.'.join(['https://' + new_host_name] + split_name[1:])

这篇关于解决 App Engine 模块上的发现路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆