☰
Current Page
Main Menu
Home
Home
Editing FastAPI Scalable Project Structure with Docker compose
Edit
Preview
H1
H2
H3
default
Set your preferred keybinding
default
vim
emacs
markdown
Set this page's format to
AsciiDoc
Creole
Markdown
MediaWiki
Org-mode
Plain Text
RDoc
Textile
Rendering unavailable for
BibTeX
Pod
reStructuredText
Help 1
Help 1
Help 1
Help 2
Help 3
Help 4
Help 5
Help 6
Help 7
Help 8
Autosaved text is available. Click the button to restore it.
Restore Text
https://nabajyotiborah.medium.com/fastapi-scalable-project-structure-with-docker-compose-45dc3a9fb4c6 # FastAPI Scalable Project Structure with Docker compose | by Nabajyoti Borah | Medium  [Nabajyoti Borah](https://nabajyotiborah.medium.com/) 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_](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 1. 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](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 ------------------------- 1. 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!!_
Uploading file...
Edit message:
Cancel