首页 > 解决方案 > 如何使这个烧瓶应用适合谷歌云引擎?

问题描述

我目前正在尝试使用谷歌云引擎来部署我的烧瓶网络应用程序,但我不确定我需要如何修改文件夹结构和一些文件名以符合要求。

我有一个这样的烧瓶应用程序:

.
├── app
│   ├── errors.py
│   ├── forms.py
│   ├── __init__.py
│   ├── models.py
│   ├── routes.py
│   ├── static
│   │   └── styles.css
│   └── templates
│       ├── 404.html
│       ├── 500.html
│       ├── archive.html
│       ├── base.html
│       ├── bootstrap.html
│       ├── editor.html
│       ├── index.html
│       ├── login.html
│       ├── _post.html
│       ├── post.html
│       └── tag.html
├── app.db
├── app.yaml
├── blog.py
├── config.py
└── requirements.txt

GCE 的要求类似于此页面main.py上的文件结构,并在顶层包含一个文件。

blog.py在顶层定义了应用程序实例。这需要是main.py文件吗?

我是否需要修改app.yaml文件以表示这种不同的文件结构?

编辑:我重命名blog.pymain.py并将以下行添加到app/__init__.py

if __name__ == '__main__':
     app.run(host='127.0.0.1', port=8080, debug=True)

但运行python main.py不会使应用程序在 localhost 上可用。

标签: pythongoogle-app-engineflask

解决方案


您走在正确的轨道上,文档应该可以帮助您完成后续步骤,您可能已经或可能还没有:

  1. 创建templates/文件夹。
  2. 创建static/文件夹。
  3. 在文件中编写代码main.py
  4. requirements.txt在文件中指定依赖项。
  5. app.yaml在文件中定义您的 Web 服务设置。您需要修改处理程序 url 以根据您的项目文件结构指向正确的目录。有关详细信息,请参阅文档

要回答您的问题,请使用以下代码:

if __name__ == '__main__':
  app.run(host='127.0.0.1', port=8080, debug=True)

仅在本地运行时使用。部署到 Google App Engine 时,Gunicorn 等网络服务器进程将为应用程序提供服务。这可以通过添加entrypointto来配置app.yaml

正如文档指出的那样, “如果您不指定入口点字段, App Engine 也会自动将 gunicorn 添加到您的文件中。”requirements.txt

最后,您将把应用程序部署到 Google App Engine。您将从app.yaml文件所在的项目的根目录发出以下命令:

gcloud app deploy

我发现了一张很好的图片(来自本文),它描述了该过程的概述:

在此处输入图像描述


推荐阅读