Skip to content
GitLab
  • Menu
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

Part 2a. Extending my first service · Changes

Page history
Update Part 2a. Extending my first service authored Oct 30, 2021 by Jens Melgard Churchill's avatar Jens Melgard Churchill
Hide whitespace changes
Inline Side-by-side
Part-2a.-Extending-my-first-service.md
View page @ b9ca79bc
......@@ -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
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