Real-life FastAPI applications are not just a single main.py
file. They often have a complex directory structure with separate files for models, routes, dependencies, and services. The goal of this project is to show an example of such a larger, structured FastAPI application and how it can be deployed using fastapi-serve
.
We're building a structured FastAPI application with a more complex directory structure. Here's how the directory is structured:
.
โโโ awesome_project
โ โโโ api
โ โ โโโ __init__.py
โ โ โโโ route1.py
โ โ โโโ route2.py
โ โ โโโ route3.py
โ โโโ dependencies
โ โ โโโ dependency1.py
โ โ โโโ dependency2.py
โ โ โโโ __init__.py
โ โโโ main.py
โ โโโ models
โ โ โโโ __init__.py
โ โ โโโ model1.py
โ โ โโโ model2.py
โ โโโ services
โ โโโ __init__.py
โ โโโ service1.py
โ โโโ service2.py
โโโ README.md
The awesome_project.main:app
part of the below fastapi-serve deploy
command is a string notation used in Python to denote a specific object within a module. Here's how to interpret it:
awesome_project.main
: This is the Python import path, pointing to the location of the Python file that contains your FastAPI application instance.app
: This is the actual FastAPI application object defined in your Python script. It's the instance of FastAPI that you've created, onto which you've loaded all your routes and middleware.
fastapi-serve deploy jcloud awesome_project.main:app
โญโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ App ID โ fastapi-a808d44495 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Phase โ Serving โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Endpoint โ https://fastapi-a808d44495.wolf.jina.ai โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ App logs โ https://cloud.jina.ai/ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Base credits (per hour) โ 10.104 (Read about pricing here) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Swagger UI โ https://fastapi-a808d44495.wolf.jina.ai/docs โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ OpenAPI JSON โ https://fastapi-a808d44495.wolf.jina.ai/openapi.json โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Let's use curl to test all 3 routes and validate that we get the expected responses.
curl -X 'GET' 'https://fastapi-a808d44495.wolf.jina.ai/route1/?arg1=some_value'
{
"name": "Route1",
"description": "Hello from Route 1 with dependency: {'arg1': 'some_value'}!"
}
curl -X 'POST' 'https://fastapi-a808d44495.wolf.jina.ai/route2/?arg2=other_value' \
-d '{
"id": 123,
"value": "some_value"
}'
{
"id": 123,
"value": "some_value",
"extra": {
"dep": {
"arg2": "other_value"
}
}
}
curl -X 'GET' 'https://fastapi-a808d44495.wolf.jina.ai/route3/'
{
"message": "Hello from Route 3! Result from Service: {'result': 'Processed some data in Service2'}"
}
This FastAPI project serves as an example of how a complex FastAPI application can be structured and deployed to the cloud using fastapi-serve. Stay tuned, as we'll be adding more structured FastAPI projects here!