运行 Quart 生产落后于 Hypercorn

Running Quart in production behind Hypercorn

大家好,我正在尝试 运行 夸脱生产。

这是我的代码: setups.py

import quart.flask_patch
from db import *

from http import HTTPStatus
from typing import Optional


import flask.json
from quart import ResponseReturnValue, jsonify, Quart
from quart.ctx import RequestContext
from quart.exceptions import HTTPException
from quart_openapi import Pint

from .confs import __author__, __description__, __mail__, __title__, __version__,SQLALCHEMY_DATABASE_URI
from .routes import initialize_routes
from .user_verify import get_jwks, verify_jwt
        

class PintCustom(Pint):
    def __init__(self, *args, no_openapi: bool = False, **kwargs) -> None:
        self.jwks = get_jwks()
        super().__init__(*args, no_openapi=no_openapi, **kwargs)

    async def preprocess_request(self, request_context: Optional[RequestContext] = None) -> Optional[
        ResponseReturnValue]:
        token = request_context.request.headers.get('Authorization')
        if request_context.request.method != 'OPTIONS':
            try:
                verify_jwt(str(token), self.jwks)
            except Exception:
                raise HTTPException(HTTPStatus.UNAUTHORIZED, 403, 'Log in')

        return await super().preprocess_request(request_context)


def get_app_instance():
    # Initial app.
    # app = Quart(__name__)
    app = PintCustom(__name__, title=__title__, version=__version__, description=__description__,
                     contact_email=__mail__, contact=__author__)


    @app.errorhandler(HTTPException)
    def http_exception(e):
        return jsonify(error=e.name, error_details=e.description), e.status_code

    app.jwks = get_jwks()

    # Initial all the REST API routes.
    initialize_routes(app)


    # DB
    app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
    db.init_app(app)

    return app

server.py

__package__ = 'nini'

from .setups import create_app
from .db import db

if __name__ == '__main__':
    app = create_app()
    db.init_app(app)

    app.run(host='127.0.0.1', debug=True)

当我运行 hypercorn server:app 我得到:

ModuleNotFoundError: No module named 'nini'

我运行下推荐../nini/server.py

我没有看到很多这方面的教程。 也试过 运行:

pipenv install quart
pipenv shell
export QUART_APP=server:app
quart run

同样的错误

server 模块上的 app 实例仅在您将该模块作为主模块调用时存在,而 Hypercorn 不会这样做。相反,您可以调用工厂函数 hypercorn "server:create_app()".

您可能希望将 db.init_app(app) 行移至 create_app 函数(我认为您已将其称为 get_app_instance?),因为它也仅在您调用服务器作为主要模块。

我认为您不需要 __package__ = 'nini',这也可能会导致问题。