Skip to content

Commit

Permalink
add header and process_time middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Nov 1, 2024
1 parent ed84dd4 commit 98bfc7d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ ENV HOST '0.0.0.0'
EXPOSE $PORT
HEALTHCHECK CMD curl --fail http://$HOST:$PORT/_stcore/health

CMD exec streamlit run app.py --server.port=$PORT --server.address=$HOST
CMD exec streamlit run index.py --server.port=$PORT --server.address=$HOST
23 changes: 22 additions & 1 deletion server/src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from contextlib import asynccontextmanager
from time import time
from typing import Callable

from fastapi import FastAPI
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi_utilities import add_timer_middleware


@asynccontextmanager
Expand All @@ -27,6 +30,24 @@ async def lifespan(_app: FastAPI):
expose_headers=["*"],
)

add_timer_middleware(app, show_avg=True)


@app.middleware("http")
async def inject_exec_time_header(request: Request, call_next: Callable):
"""add request execution time header"""
start_time = time()
response = await call_next(request)
response.headers["X-Execution-Time"] = f"{(time() - start_time):.2f}s"
return response


@app.middleware("http")
async def log_request_headers(request: Request, call_next: Callable):
"""log request headers"""
print("Request headers: %s", request.headers)
return await call_next(request)


@app.get("/")
async def root():
Expand Down

0 comments on commit 98bfc7d

Please sign in to comment.