]> git.ipfire.org Git - pbs.git/commitdiff
api: Add processing time to the response headers
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 10 Jul 2025 13:22:55 +0000 (13:22 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 10 Jul 2025 13:22:55 +0000 (13:22 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/api/__init__.py

index 27bd851e185d17af516b5ec02d0b70828505bc3d..813f9e23009b66784027e9ad53b63a16adf6917b 100644 (file)
@@ -20,6 +20,7 @@
 
 import fastapi
 import fastapi.middleware.cors
+import time
 
 from .. import Backend
 
@@ -36,6 +37,25 @@ app = fastapi.FastAPI(
        debug = True,
 )
 
+@app.middleware("http")
+async def add_process_time_header(request: fastapi.Request, call_next):
+       """
+               Add the processing time of the request to the response headers.
+       """
+       # Store the start time
+       t_start = time.perf_counter()
+
+       # Continue to run the request...
+       response = await call_next(request)
+
+       # Store the end time
+       t_end = time.perf_counter()
+
+       # Set the process time in the header in milliseconds
+       response.headers["X-Process-Time"] = "%.2fms" % ((t_end - t_start) * 1000.0)
+
+       return response
+
 # Add CORS
 app.add_middleware(
        fastapi.middleware.cors.CORSMiddleware,