feat: request/response logging, CORS, error handling
All checks were successful
Deploy Tracker / deploy (push) Successful in 17s
All checks were successful
Deploy Tracker / deploy (push) Successful in 17s
This commit is contained in:
parent
0891452d63
commit
37c2ac3717
@ -1,8 +1,12 @@
|
|||||||
"""FastAPI application."""
|
"""FastAPI application."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
|
import traceback
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
from tracker.api import agents, labels, projects, tasks
|
from tracker.api import agents, labels, projects, tasks
|
||||||
from tracker.config import settings
|
from tracker.config import settings
|
||||||
@ -12,6 +16,7 @@ logging.basicConfig(
|
|||||||
level=logging.DEBUG if settings.env == "dev" else logging.INFO,
|
level=logging.DEBUG if settings.env == "dev" else logging.INFO,
|
||||||
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
||||||
)
|
)
|
||||||
|
logger = logging.getLogger("tracker")
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="Team Board Tracker",
|
title="Team Board Tracker",
|
||||||
@ -19,6 +24,38 @@ app = FastAPI(
|
|||||||
docs_url="/docs" if settings.env == "dev" else None,
|
docs_url="/docs" if settings.env == "dev" else None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def log_requests(request: Request, call_next):
|
||||||
|
start = time.time()
|
||||||
|
body = None
|
||||||
|
if request.method in ("POST", "PUT", "PATCH"):
|
||||||
|
try:
|
||||||
|
body = await request.body()
|
||||||
|
logger.info("[REQ] %s %s body=%s", request.method, request.url.path, body.decode()[:500])
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
logger.info("[REQ] %s %s", request.method, request.url.path)
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await call_next(request)
|
||||||
|
elapsed = (time.time() - start) * 1000
|
||||||
|
logger.info("[RES] %s %s → %d (%.0fms)", request.method, request.url.path, response.status_code, elapsed)
|
||||||
|
return response
|
||||||
|
except Exception as e:
|
||||||
|
elapsed = (time.time() - start) * 1000
|
||||||
|
logger.error("[ERR] %s %s → %s (%.0fms)\n%s", request.method, request.url.path, str(e), elapsed, traceback.format_exc())
|
||||||
|
return JSONResponse({"error": str(e)}, status_code=500)
|
||||||
|
|
||||||
|
# CORS
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["https://team.uix.su", "http://localhost:3100"],
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
# REST API
|
# REST API
|
||||||
app.include_router(projects.router, prefix="/api/v1")
|
app.include_router(projects.router, prefix="/api/v1")
|
||||||
app.include_router(tasks.router, prefix="/api/v1")
|
app.include_router(tasks.router, prefix="/api/v1")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user