![](/img/trans.png)
[英]RuntimeError when running a simple flask_restful app on Google App Engine
[英]Use Flask Restful Google App Engine with Angular
我正試圖嫁給這個角形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
app/
文件夾中的所有內容與python angular項目完全相同。
如果我從app.yaml
注釋掉以下內容,我可以訪問/rest/query
並獲得預期的輸出。
- url: /(.+)
static_files: app/\1
upload: app/.*
- url: /
static_files: app/index.html
upload: app/index.html
但是,如果沒有注釋掉,我會得到404
/rest/query
。 在/
我能夠看到靜態index.html
頁面,加載了角鈎。 由於app.js
無法查詢/rest/query
因此不會填充任何數據。
如何設置使用Angular的GAE Flask restful項目?
我必須這樣做,我部署了一個帶有數據庫作為Web服務和獨立應用程序的燒瓶api。
接下來,您必須添加CORS以允許您的服務與另一個應用程序通信,在這種情況下是使用角度框架的前端客戶端。
然后我部署我的角度應用程序,它將接受api調用你的后端Web服務。
我的項目使用sql server和.net core web api為部署到azure的Web服務器。 一個Angular應用程序部署到谷歌雲。 我還有另一個角色應用程序部署到azure。
我正在研究一個新的api和dB for for google cloud angular frontend app,以便於測試和開發。
我不是這種解決方法的忠實粉絲,但我將路由從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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.