Mapping subdomain to a service in Google App Engine project

那年仲夏 提交于 2019-12-07 09:57:06

问题


I have a Google App Engine project with following yaml file

handlers:
- url: /web/.*
  script: web_server.app

- url: /api/.*
  script: rest_server.app

How do I make sure subdomain, of a domain I own, be served by rest_server.app script.

Example: If I own example.com

I want example.com to be served by web_server.app, and api.example.com to be served by rest_server.app

Is it possible to do that using Google App Engine.

Example:

handlers:
- url: example.com/.*
  script: web_server.app
- url: api.example.com/.*
  script: rest_server.app

回答1:


Request routing in the app.yaml can not be used to route based on the URL's domain name, see the url table row in the Handlers element doc section.

Because of that you can't really have a single module/service serving your app while simultaneously stripping the filepath portion of the URL you're currently used in your handlers' url configs for routing requests to one script or the other.

You can obtain what you desire by splitting your app into 2 separate services/modules, each handling one script. One of the modules has to be the default module, I'd make the web one the default. A dispatch.yaml file would be used to route requests to their respective modules based on the URL hostname.

The web.yaml file would contain:

module: default

handlers:
- url: /.*
  script: web_server.app

The rest.yaml file would contain:

module: rest

handlers:
- url: /.*
  script: rest_server.app

In the dispatch.yaml file you only need routes for the non-default module(s), requests matching no routes are by default routed to the defaut module:

- url: "api.example.com/*"
  module: rest

You can find a more complete example here: https://stackoverflow.com/a/34111170/4495081

You'd then map both your example.com naked domain and api.example.com subdomain to your app. Follow the Adding a custom domain for your application procedure, paying extra attention to the sections which are slightly different when configuring a naked domain vs a subdomain. See also https://stackoverflow.com/a/36317462/4495081

There is one problem, tho - dispatch.yaml routing based on hostnames doesn't work with the local development server, the requests destined for the rest module would actually go to the default module.

A simpler workaround would be to direct the rest module clients to the actual localhost:PORT URL where the local devserver's rest module listens (displayed in the terminal at dev server startup), instead.

This might not be possible in all cases or for all apps. For example it's an issue if the app makes cross-module requests with auto-generated URLs.

In such cases, to work around it you can temporarily insert a small path portion in the rest.yaml URL, only during testing on the local dev server the rest module (you'd need matching changes on the client side and/or the cross-module URL generation logic):

module: rest

handlers:
- url: /api/.*
  script: rest_server.app

And then you can add a dispatch.yaml rule that is not host-based and would also with the local dev server. This can be left in there permanently, it doesn't hurt if/when deployed in production when the temporary rest.yaml change is reversed:

- url: "api.example.com/*"
  module: rest
- url: "*/api/*"
  module: rest


来源:https://stackoverflow.com/questions/40756290/mapping-subdomain-to-a-service-in-google-app-engine-project

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