0

嘿伙计们,我正在尝试在生产中运行 Quart。

那是我的代码: 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

服务器.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:没有名为“nini”的模块

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

我没有看到很多关于这方面的教程。还尝试运行:

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

同样的错误

4

1 回答 1

0

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

您可能希望将该db.init_app(app)行移动到create_app函数内(我认为您已经调用get_app_instance了它?),因为它也仅在您调用 server 作为主模块时才被调用。

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

于 2021-06-09T20:47:57.027 回答