[404在uWSGI / Gunicorn上使用Flask的所有路线

问题描述 投票:0回答:1

我找到并创建了以下Flask / SQLAlchemy / Marshmallow示例项目:

https://github.com/summersab/RestaurantAPI

它就像一个符咒,我用它来构建一个自定义API。它完美运行:

python run.py

然后,我尝试在适当的Web服务器上运行它:

uwsgi --http localhost:5000 --wsgi-file run.py --callable app

但是,除//api/api/v1.0以外的所有路由都得到404。我在Gunicorn上尝试了相同的结果,使我相信问题出在代码,而不是网络服务器配置。

以下所有帖子都有相同的问题,但是据我所知,没有一个帖子对我有用(我可能错过了一些东西:]]

编辑:

根据来自@ v25的回复,我将run.py更改为以下内容:

from flask import Flask, redirect, render_template
from app import api_bp
from model import db, redis_cache
from config import DevelopmentConfig, TestingConfig, BaseConfig, PresentConfig

app = Flask(__name__)

t = 0
def create_app(config_filename):
    app.config.from_object(config_filename)
    global t
    if t == 0:
        app.register_blueprint(api_bp, url_prefix='/api/v1.0')
        t = 1
    if config_filename != TestingConfig:
        db.init_app(app)
        redis_cache.init_app(app)
    return app

@app.route('/')
@app.route('/api/')
@app.route('/api/v1.0/')
def availableApps():

    return render_template('availableApp.html')

PresentConfig = BaseConfig
app = create_app(PresentConfig)

if __name__ == "__main__":
    app.run(use_debugger=False, use_reloader=False, passthrough_errors=True)

然后我用uwsgi运行了它,它按预期运行:

uwsgi --http localhost:5000 --wsgi-file run.py --callable app

感谢您的帮助!

我发现并分支了以下Flask / SQLAlchemy / Marshmallow示例项目:https://github.com/summersab/RestaurantAPI它的工作原理很像魅力,我用它来构建自定义API。它运行...

python flask gunicorn uwsgi
1个回答
1
投票

这可以通过创建一个具有以下内容的新文件wsgi.py来快速解决:

© www.soinside.com 2019 - 2024. All rights reserved.