Skip to content
GitLab
  • Menu
    • Projects Groups Snippets
      Help
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • F FastAPI
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Deployments
    • Deployments
    • Releases
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Wiki
    • Wiki
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • Sitemule
  • Public
  • workshops
  • open source on IBM i
  • FastAPI
  • Wiki
  • Part 2a. Extending my first service
Last edited by Jens Melgard Churchill 3 years ago
Page history
This is an old version of this page. You can view the most recent version or browse the history.

Part 2a. Extending my first service

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 run pip 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 run pip 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-script start.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-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
  • .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!

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.

Clone repository
  • Part 1a. My first service
  • Part 1b. Testing my first service
  • Part 2a. Extending my first service
  • Part 2b. Organise my first service
  • Part 3a. Review my first service
  • Part 3b. Connecting my first service
  • Home

Menu

Projects Groups Snippets
Help