使用Flask Restful和Angular在Google App Engine上。

3
我试图将这个angular python项目和这个restful flask项目结合起来。 目录
- /app
    - css/app.css
    - js/app.js
    - index.html
- app.yaml
- main.py
- appengine_config.py
- vendor.py
- requirements.txt

app.yaml

application: your-application-id-here
version: 1
runtime: python37
api_version: 1
threadsafe: yes

handlers:
- url: /rest/.*
  script: main.APP

- url: /(.+)
  static_files: app/\1
  upload: app/.*

- url: /
  static_files: app/index.html
  upload: app/index.html

main.py

from flask import Flask
from flask.ext import restful


APP = Flask(__name__)
api = restful.Api(APP)


class HelloWorld(restful.Resource):
    def get(self):
        return {'hello': 'world'}


api.add_resource(HelloWorld, '/rest/query/')


@app.errorhandler(404)
def page_not_found(e):
    """Return a custom 404 error."""
    return 'Sorry, Nothing at this URL.', 404


@app.errorhandler(500)
def page_not_found(e):
    """Return a custom 500 error."""
    return 'Sorry, unexpected error: {}'.format(e), 500

app/文件夹中的所有内容与Python Angular项目完全相同。

如果我从app.yaml中注释掉以下内容,我就可以访问/rest/query并获得预期的输出。

- url: /(.+)
  static_files: app/\1
  upload: app/.*

- url: /
  static_files: app/index.html
  upload: app/index.html

然而,如果没有将其注释掉,我会在/rest/query处得到一个404错误。在/处,我能够看到静态的index.html页面,已经加载了angular钩子。由于app.js无法查询/rest/query,因此没有数据被填充。

我应该如何设置一个使用Angular的GAE Flask Restful项目?


你能展示一下你的应用程序目录结构,包括 app.yamlmain.py 文件的位置吗? - Dan Cornilescu
@DanCornilescu 编辑,如果您查看上面的链接,您将找到 appengine_config .pyvendor.py。它们基本上是在目录根目录下寻找生成的 /lib 文件夹中的依赖项。 - user1222324562
2个回答

1

我不是很喜欢这种解决方法,但我把路由从app.yaml移动到了main.py

main.py

from flask import Flask, render_template
from flask.ext import restful


class HelloWorld(restful.Resource):
    def get(self):
        return {'hello': 'world'}


app = Flask(__name__)
api = restful.Api(app)
api.add_resource(HelloWorld, '/rest/query')


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


@app.errorhandler(404)
def page_not_found(e):
    """Return a custom 404 error."""
    return 'Sorry, Nothing at this URL.', 404


@app.errorhandler(500)
def page_not_found(e):
    """Return a custom 500 error."""
    return 'Sorry, unexpected error: {}'.format(e), 500

app.yaml

application: application-id-here
version: 1
runtime: python37
api_version: 1
threadsafe: yes

handlers:
- url: /static
  static_dir: static

- url: /.*
  script: main.app

directory

- /static
    - css/app.css
    - js/app.js
    - partials/...
- /templates/index.html
- app.yaml
- main.py
- appengine_config.py
- vendor.py
- requirements.txt

1
我需要部署一个带有数据库的Flask API作为Web服务和独立应用程序来实现此操作。
接下来,您需要添加CORS以允许您的服务与另一个应用程序通信,在这种情况下是使用Angular框架的前端客户端。
然后,我部署我的Angular应用程序,它将接受对您的后端Web服务的API调用。
我的项目使用SQL Server和.NET Core Web API作为Web服务器部署到Azure。还有一个部署在Google Cloud上的Angular应用程序。我还有另一个部署在Azure上的Angular应用程序。
我正在开发一个新的API和dB,用于我的Google Cloud Angular前端应用程序,以使测试和开发变得更加容易。

我的意图是在一个GAE项目下保持一切小而简单。我正在努力克服这个障碍,即我可以运行一个Angular应用程序_或_REST API,但不能同时运行两者。这一定是一个app.yaml路由问题,对吧? - user1222324562
特别是在上面的Angular示例中,您可以使用webapp2来实现这一点。 - user1222324562

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接