fastapi

FastAPI 工程管理(四) 工程示例

倾然丶 夕夏残阳落幕 提交于 2020-10-02 09:04:49
作者:麦克煎蛋 出处:https://www.cnblogs.com/mazhiyong/ 转载请保留这段声明,谢谢! 结合前期学习的过程,整理了一份工程实例模板,在基于FastAPI框架的前提下,参考了Flask的一些业务逻辑和文件配置。 在测试环境和生产环境都经过了实际测试,个人认为可以实际应用于正式环境了。 代码地址: https://github.com/zhiyongma/fastproject 工程目录结构 ├── app │ ├── auth # JWT Authorization │ ├── models # database models │ ├── routers # api routers │ └── util # utility │ ├── __init__ .py # entry file │ ├── config.py # project config │ ├── database.py # database ├── deploy # deploy tools │ ├── gunicorn_fast.service # service sample │ ├── test_user.sql # user db sample ├── gunicorn.py # gunicorn config ├── local.py # for development

FastAPI middleware peeking into responses

时光怂恿深爱的人放手 提交于 2020-08-25 09:16:32
问题 I try to write a simple middleware for FastAPI peeking into response bodies. In this example I just log the body content: app = FastAPI() @app.middleware("http") async def log_request(request, call_next): logger.info(f'{request.method} {request.url}') response = await call_next(request) logger.info(f'Status code: {response.status_code}') async for line in response.body_iterator: logger.info(f' {line}') return response However it looks like I "consume" the body this way, resulting in this

Python 简单api 框架 fastapi

回眸只為那壹抹淺笑 提交于 2020-08-15 04:01:19
要求 python3.6版本及以上 pip install fastapi pip install uvicorn 简单实例 from fastapi import FastAPI app = FastAPI() # 创建API实例 @app.get("/") async def root(): return {"message": "Hello World"} @app.get("/") 功能是定义路径操作,代表着访问 example.com/ 时执行GET操作。 路径,即网址第一个斜杠到最后的部分,比如 https://example.com/items/foo 的路径就是 /items/foo ,通常也称为端点或 路由 操作,即GET,POST,PUT,DELETE等HTTP方法 在python中, @something 被称为装饰,意味着采用下面的函数进行处理。 async def 是定义异步函数的方法,你也可以定义为普通函数 def 简单来说,如果你的程序不需要执行的先后顺序(比如先访问数据库,再返回字典),那么可以用异步,否则的话用普通的函数即可 return 可以返回dict,list,str,int等等。 将其复制到 main.py ,打开 cmd ,输入 uvicorn main:app --reload ,即可运行。 参数解释。 main :文件 main.py