feat: implement S3 attachments and presigned uploads
This commit is contained in:
+25
-5
@@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
from contextlib import asynccontextmanager, suppress
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
@@ -13,25 +14,43 @@ from app.api.auth import router as auth_router
|
||||
from app.api.users import router as users_router
|
||||
from app.api.chats import router as chats_router
|
||||
from app.api.messages import router as messages_router
|
||||
from app.api.attachments import router as attachments_router
|
||||
from app.core.config import settings
|
||||
from app.db.session import engine
|
||||
from app.services.object_storage import client as s3_client
|
||||
from app.services.attachment_cleanup import cleanup_expired_uploads
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
|
||||
logger = logging.getLogger(__name__)
|
||||
async def attachment_cleanup_loop():
|
||||
while True:
|
||||
try:
|
||||
# Synchronous boto3/SQLAlchemy I/O must not block the event loop.
|
||||
await asyncio.to_thread(cleanup_expired_uploads)
|
||||
except Exception:
|
||||
logger.exception("Attachment cleanup worker iteration failed")
|
||||
await asyncio.sleep(settings.attachment_cleanup_interval_seconds)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("Application startup instance_id=%s", settings.instance_id)
|
||||
cleanup_task = asyncio.create_task(attachment_cleanup_loop())
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
cleanup_task.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await cleanup_task
|
||||
engine.dispose()
|
||||
logger.info("Application shutdown instance_id=%s", settings.instance_id)
|
||||
|
||||
app = FastAPI(title=settings.app_name, version="1.3.0", lifespan=lifespan)
|
||||
app = FastAPI(title=settings.app_name, version="1.4.0", lifespan=lifespan)
|
||||
app.include_router(users_router)
|
||||
app.include_router(auth_router)
|
||||
app.include_router(chats_router)
|
||||
app.include_router(messages_router)
|
||||
app.include_router(attachments_router)
|
||||
|
||||
@app.middleware("http")
|
||||
async def response_headers(request: Request, call_next):
|
||||
@@ -77,10 +96,11 @@ def readiness():
|
||||
try:
|
||||
with engine.connect() as connection:
|
||||
connection.execute(text("SELECT 1"))
|
||||
s3_client().head_bucket(Bucket=settings.s3_bucket)
|
||||
return {"status": "ok"}
|
||||
except SQLAlchemyError:
|
||||
logger.exception("PostgreSQL readiness check failed")
|
||||
except Exception:
|
||||
logger.exception("PostgreSQL or S3 readiness check failed")
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail="Database unavailable",
|
||||
detail="Required storage unavailable",
|
||||
) from None
|
||||
|
||||
Reference in New Issue
Block a user