How to handle bigger projects with FastAPI
What are FastAPI Routers?
In my previous blog post, I talked about FastAPI and how we can leverage it to quick build and prototype Python back-end APIs. In this piece, I would to tackle another aspect during development with FastAPI — handling bigger projects.
The first question that comes to mind is when do we need to branch out to different files and modules? Well, it pretty much depends on your service architecture and what it actually does. What that means is that you could divide your API into logical pieces and spread them across files/modules to increase the readability, reusability, etc.
Lets take a look at typical FastAPI project structure:
src/
main.py
config.py
utils.py
...
requirements.txt
CHANGELOG.md
README.md
.gitignore
What we have is a single src
(source) folder containing our application code. In this case we will focus on main.py
as this is the entry point to our service(s).
#main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/publicapi/v1/health")
async def get_health() -> dict:
"""Health check information about this service"""
return {
'Database': 'Status is up',
'Application:': 'Status is up',
'Networking': '0.99 ms latency'
}
if __name__ == '__main__'…