... | ... | @@ -30,10 +30,33 @@ You'll note a lot of new files have appeared, of note are: |
|
|
* _stop.sh_\
|
|
|
The bash shell-script `stop.sh` will stop the service if it is running, it is used by `start.sh`, and in general we should have to worry about it.
|
|
|
* _view.sh_\
|
|
|
Running the service in the background is nice, but if we ever want to inspect what the service is doing `view.sh` will display the log of the service
|
|
|
Running the service in the background is nice, but if we ever want to inspect what the service is doing `view.sh` will display the log of the service
|
|
|
* _.vimrc_\
|
|
|
Finally, we've added some configuration to vim, to make things a little more enjoyable here as well.\
|
|
|
First up, line-numbers and syntax highlighting, which should improve on legibility\
|
|
|
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! |
|
|
\ No newline at end of file |
|
|
## 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 |