sachin commited on
Commit
230a925
·
1 Parent(s): 225ae45

add- timing

Browse files
Files changed (1) hide show
  1. src/server/main.py +13 -0
src/server/main.py CHANGED
@@ -453,6 +453,7 @@ class ChatRequest(BaseModel):
453
  raise ValueError(f"Unsupported language code: {v}. Supported codes: {', '.join(SUPPORTED_LANGUAGES)}")
454
  return v
455
 
 
456
  class ChatResponse(BaseModel):
457
  response: str
458
 
@@ -531,6 +532,7 @@ app = FastAPI(
531
  lifespan=lifespan
532
  )
533
 
 
534
  app.add_middleware(
535
  CORSMiddleware,
536
  allow_origins=["*"],
@@ -539,6 +541,17 @@ app.add_middleware(
539
  allow_headers=["*"],
540
  )
541
 
 
 
 
 
 
 
 
 
 
 
 
542
  limiter = Limiter(key_func=get_remote_address)
543
  app.state.limiter = limiter
544
 
 
453
  raise ValueError(f"Unsupported language code: {v}. Supported codes: {', '.join(SUPPORTED_LANGUAGES)}")
454
  return v
455
 
456
+
457
  class ChatResponse(BaseModel):
458
  response: str
459
 
 
532
  lifespan=lifespan
533
  )
534
 
535
+ # Add CORS Middleware
536
  app.add_middleware(
537
  CORSMiddleware,
538
  allow_origins=["*"],
 
541
  allow_headers=["*"],
542
  )
543
 
544
+ # Add Timing Middleware
545
+ @app.middleware("http")
546
+ async def add_request_timing(request: Request, call_next):
547
+ start_time = time()
548
+ response = await call_next(request)
549
+ end_time = time()
550
+ duration = end_time - start_time
551
+ logger.info(f"Request to {request.url.path} took {duration:.3f} seconds")
552
+ response.headers["X-Response-Time"] = f"{duration:.3f}"
553
+ return response
554
+
555
  limiter = Limiter(key_func=get_remote_address)
556
  app.state.limiter = limiter
557