-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
32 lines (26 loc) · 928 Bytes
/
server.py
File metadata and controls
32 lines (26 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# server.py (FastAPI or your agent service)
from fastapi import FastAPI, Request
import logging
import uuid
from logging_config import init_logging, cv_request_id, cv_user, cv_ip
init_logging()
log = logging.getLogger(__name__)
app = FastAPI()
@app.middleware("http")
async def add_log_context(request: Request, call_next):
# set per-request context vars so ContextFilter can include them
token_req = cv_request_id.set(str(uuid.uuid4()))
token_user = cv_user.set(getattr(request.state, "user_id", None))
token_ip = cv_ip.set(request.client.host if request.client else None)
try:
response = await call_next(request)
return response
finally:
# reset to avoid leaking between requests
cv_request_id.reset(token_req)
cv_user.reset(token_user)
cv_ip.reset(token_ip)
@app.get("/healthz")
def health():
log.debug("health check")
return {"ok": True}