1 Star 2 Fork 1

三国电视台/mongodb-fastapi

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
app.py 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
三国电视台 提交于 2022-12-04 19:35 . 增加异常处理
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi_authz import CasbinMiddleware
from starlette.exceptions import HTTPException
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import PlainTextResponse
import auth.permission.route
import auth.role.route
import auth.user.route
from auth.jwt.service import JWTAuthenticationBackend
from auth.role.service import enforcer
from conf.config import init_database
app = FastAPI()
app.add_middleware(CasbinMiddleware, enforcer=enforcer)
app.add_middleware(AuthenticationMiddleware, backend=JWTAuthenticationBackend())
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.on_event("startup")
async def start_database():
await init_database()
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
return PlainTextResponse(str(exc.detail), status_code=exc.status_code)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
return PlainTextResponse(str(exc), status_code=400)
@app.get("/", tags=["Root"])
async def read_root():
return {"message": "Welcome to this fantastic app."}
app.include_router(auth.permission.route.router, tags=["Permission"], prefix="/auth/permission")
app.include_router(auth.role.route.router, tags=["Role"], prefix="/auth/role")
app.include_router(auth.user.route.router, tags=["User"], prefix="/auth/user")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/treemp3/mongodb-fastapi.git
[email protected]:treemp3/mongodb-fastapi.git
treemp3
mongodb-fastapi
mongodb-fastapi
master

搜索帮助