Add db_session_middleware to main.py that opens the connection at the start of each request and closes it in a try/finally block, ensuring connections are always returned even on uncaught exceptions. Remove all individual db.close() calls from 30 router files in app/routers_v2/ — the middleware now handles all code paths. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
34 lines
960 B
Python
34 lines
960 B
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
import logging
|
|
|
|
from ..db_engine import db, Player
|
|
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA, PRIVATE_IN_SCHEMA
|
|
|
|
logging.basicConfig(
|
|
filename=LOG_DATA['filename'],
|
|
format=LOG_DATA['format'],
|
|
level=LOG_DATA['log_level']
|
|
)
|
|
|
|
router = APIRouter(
|
|
prefix='/api/v2/admin',
|
|
tags=['Admin']
|
|
)
|
|
|
|
|
|
@router.post('/stl-fix', include_in_schema=PRIVATE_IN_SCHEMA)
|
|
async def stl_cardinals_fix(token: str = Depends(oauth2_scheme)):
|
|
if not valid_token(token):
|
|
logging.warning(f'Bad Token: {token}')
|
|
raise HTTPException(
|
|
status_code=401,
|
|
detail='You are not authorized to post. This event has been logged.'
|
|
)
|
|
|
|
p_query = Player.update(mlbclub='St Louis Cardinals', franchise='St Louis Cardinals').where(
|
|
Player.mlbclub == 'St. Louis Cardinals'
|
|
).execute()
|
|
|
|
return {'detail': f'Removed the period from St Louis'}
|
|
|