Organising our files
You are doing great and as we'll soon see, the possibilities are out of bounds.
But first let's make sure we are all on the same page.
Run git checkout -f Step-1
everyone should now be at the same PTF level...
Time for some action, just a fraction of friction
If your not used to working in a bash shell, this might seem overly complicated,
and if you've never used vim before, it might be downright terrifying!
The branch you just pulled will try and alleviate it the pain, taking us from grit 80 sandpaper to (hopefully) very little friction.
Try and list the folder content, by typing ls -la
...
You'll note a lot of new files have appeared, of note are:
-
requirements.txt
This file lists the packages our project requires, and their expected version.
So instead of pip installing everything, we'll run simply runpip install -r requirements.txt
and it will install what ever is needed.
You can create this fill, by getting pip to list all dependencies in your project, and saving it to the requirements file.
Simply runpip freeze >requirements.txt
-
start.sh
Going forward, we don't want to bother with all this installing, running, backgrounding and stopping nonsense.
The bash shell-scriptstart.sh
will handle all the pain.
If there is no virtual environment, it will create it, install the required packages (using the above file),
test if the FastAPI service is running, and depending on what is required start or restart it. -
stop.sh
The bash shell-scriptstop.sh
will stop the service if it is running, it is used bystart.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 doingview.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 thestart.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 the 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 7 ( 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
Exit insert-mode by pressing Esc
, and save the file using :w
The server will auto-reload your changes within 5 seconds
Note: For the impatient out there, the issue can be forced by pressing F5 in vim
Change to your browser window, and refresh the page from before by pressing F5
Note: While the server reloads the changes, the browser might display "bad gateway" or "unreachable site" messages.