简体   繁体   English

Google App Engine API +静态架构

[英]Google App Engine API + static architecture

I'm trying to (con)figure the best way to structure a JS client + NodeJS server app to host it on Google Cloud AppEngine (plus possibly other GCP resources). 我正在尝试(构建)构建JS客户端+ NodeJS服务器应用程序以在Google Cloud AppEngine(以及可能的其他GCP资源)上托管它的最佳方法。 So I'm looking for advice / best practices here. 所以我在这里寻找建议/最佳实践。

We have an API server running on a non-default AppEngine service and would like to be able to run several, eg development/staging/production versions on the same project (if possible). 我们有一个在非默认AppEngine服务上运行的API服务器,并且希望能够在同一个项目上运行多个(例如开发/暂存/生产版本)(如果可能)。

We would like to host / serve our static client app on this system because we want to use the same domain to point at it. 我们希望在此系统上托管/提供我们的静态客户端应用程序,因为我们希望使用相同的域来指向它。

In our normal server based setup, the client app is proxied/served on domain.com/ and requests to the API are on domain.com/v1/ 在我们基于服务器的常规设置中,客户端应用程序在domain.com/上代理/提供,对API的请求在domain.com/v1/

I've been working through different options - hosting a separate static site running on AppEngine and using dispatch.yaml to try to route requests - this option doesn't seem to work with domain prefixes, only wildcards, eg 我一直在研究不同的选项 - 在AppEngine上运行一个单独的静态站点并使用dispatch.yaml尝试路由请求 - 这个选项似乎不适用于域前缀,只有通配符,例如

dispatch:
- url: "my-client-service-project.appspot.com/"
  service: my-client-service
- url: "my-client-service-project.appspot.com/v1/*"
  service: my-backend-service

Doesn't work, but: 不起作用,但是:

- url: "*/v1/*"
  service: my-backend-service

Does, which we didn't want because we'd like to run dev , staging & production if possible. 我们不想要的,因为我们想要尽可能地运行devstagingproduction

The other option I've been looking at is having the static folder hosted as part of my app, but I can't seem to get this working either, here is the snippet from my app.yaml : 我一直在看的另一个选项是将静态文件夹作为我的应用程序的一部分托管,但我似乎无法使其工作,这里是我的app.yaml的片段:

handlers:
  - url: /.*
    static_files: client/dist/index.html
    upload: frontend/dist/index.html
  - url: /v1/*
    script: dist/index.js

My guess is that script may not work the same as for Python apps, but I could be wrong - the doc's aren't very clear. 我的猜测是script可能与Python应用程序的工作方式不同,但我可能错了 - 文档不是很清楚。

Ideally, I'd like to host the client front-end static files on storage and point to the AppEngine API server (without specifically pointing to a domain from the client, eg /v1/auth/login rather than my-backend-service-project.appspot.com/v1/ 理想情况下,我想在存储上托管客户端前端静态文件并指向AppEngine API服务器(没有专门指向客户端的域,例如/v1/auth/login而不是my-backend-service-project.appspot.com/v1/

References: 参考文献:

How can I use bucket storage to serve static files on google flex/app engine environment? 如何在google flex / app引擎环境中使用存储桶存储来提供静态文件?

Node.js + static content served by Google App Engine 由Google App Engine提供的Node.js +静态内容

https://cloud.google.com/appengine/docs/flexible/nodejs/serving-static-files https://cloud.google.com/appengine/docs/flexible/nodejs/serving-static-files

https://cloud.google.com/appengine/docs/standard/python/how-requests-are-routed#routing_via_url https://cloud.google.com/appengine/docs/standard/python/how-requests-are-routed#routing_via_url

https://cloud.google.com/appengine/docs/standard/python/config/appref https://cloud.google.com/appengine/docs/standard/python/config/appref

https://cloud.google.com/appengine/docs/standard/python/config/dispatchref https://cloud.google.com/appengine/docs/standard/python/config/dispatchref

To begin: you're mixing up standard and flexible env docs - not a good idea as they don't work the same way. 首先:你正在混合标准和灵活的环境文档 - 这不是一个好主意,因为它们的工作方式不同。 See How to tell if a Google App Engine documentation page applies to the standard or the flexible environment . 请参阅如何判断Google App Engine文档页面是否适用于标准环境或灵活环境

Since your app is Node.JS you have to use the flexible env, for which script and static_files aren't applicable inside app.yaml . 由于您的应用程序是Node.JS,您必须使用灵活的env,其中scriptstatic_files不适用于app.yaml Which is why you can't get them to work. 这就是为什么你不能让他们工作的原因。

The first reference in your list shows the options you have for serving the static files. 列表中的第一个引用显示了为静态文件提供服务的选项。 But I kinda question your desire to use the shared GCS option - it will serve the same content regardless of the dev/staging/production environment, so: 但我有点质疑您希望使用共享GCS选项 - 无论dev / staging / production环境如何,它都将提供相同的内容,因此:

  • you can't have different client side environments 您不能拥有不同的客户端环境
  • how do you see selecting a particular server-side environment since the client side references can only point in one direction (ie environment, if I understand your intention correctly)? 您如何看待选择特定的服务器端环境,因为客户端引用只能指向一个方向(即环境,如果我理解您的意图正确)?

If your desire to use a single domain means that you'd still be OK with using different subdomains (of that domain) and if you'd be willing to use a custom domain this might be of interest: How to use GAE's dispatch.yaml with multiple development environments? 如果您希望使用单个域意味着您仍然可以使用不同的子域(该域名),并且如果您愿意使用自定义域,则可能会对此感兴趣: 如何使用GAE的dispatch.yaml有多个开发环境?

UPDATE: 更新:

Node.JS is currently available in the standard environment as well, so you can use those features, see: Node.JS目前也可在标准环境中使用,因此您可以使用这些功能,请参阅:

Take this answer as a complement to the one of @Dan Corneliscu, as I think it is pretty useful and summarizes what you are doing wrong and what can be achieved in the type of scenario you present. 把这个答案作为@Dan Corneliscu的补充,因为我认为它非常有用,并总结了你做错了什么以及你所呈现的场景类型可以实现什么。 In any case, I would like to provide some more information which may be useful. 无论如何,我想提供一些可能有用的更多信息。

As for the reason why the dispatch rules approach you suggested does not work, you should update your paths in the application accordingly. 至于您建议的调度规则方法不起作用的原因,您应该相应地更新应用程序中的路径。 They should now be listening to /v1/your_endpoint instead of /your_endpoint as they probably did before. 他们现在应该正在侦听/v1/your_endpoint而不是/your_endpoint就像他们之前可能做的那样。 It is not enough with changing your dispatch file. 更改您的调度文件是不够的。 Then also make sure that the Dispatch routes field is populated in your App Engine > Services tab in the Console. 然后还要确保在控制台的App Engine>服务选项卡中填充Dispatch routes字段。

Also the alternative approach you suggested will indeed not work using static_files , but you can follow this guide explaining how to serve Static Files from a GAE Flexible application . 此外,您建议的替代方法确实无法使用static_files ,但您可以按照本指南说明如何从GAE Flexible应用程序提供静态文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM