Repository navigation
Expand file tree
/
Copy pathcode2.py
More file actions
144 lines (109 loc) · 4.09 KB
/
Copy pathcode2.py
File metadata and controls
144 lines (109 loc) · 4.09 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import time
from contextlib import asynccontextmanager
from dotenv import load_dotenv
load_dotenv()
from fastapi import FastAPI, Response, Request, status
from fastapi.middleware.cors import CORSMiddleware
from starlette.types import ASGIApp, Scope, Receive, Send
from redis.asyncio import Redis
from slowapi import _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from core.rate_limiters.limiter_file import limiter
from core.Exceptions.exception_handlers import global_exception_handler, unexpected_exception_handler
from core.Exceptions.exceptions import AppException
from routers.Ai import take_doc_route
from routers.auth import auth_route
from routers.users import users_routes
from routers.chat import question_route
from utils.logging.config import setup_logging
from utils.config import settings
setup_logging()
os.environ["LANGCHAIN_TRACING_V2"] = "false"
#Pure ASGI Middleware for Process Time & Request Logging
class ProcessTimeMiddleware:
def __init__(self, app: ASGIApp):
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send):
if scope["type"] != "http":
await self.app(scope, receive, send)
return
start_time = time.perf_counter()
path = scope.get("path", "")
method = scope.get("method", "")
client = scope.get("client")
client_host = client[0] if client else None
print(f"[REQUEST START] {method} {path} client={client_host}")
status_code = 500
async def wrapped_send(message):
nonlocal status_code
if message["type"] == "http.response.start":
status_code = message["status"]
process_time_ms = (time.perf_counter() - start_time) * 1000
headers = list(message.get("headers", []))
headers.append((b"x-process-time-ms", f"{process_time_ms:.2f} ms".encode()))
message["headers"] = headers
print(f"[REQUEST END] {method} {path} status={status_code} time={process_time_ms:.2f}ms")
await send(message)
await self.app(scope, receive, wrapped_send)
@asynccontextmanager
async def lifespan(app: FastAPI):
print("Starting application")
app.state.redis = Redis(
host="localhost",
port=6379,
decode_responses=True
)
app.state.redis_binary = Redis(
host="localhost",
port=6379,
decode_responses=False # keeps bytes bytes!
)
yield
print("Closing application")
await app.state.redis.close()
await app.state.redis_binary.close()
app = FastAPI(
title="Social Network Aggregator API",
lifespan=lifespan
)
app.add_middleware(ProcessTimeMiddleware)
origins = [
"http://localhost:5173",
"http://127.0.0.1:5173",
"http://localhost:3000",
"http://127.0.0.1:3000",
"http://localhost:8080",
"http://127.0.0.1:8080",
"http://localhost:4200",
"http://127.0.0.1:4200",
"http://10.0.2.2:8000",
"https://www.yourdomain.com",
"https://yourdomain.com",
"https://staging.yourdomain.com",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.middleware("http")
async def debug_middleware(request: Request, call_next):
print(f"--> Incoming request: {request.method} {request.url.path}")
response = await call_next(request)
print(f"<-- Outgoing response: {response.status_code}")
return response
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.add_exception_handler(AppException, global_exception_handler)
app.add_exception_handler(Exception, unexpected_exception_handler)
# Routers
app.include_router(router=users_routes.router)
app.include_router(router=auth_route.router)
app.include_router(router=take_doc_route.router)
app.include_router(router=question_route.router)
@app.get("/", status_code=status.HTTP_200_OK)
def root():
return {"message": "Hello World"}