FastAPI Scalable Project Structure with Docker compose
FastAPI is a modern, fast (high-performance) on par with Nodejs and GO, web framework for building REST APIs in python language. With FastAPI anyone can start building Rest APIs with ease and it can connect to any SQL or NoSql database you want.
Features of FastAPI
1. The key features provided in the framework are Fast To code, Robust, Easy, Short, and many more
2. With help of FastAPI you can easily deploy your machine learning models on the go on any server
3. FastAPI uses Swagger UI and ReDoc for automated API documentation
4. All the validations are handled by the well-established and robust Pydantic.
5. Async await is one of the features which makes FastAPI one of the modern frameworks
Github Repo: https://github.com/Nabajyoti4/Todo-FastAPI
Today we are going to create a FastAPI project in a structured manner that can be easily scaled when needed, for that we are going to use the FastAPI routers module. The project will also contain JWT token authentication and database connection with SqlAlchemy.
Let's start with creating a fast API project to create daily Todo’s
- Create a new project and activate the virtual environment on it and also install the dependencies needed for fastAPI
2. Now let's create the necessary files needed for the project
router
folder will contain all the apps which are going to be used in our project, each app file will contain all the APIs needed for that app
database.py
file will contain the connection with the database you want
config.py
file will have the configurations necessary for the project
main.py
file is the root level file of our project from where the project will run
schemas.py
file will have the schema pydantic schema for the Post requests
model.py
file will contain the models needed for auth and todo
3. Let's start with creating the models for our auth and todo, we are using sqlalchemy orm for this. Here we have users
and todos
table and they have foreign key relation with each other.
4. Now let's complete our database connection in the database.py file
5. Create the schemas for the POST request now in schemas.py
6. Now create a .env file and add these env variables in the env file which are used in config.py
FastAPI internally used the dotenv module of python to fetch the env variables from the .env file
7. Now let's create the apps in the routers folder, first start with auth app
First import the modules which are needed here
Here we are using the APIRouter module of fastAPI to create the API in this file and import them in the main file easily , we can define prefix or tags also here if needed
Now lets create a DB session function to connect with the database
Now let's create the API needed to login and create a user
8. Now lets complete the API for todos app , here in the todos.py file each API request is first authenticated for the current login user and then the request is processed
9. Now finally complete the main.py
file to integrate all the apps and also database connection
So now we have our whole project ready lets run the project
Here note that main is the name of the main file and app is the instance of fastapi created inside the main file.
Now open Uvicorn running on `http://127.0.0.1:8000/docs` and you will get a full documented Swagger UI which is like a postman only to send post requests or get requests from browser only
Dockerize the application
- Create a Dockerfile in the root folder only
This will create a docker image of our application
2. Create a docker-compose.yaml
file now, here we will map the docker container port with our local port 8000 so that we can access the running fastAPI application from the docker container. And also define the .env file here only so that our application can get the environment variables from env file
Now let's run the project
For future scope i will upload a new blog for fastAPI with SqlModel as its ORM
I will be posting more blogs with FastAPI , Tensorflow and AWS as the major content, and hope you will support me further
Thank for reading. Cheers!!