-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RBAC authorisation and some tools or optimisations (#41)
* WIP: add rbac authorization * Perform pre-commit fixes * add rbac route whitelist * add init test data user role associations * Restore database table id naming to fix generic crud base * Add database section value uniqueness settings * Update the test directory to tests * Update route_name file name to health_check * Split user auth and user action interfaces * Fix conflict between merge and current branch * Add pymysql dependencies * Fix RBAC authentication method * Add the select serialisation tool * Fix missing return messages due to global exception handler slicing * Update the user interface with associated relationships * Add items to be completed * Perform pre-commit fixes * Add pre-made routers * Paging data return structure optimisation * Split user auth and user interface tests * Fix user register test data structure error * Fix duplicate named test classes
- Loading branch information
Showing
62 changed files
with
1,149 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# FastAPI 最佳架构 | ||
|
||
这是 FastAPI 框架的一个基础项目,在制作中 | ||
|
||
它的目的是让你直接用它作为你的基础项目来开发你的项目 | ||
|
||
支持 python3.10 及以上版本 | ||
|
||
## 技术栈 | ||
|
||
- [x] FastAPI | ||
- [x] Pydantic | ||
- [x] SQLAlchemy | ||
- [x] Alembic | ||
- [x] MySQL | ||
- [x] Redis | ||
- [x] APScheduler | ||
- [x] Docker | ||
|
||
## 克隆 | ||
|
||
```shell | ||
git clone https://github.com/wu-clan/fastapi_best_architecture.git | ||
``` | ||
|
||
## 使用: | ||
|
||
### 1:传统 | ||
|
||
1. 安装依赖项 | ||
```shell | ||
pip install -r requirements.txt | ||
``` | ||
|
||
2. 创建一个数据库`fba`,选择 utf8mb4 编码 | ||
3. 安装并启动 Redis | ||
4. 在`backend/app/`目录下创建一个`.env`文件 | ||
```shell | ||
cd backend/app/ | ||
touch .env | ||
``` | ||
5. 复制 `.env.example` 到 `.env` 并查看`backend/app/core/conf.py`,更新数据库配置信息 | ||
6. 进行数据库迁移[alembic](https://alembic.sqlalchemy.org/en/latest/tutorial.html) | ||
```shell | ||
cd backend/app/ | ||
# 生成迁移文件 | ||
alembic revision --autogenerate | ||
# 执行迁移 | ||
alembic upgrade head | ||
``` | ||
7. 执行 `backend/app/main.py` 文件启动服务 | ||
8. 浏览器访问:http://127.0.0.1:8000/v1/docs | ||
|
||
--- | ||
|
||
### 2:Docker | ||
|
||
1. 在 `docker-compose.yml` 文件所在的目录中运行一键启动命令 | ||
|
||
```shell | ||
docker-compose up -d -build | ||
``` | ||
|
||
2. 等待命令自动完成 | ||
|
||
3. 浏览器访问:http://127.0.0.1:8000/v1/docs | ||
|
||
## 初始化测试数据 | ||
|
||
执行 `backend/app/init_test_data.py` 文件 | ||
|
||
## 测试 | ||
|
||
通过 pytest 进行测试 | ||
|
||
**提示**: 在测试开始前,请先执行初始化测试数据,同时,需要启动 fastapi 服务。 | ||
|
||
1. 首先,进入app目录 | ||
|
||
```shell | ||
cd backend/app/ | ||
``` | ||
|
||
2. 执行测试命令 | ||
|
||
```shell | ||
pytest -vs --disable-warnings | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter() | ||
|
||
# TODO: 添加 api 相关接口 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
from fastapi import APIRouter | ||
from backend.app.api.v1.auth.user import router as user_router | ||
from backend.app.api.v1.auth.auth import router as auth_router | ||
|
||
router = APIRouter(prefix='/auth', tags=['用户管理']) | ||
router = APIRouter(prefix='/auth', tags=['认证']) | ||
|
||
router.include_router(user_router, prefix='/users') | ||
router.include_router(auth_router, prefix='/users') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
from fastapi import APIRouter, Depends | ||
from fastapi.security import OAuth2PasswordRequestForm | ||
|
||
from backend.app.common.jwt import DependsUser | ||
from backend.app.common.response.response_schema import response_base | ||
from backend.app.schemas.token import Token | ||
from backend.app.schemas.user import Auth | ||
from backend.app.services.user_service import UserService | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post('/swagger_login', summary='swagger 表单登录', description='form 格式登录,仅用于 swagger 文档调试接口') | ||
async def swagger_user_login(form_data: OAuth2PasswordRequestForm = Depends()) -> Token: | ||
token, user = await UserService.swagger_login(form_data) | ||
return Token(access_token=token, user=user) | ||
|
||
|
||
@router.post('/login', summary='用户登录', description='json 格式登录, 仅支持在第三方api工具调试接口, 例如: postman') | ||
async def user_login(obj: Auth): | ||
token, user = await UserService.login(obj) | ||
# TODO: token 存储 | ||
data = Token(access_token=token, user=user) | ||
return response_base.response_200(data=data) | ||
|
||
|
||
@router.post('/logout', summary='用户登出', dependencies=[DependsUser]) | ||
async def user_logout(): | ||
# TODO: 加入 token 黑名单 | ||
return response_base.response_200() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter() | ||
|
||
# TODO: 添加 casbin 相关接口 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter() | ||
|
||
# TODO: 添加 dept 相关接口 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter() | ||
|
||
# TODO: 添加 menu 相关接口 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter() | ||
|
||
# TODO: 添加 role 相关接口 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.