James Conkling
James Conkling

Reputation: 3663

Serve static SPA from Google Cloud Storage and API from Google App Engine

I have a static web client SPA serviced by a REST API. I'm trying to figure out the best way to host these apps on Google's Cloud Platform using App Engine to host the API, and Cloud Storage to host the static web client.

If I were doing this from scratch, a simple reverse proxy could manage routing traffic between the API and the client assets. To do the equivalent with GCP, I've looked at the following:

All of the above have limitations. What i'm trying to do seems fairly conventional, but I'm not sure what the path of least resistance is on GCP.

Upvotes: 7

Views: 3783

Answers (3)

程柏硯
程柏硯

Reputation: 156

Let's talk about serving SPA static files from Google App Engine.

The SPAs need to serve many routes to a single index.html, normally called rewrite rules. App Engine can do that with a proper configured app.yaml handlers section.

For the real files part, you serve the real path:

- url: /assets/
  static_dir: path/to/real/files

For these fake routes, serve the entrypoint index.html:

- url: /
  static_file: path/to/index.html
  upload: path/to/index.html
- url: /.*
  static_file: path/to/index.html
  upload: path/to/index.html

By this configuration, Google Frontend will serve the static files without hitting your backend.

Here's one Angular application and I deployed to App Engine, as an example:

Other stuff about securing APIs and CORS policies, you can consider using dispatch.yaml to avoid cross domain problem. Or serve from different domain with cloud endpoints (with IAP jwk configured).

Upvotes: 1

oakinlaja
oakinlaja

Reputation: 906

As you have rightly observed, there are a number of complications that might come into play with your setup. The Google Cloud Storage is simply a Storage, which might not necessarily manage requests to GAE as well as you desire. Perhaps, using Endpoints would be a more viable solution in this case (considering your listed options), where you can use simple Javascripts to call Endpoints in your GAE applications from your Application Files in Google Cloud Storage. However, that being said, I think the better option is to move your static files into App Engine as described here. This will ease the complication of managing resources between two different technologies

Upvotes: -1

Alexandre
Alexandre

Reputation: 2050

Google Cloud storage allow you to host a static website : https://cloud.google.com/storage/docs/hosting-static-website

You don't need to use Endpoint or AppEngine as a reverse proxy

If you need to setup a load balancer based on route or if you need to setup ssl certificates you could use storage bucket as a service backend : https://cloud.google.com/compute/docs/load-balancing/http/backend-bucket

Upvotes: 2

Related Questions