我正在嘗試在 GAE 上部署一個 Flask 應用程式,該應用程式在幾個藍圖中定義的 Flask 前端和后端路由旁邊提供了一個構建的 React 前端。
在本地,一切正常。在 Flask 應用程式中,我使用 React 的 build 目錄作為靜態檔案夾,并為大多數前端路由回傳 React index.html 檔案。
這是我的結構的基本概述:
- main.py
- build
- index.html
- static
- css
- js
- api
- auth.py
- templates
- static
Flask 上的索引路由捕獲 URL,有時由 id 引數化,并將這些請求發送到 React 前端:
@views.route('/', defaults={'path': '', 'id': ''})
@views.route('/<string:path>', defaults={'id': ''}) # Match all strings
@views.route('/<path:path>', defaults={'id': ''}) # Match all paths
@views.route('/<string:path>/<string:id>') # Match URLs that contain IDs
def index(path, id):
return app.send_static_file('index.html')
但是,身份驗證 UI 不在 React 前端。它需要由 Flask 單獨提供。為了處理這些請求,身份驗證藍圖會切換 url 前綴和靜態檔案夾。
authentication = Blueprint(
'authentication',
__name__,
url_prefix='/auth',
static_folder='static',
template_folder='templates'
)
@authentication.route('/admin_settings', methods=['GET'])
@login_required
def admin_settings():
return render_template('admin_settings.html')
同樣,在本地運行時,此設定運行良好。但是在部署到 GAE 時,我遇到了許多不同的錯誤,這些錯誤源于我構建 app.yaml 設定的方式,包括 502、404 和無休止的網路請求,這顯然是作業人員超時的結果。
應如何為此設定撰寫 GAE 上的 app.yaml 檔案?我獲得的最大成功是使用以下 app.yaml 檔案,它至少加載了 React 前端(盡管所有其他路由似乎都觸發了超時):
# app.yaml
runtime: python39
entrypoint: gunicorn -b :$PORT main:app
# handlers
handlers:
- url: /.*
script: main.app
uj5u.com熱心網友回復:
您應該配置您的應用程式app.yaml以提供靜態檔案并處理應用程式的路由。例如:
runtime: python39
entrypoint: gunicorn -b :$PORT main:app
# handlers
handlers:
- url: /api
static_dir: api
- url: /api/static
static_dir: static
- url: /.*
script: auto
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372080.html
下一篇:GoogleCloudDjangoifos.getenv('SERVER_SOFTWARE','').startswith('GoogleAppEng
