问题描述
我开始在 Flask 中为 IoT 应用程序开发一个项目。这个应用程序有一个 API 蓝图,该蓝图一直在增长,现在我正在尝试将 API 迁移到 FastAPI。我的项目遵循以下结构:
api (FastAPI application)
__init__.py (where I create the FastAPI app)
application (Flask application)
__init__.py (where I create the Flask app)
models (sql alchemy models that I want to share between both applications)
wsgi.py (I use multiprocessing to launch to separate processes,one for the Flask App and another for Fast Api)
Flask 模块运行良好,FastApi 模块也运行良好。但是,我无法在 FastAPI 路由器中使用我的模型,因为我没有在此正确初始化数据库。我需要创建一些东西,让我在 FastAPI 和 Flask 模块之间共享会话。
我让你使用我的 __init__.py
两个模块:
烧瓶:
from flask import Flask
from flask_sqlalchemy import sqlAlchemy
db = sqlAlchemy()
def create_app():
"""Initialize the core application."""
app = Flask(__name__,instance_relative_config=False)
# The following line loads the configurations from Config file
app.config.from_object('config.DevelopmentConfig')
db.init_app(app)
with app.app_context():
# Import parts of our application
from application.blueprints.oauth.routes import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
....
db.create_all()
return app
快速API:
from fastapi import FastAPI,APIRouter
from fastapi_sqlalchemy import DBSessionMiddleware # middleware helper
from fastapi_sqlalchemy import db
from .controllers.system import *
def create_app():
"""Initialize the core application."""
app = FastAPI(debug=True)
app.add_middleware(DBSessionMiddleware,db_url="sqlite:///../database.db")
app.include_router(router=router)
return app
我怎样才能做到这一点?
最好的问候,
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)