简体   繁体   English

使用带有Angular的Flask Restful Google App Engine

[英]Use Flask Restful Google App Engine with Angular

I am trying to marry this angular python project and this restful flask project . 我正试图嫁给这个角形python项目和这个宁静的烧瓶项目

Directory

- /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

Everything in the app/ folder is exactly the same from the python angular project. app/文件夹中的所有内容与python angular项目完全相同。

If I comment out the following from app.yaml , I can access /rest/query and get the expected output. 如果我从app.yaml注释掉以下内容,我可以访问/rest/query并获得预期的输出。

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

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

However, when it is not commented out I get a 404 for /rest/query . 但是,如果没有注释掉,我会得到404 /rest/query At / I am able to see the static index.html page, loaded with the angular hooks. /我能够看到静态index.html页面,加载了角钩。 No data is populated since app.js cannot query /rest/query . 由于app.js无法查询/rest/query因此不会填充任何数据。

How can I set up a GAE Flask restful project that uses Angular? 如何设置使用Angular的GAE Flask restful项目?

The way I had to do this I deploy a flask api with a database as a web service and stand alone app. 我必须这样做,我部署了一个带有数据库作为Web服务和独立应用程序的烧瓶api。

Next you have to add CORS to allow your service to talk to another app, in this case a frontend client using the angular framework. 接下来,您必须添加CORS以允许您的服务与另一个应用程序通信,在这种情况下是使用角度框架的前端客户端。

Then I deploy my angular app that will accept api calls to your backend web service. 然后我部署我的角度应用程序,它将接受api调用你的后端Web服务。

My project uses sql server and .net core web api for the web server deployed to azure. 我的项目使用sql server和.net core web api为部署到azure的Web服务器。 And an Angular app deployed to google cloud. 一个Angular应用程序部署到谷歌云。 I also have another angular app deployed to azure. 我还有另一个角色应用程序部署到azure。

I am working on a new api and dB for for my google cloud angular frontend app to make things easier to test and develop. 我正在研究一个新的api和dB for for google cloud angular frontend app,以便于测试和开发。

I'm not a big fan of this workaround but I moved the routing to happen from app.yaml to main.py 我不是这种解决方法的忠实粉丝,但我将路由从app.yamlmain.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

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

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