Using Google endpoints in different modules of the same app

為{幸葍}努か 提交于 2019-12-11 05:57:24

问题


I'm quite new to development with Google App engine and other Google services of the Cloud platform and I'd like to create an app with different modules (so they can have their own lifecycle) which use endpoints.

I'm struggling with api paths because I don't know how to route requests to the good module.

My directory tree is like that:

/myApp
  /module1
    __init__.py
    main.py
  /module2
    __init__.py
    main.py
  module1.yaml
  module2.yaml
  dispatch.yaml

module1.yaml

application: myapp
runtime: python27
threadsafe: true
module: module1
version: 0
api_version: 1

handlers:
# The endpoints handler must be mapped to /_ah/spi.
# Apps send requests to /_ah/api, but the endpoints service handles mapping
# those requests to /_ah/spi.
- url: /_ah/spi/.*
  script: module1.main.api

libraries:
- name: pycrypto
  version: 2.6
- name: endpoints
  version: 1.0

module2.yaml

application: myapp
runtime: python27
threadsafe: true
module: module2
version: 0
api_version: 1

handlers:
# The endpoints handler must be mapped to /_ah/spi.
# Apps send requests to /_ah/api, but the endpoints service handles mapping
# those requests to /_ah/spi.
- url: /_ah/spi/.*
  script: module2.main.api

libraries:
- name: pycrypto
  version: 2.6
- name: endpoints
  version: 1.0

dispatch.yaml

dispatch:
  - url: "*/_ah/spi/*"
    module: module1

  - url: "*/_ah/spi/.*"
    module: module2

So I'd like my endpoints to be called with the name of the corresponding module somewhere ('_ah/api/module1' or 'module1/_ah/api'). I don't know what to put in the different .yaml files. I don't even know if what I'm doing is right, or possible.

Thanks for your answers.


回答1:


You can host different endpoints on different modules (now called services); the way to correctly address them is as follows:

https://<service-name>-dot-<your-project-id>.appspot.com/_ah/api

Now, let's say you have—as per your description—module1 and module2, each one hosting different endpoints. You will call module1 APIs by hitting:

https://module1-dot-<your-project-id>.appspot.com/_ah/api

And in a similar fashion, module2 APIs:

https://module2-dot-<your-project-id>.appspot.com/_ah/api

If you want to dig deeper into how this URL schema works (including versions, which are another important part of the equation here), go read Addressing microservices and the immediately following section Using API versions



来源:https://stackoverflow.com/questions/37166540/using-google-endpoints-in-different-modules-of-the-same-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!