feat: 初始化仓库
This commit is contained in:
101
app.py
Normal file
101
app.py
Normal file
@@ -0,0 +1,101 @@
|
||||
# _*_ coding : UTF-8 _*_
|
||||
# @Time : 2025/01/17 21:18
|
||||
# @UpdateTime : 2025/01/17 21:18
|
||||
# @Author : sonder
|
||||
# @File : app.py
|
||||
# @Software : PyCharm
|
||||
# @Comment : 本程序为项目启动文件
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from fastapi.openapi.utils import get_openapi
|
||||
|
||||
from api.cache import cacheAPI
|
||||
from api.department import departmentAPI
|
||||
from api.file import fileAPI
|
||||
from api.log import logAPI
|
||||
from api.login import loginAPI
|
||||
from api.permission import permissionAPI
|
||||
from api.role import roleAPI
|
||||
from api.server import serverAPI
|
||||
from api.user import userAPI
|
||||
from api.i18n import i18nAPI
|
||||
from config.database import init_db, close_db
|
||||
from config.env import AppConfig
|
||||
from config.get_redis import Redis
|
||||
from exceptions.handle import handle_exception
|
||||
from middlewares.handle import handle_middleware
|
||||
from utils.log import logger
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info(f'{AppConfig.app_name}开始启动')
|
||||
app.state.redis = await Redis.create_redis_pool()
|
||||
logger.info(f'{AppConfig.app_name}启动成功')
|
||||
await init_db()
|
||||
yield
|
||||
await close_db()
|
||||
await Redis.close_redis_pool(app)
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title=AppConfig.app_name,
|
||||
description=f'{AppConfig.app_name}接口文档',
|
||||
version=AppConfig.app_version,
|
||||
lifespan=lifespan,
|
||||
)
|
||||
# 加载中间件处理方法
|
||||
handle_middleware(app)
|
||||
# 加载全局异常处理方法
|
||||
handle_exception(app)
|
||||
|
||||
|
||||
def custom_openapi():
|
||||
if app.openapi_schema:
|
||||
return app.openapi_schema
|
||||
openapi_schema = get_openapi(
|
||||
title="My API",
|
||||
version="1.0.0",
|
||||
routes=app.routes,
|
||||
)
|
||||
# 修改 Swagger UI 配置
|
||||
openapi_schema["components"]["securitySchemes"] = {
|
||||
"Bearer": {
|
||||
"type": "apiKey",
|
||||
"name": "Authorization",
|
||||
"in": "header"
|
||||
}
|
||||
}
|
||||
app.openapi_schema = openapi_schema
|
||||
return app.openapi_schema
|
||||
|
||||
|
||||
api_list = [
|
||||
{'api': loginAPI, 'tags': ['登录管理']},
|
||||
{'api': userAPI, 'tags': ['用户管理']},
|
||||
{'api': departmentAPI, 'tags': ['部门管理']},
|
||||
{'api': roleAPI, 'tags': ['角色管理']},
|
||||
{'api': permissionAPI, 'tags': ['权限管理']},
|
||||
{'api': fileAPI, 'tags': ['文件管理']},
|
||||
{'api': logAPI, 'tags': ['日志管理']},
|
||||
{'api': cacheAPI, 'tags': ['缓存管理']},
|
||||
{'api': serverAPI, 'tags': ['服务器管理']},
|
||||
{'api': i18nAPI, 'tags': ['国际化管理']},
|
||||
|
||||
]
|
||||
|
||||
for api in api_list:
|
||||
app.include_router(router=api.get("api"), tags=api.get("tags"))
|
||||
|
||||
if __name__ == '__main__':
|
||||
uvicorn.run(
|
||||
app='app:app',
|
||||
host=AppConfig.app_host,
|
||||
port=AppConfig.app_port,
|
||||
root_path=AppConfig.app_root_path,
|
||||
reload=AppConfig.app_reload,
|
||||
log_config="uvicorn_config.json"
|
||||
)
|
||||
Reference in New Issue
Block a user