Putting a Cloud Endpoints API in a separate App En

2019-02-17 06:37发布

I am developing an App Engine app and plan to also provide an API. I would like to separate this API from the main site, so I'm trying to use the "modules" feature to separate both apps. The main site would be the "default" module, and the API would lie in the "api" module. However, I'm having troubles with this.

Right now my main app's YAML file is like this:

application: my-app
module: default
runtime: python27
api_version: 1

...

handlers:
# Root handler
- url: /.*
  script: main.app
  secure: always

...

And the API module YAML file, like this:

application: my-app
module: api
runtime: python27
api_version: 1

handlers:
# Endpoints handler
- url: /_ah/spi/.*
  script: api_main.app
  secure: always

...

On the development server, the app is served on port 8000, and the API on port 7998.

With this configuration, my API doesn't work. Whenever I try to access it using localhost:7998/_ah/api/explorer, I don't get any result. If I try to run an API request manually, I get the following error: {"error": {"message": "BackendService.getApiConfigs Error"}}.

What's strange is I'm also seeing the following lines in the development server logs:

INFO     2014-06-15 18:00:32,368 module.py:639] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 500 -
INFO     2014-06-15 18:00:32,368 module.py:639] api: "GET /_ah/api/my-app/v1/events HTTP/1.1" 500 60

It seems like the API module is trying to POST data to the default module (as seen in the first line of logs).

Right now, the only workaround I found is to add the same handlers for /_ah/spi/.* in the default YAML file, but in this situation the separation between the main app and the API is not effective.

Can someone tell me if the configuration I'm trying to achieve is supported by Cloud Endpoints? Thank you very much!

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-17 07:09

Same problem, I was able to make it work after may temptatives: the (only) way i found is to make the cloud endopoints module the default module. Then i have: on the dev server the two modules listening at different ports, you can see prot numbers on the log and on xxx.appspot.com: yourprojectid.appspot.com for the cloud endpoints and modulename-dot-yourprojectid.appspot.com for the other module

查看更多
Animai°情兽
3楼-- · 2019-02-17 07:22

I had the same problem.

I solved using one file to publish my APIs. This file is indicated in app.yaml. I put my APIs in different files.

\
 app.yaml
 \apis
    publish_api.py
    \teacher
        teacher_api.py
    \student
        student_api.py

**app.yaml:**
- url: /_ah/spi/.*
  script: apis.publish_api.api
  secure: always

**publish_api.py:**
import endpoints
from teacher.teacher_api import TeacherApi
from student.student_api import StudentApi

api = endpoints.api_server([TeacherApi, StudentApi])

**teacher_api.py:**
@endpoints.api( name='teacher',
                version='v1',
                allowed_client_ids=[WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID],
                scopes=[EMAIL_SCOPE])
class TeacherApi(remote.Service):

    @endpoints.method(message_types.VoidMessage, StringMessage,
                      path='teacher', http_method='POST', name='writeTeacher')

**student_api.py:**
...

So, I get to mantain each file separately.

查看更多
登录 后发表回答