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

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

问题描述

我想为Python中的云端点构建客户端,如文档

我想从托管虚拟机创建api,因此我通过调用
来获取API的路径

  modules.get_hostname(module =default)

这对devserver可以正常工作,我可以创建发现端点的完整路径,但是在实时系统上,这会将url返回到某个特定版本,如:

  20150628t110011.default.guestbook.appspot.com 

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

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



<但是没有发现证明文件,也许是由于证明事实不匹配长链接和https失败的网址。



有没有合适的方法将默认模块的基本网址接收到?像这样:

  default.guestbook.appspot.com 

,因为这会导致发现工作端点:

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

我想避免在这里执行字符串操作,因为在本地devserver中,这不会工作,因为模块url解析为 localhost:1234

解决方案

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



有关键点:


  • Google不会针对在appspot.com托管的双通配域
    颁发SSL证书,该证书对于https:// 20150628t110011.default.guestbook 不起作用。 appspot.com

  • 您可以使用 -dot - 分隔符来使证书工作。特别是默认
    模块的默认版本可以直接在 guestbook.appspot.com



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

在尝试解决此类问题时,我意识到 modules.get_hostname()现在不再能够执行它的名字所暗示的原始函数了(我猜是因为访问相同实体的多个可能路径)。这可能解释了为什么他们不会尝试修复api以返回正确的主机名:(请参阅可以返回的信息(视适用情况而定,视情况而定)。 调用参数和执行上下文)是恕我直言非常有用的,允许人们为所有3种可能的应用程序使用情况编程获取适当的主机名/ URL:在开发服务器上,在.appspot.com域和自定义域映射(包括主机名):

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

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

  def get_module_url ('self',module_name ='default'):
host_name = modules.get_hostname(module = module_name)
如果os.environ.get('SERVER_SOFTWARE')。startswith('Development'):
返回'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:
#基于自定义主机名的域映射,默认模块转到`www`.mydomain.com
new_host_name ='www'if module_name =='default'else module_name $ b $ if app_name.endswith(' - staging') :
#GAE应用程序的副本,用于分期付款姿势在mydomain.com上
new_host_name + ='-staging'
return'。'。join(['https://'+ new_host_name] + split_name [1:])


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

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")

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

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"

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.

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"

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.

解决方案

You probably want to go through the GAE URl routing doc: https://cloud.google.com/appengine/docs/python/modules/routing#routing_via_url

Key points in there:

  • 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.

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)

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引擎模块上的发现路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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