Flask 应用程序在 Heroku 上因 R10 错误而崩溃

Flask app crashes with R10 error on Heroku

我正在尝试将我的 Flask 应用程序部署到 Heroku,但收到以下错误:

2022-05-10T12:13:10.776664+00:00 heroku[web.1]: State changed from crashed to starting
2022-05-10T12:13:15.072537+00:00 heroku[web.1]: Starting process with command `python website/__init__.py -p 31242`
2022-05-10T12:13:19.049387+00:00 app[web.1]: * Serving Flask app '__init__' (lazy loading)
2022-05-10T12:13:19.049410+00:00 app[web.1]: * Environment: production
2022-05-10T12:13:19.049443+00:00 app[web.1]: WARNING: This is a development server. Do not use it in a production deployment.
2022-05-10T12:13:19.049473+00:00 app[web.1]: Use a production WSGI server instead.
2022-05-10T12:13:19.049493+00:00 app[web.1]: * Debug mode: off
2022-05-10T12:13:19.060373+00:00 app[web.1]: * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
2022-05-10T12:13:20.000000+00:00 app[api]: Build succeeded
2022-05-10T12:14:15.405462+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-05-10T12:14:15.556488+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-05-10T12:14:15.802247+00:00 heroku[web.1]: Process exited with status 137
2022-05-10T12:14:16.000898+00:00 heroku[web.1]: State changed from starting to crashed

这是我的 Procfile:

web: python website/__init__.py -p $PORT

这是我的项目结构: Project structure

这是我的 __init__.py:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_migrate import Migrate

app = Flask(__name__)
app.config.from_object("website.config")

login_manager = LoginManager(app)
login_manager.init_app(app)
login_manager.login_view = 'login'

db = SQLAlchemy()
migrate = Migrate(app, db)
db.init_app(app)

from website.blueprints.main import main as main_blueprint

app.register_blueprint(main_blueprint)
from website.blueprints.auth import auth as auth_blueprint

app.register_blueprint(auth_blueprint)
from website.blueprints.shop import shop as shop_blueprint

app.register_blueprint(shop_blueprint)
from website.blueprints.admin import admin as admin_blueprint

app.register_blueprint(admin_blueprint)

import website.models
import website.forms
import website.utils

db.create_all(app=app)

if __name__ == "__main__":
    app.run()

看不出哪里出了问题,Flask 正常启动,但随后 heroku 崩溃了。我想我的项目结构或 Procfile 是错误的,我搜索了我的问题但没有找到解决方案。请帮助我。

Flask 应用程序需要以编程方式绑定到 Heroku $PORT

port_nr = int(os.environ.get("PORT", 5001))
app.run(port=port_nr, host='0.0.0.0')