【问题标题】:Configure Google App Engine to serve a JS client side app配置 Google App Engine 以提供 JS 客户端应用程序
【发布时间】:2019-08-23 13:30:04
【问题描述】:

我有一个基本的flask 应用程序,它使用flask_restful 扩展名(用于制作宁静的服务):

ma​​in.py

from google.cloud import datastore
from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

datastore_client = datastore.Client()

class HelloWorld(Resource):
    def get(self):
        # retrieve from datastore here
        return {'greeting': 'hello'}

api.add_resource(HelloWorld, '/greet')

通常,flask 应用会呈现这样的模板:

@app.route('/')
def home():
    return render_template("index.html")

默认情况下,flask 会从 templates/index.html 目录。

但是,当访问 / 路由时,我不想渲染 a 模板。相反,我想在应用引擎项目的一个完全不同的目录中提供一个 JS 客户端应用程序(Angular、React、Vue):

my-gae-project/
    app.yaml
    main.py
    main_test.py
    requirements.txt
    app/          <--- JS app lives here; consumes flask restful api
        src/
            index.html
            script.js
            style.js
        build/
            index.html

一开始我以为只要修改app.yaml文件指向app/子目录就可以了:

runtime: python37

handlers: 

- url: /static
  static_dir: static

- url: /app
  static_dir: app

- url: /.*      <--- route all requests to '/' to app.index.html
  script: app/index.html

但这不起作用。那么,我究竟如何在 GAE 中为使用 flask rest API 的 JS 客户端提供服务?

【问题讨论】:

    标签: google-app-engine flask google-cloud-platform


    【解决方案1】:

    不建议在 App Engine 的生产环境中这样做,您可以决定是使用 Python Flask 还是 JS(React、Angular、Vue)开发整个应用程序。

    对此的最佳解决方案是将您的应用程序划分为在 GAE 上运行的 two services.

    【讨论】:

    • 这是否意味着一个服务是纯 Python/Flask Rest API 而另一个服务是 JS 客户端?如果是这样,我假设 dispatch.yaml 会将所有流量引导到“JS 客户端”服务,然后使用 restful api 服务?
    • 是的,是的。一个服务将在 Python 中,另一个在 JavaScript 中,例如,一旦向 /index 发出请求,您的 [dispatch.yaml][1] 会将流量定向到 JS 服务。 [1]; cloud.google.com/appengine/docs/standard/python3/reference/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 2011-02-01
    • 2020-07-20
    • 2012-05-24
    相关资源
    最近更新 更多