... | ... | @@ -37,3 +37,26 @@ You'll note a lot of new files have appeared, of note are: |
|
|
Second, we've bound **F5** to the `start.sh` script, so now updating packages and restarting the service is as simple as pressing **F5**.
|
|
|
|
|
|
## Before we start adding stuff, lets introduce some timestamps, everyone loves timestamps!
|
|
|
|
|
|
FastAPI has a concept called _middleware_ - from te documentation:
|
|
|
|
|
|
_A "middleware" is a function that works with every **request** before it is processed by any specific path operation._\
|
|
|
_And also with every **response** before returning it._
|
|
|
|
|
|
Lets take advantage of that, to add timing data our request/response cycle, run `vim main.py`
|
|
|
|
|
|
Enter insert-mode ( press `i` ), and insert the following code at line 12 ( below `app = FastAPI()` )
|
|
|
|
|
|
```
|
|
|
from time import time
|
|
|
from fastapi import Request
|
|
|
|
|
|
@app.middleware("http")
|
|
|
async def add_process_time_header(request: Request, call_next):
|
|
|
start_time = time()
|
|
|
response = await call_next(request)
|
|
|
process_time = time() - start_time
|
|
|
|
|
|
response.headers["X-Process-Time"] = str(process_time)
|
|
|
return response
|
|
|
``` |
|
|
\ No newline at end of file |