first init

This commit is contained in:
xingc
2025-02-18 14:26:47 +08:00
commit 48a02974fc
41 changed files with 2297 additions and 0 deletions

51
core/app.py Normal file
View File

@@ -0,0 +1,51 @@
# -*- coding = utf-8 -*-
# @Time : 2025/2/13 下午12:27
# @File : app.py
# @Software : PyCharm
# @Author : xingc
# @Desc :
import logging
from fastapi import FastAPI, Request, Response
from config import settings
from core.routers import routers
logger = logging.getLogger(__name__)
async def exception_log(request: Request, exc):
logger.error('\n'.join([
'',
f'request method: {request.method}',
f'request path: {request.path_params}',
f'request host: {request.client.host}',
f'query parameters: {dict(request.query_params)}',
f'abnormal description: {exc}',
]))
logger.exception(exc)
def create_app() -> FastAPI:
app = FastAPI(
debug=settings.DEBUG,
openapi_url='/openapi.json' if settings.DEBUG else None,
)
for router in routers:
app.include_router(router)
@app.on_event('startup')
async def startup_event():
logger.info(f'Application <<{settings.NAME}>> is started')
@app.on_event('shutdown')
async def shutdown_event():
logger.info(f'Application <<{settings.NAME}>> is closed')
# @app.exception_handler(Exception)
# async def http_exception_handler(request: Request, exc: Exception):
# await exception_log(request, exc)
# return base_response.error(status_code=500, message='Server error')
return app