FastAPI/FastAPI Scalable Project Structure with Docker compose.md
... ...
@@ -0,0 +1,118 @@
1
+https://nabajyotiborah.medium.com/fastapi-scalable-project-structure-with-docker-compose-45dc3a9fb4c6
2
+
3
+# FastAPI Scalable Project Structure with Docker compose | by Nabajyoti Borah | Medium
4
+
5
+![Nabajyoti Borah](https://miro.medium.com/v2/da:true/resize:fill:88:88/0*hfer9bj-1oDIG727)
6
+[Nabajyoti Borah](https://nabajyotiborah.medium.com/)
7
+
8
+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.
9
+
10
+Features of FastAPI
11
+-------------------
12
+
13
+**_1_**_. The key features provided in the framework are_ **_Fast To code, Robust, Easy, Short,_** _and many more_
14
+
15
+**_2_**_. With help of FastAPI you can easily deploy your machine learning models on the go on any server_
16
+
17
+**_3_**_. FastAPI uses_ **_Swagger UI_** _and_ **_ReDoc_** _for automated API documentation_
18
+
19
+**_4_**_. All the validations are handled by the well-established and robust_ **_Pydantic_**.
20
+
21
+**5**. **_Async await_** _is one of the features which makes FastAPI one of the modern frameworks_
22
+
23
+_Github Repo:_ [_https://github.com/Nabajyoti4/Todo-FastAPI_](https://github.com/Nabajyoti4/Todo-FastAPI)
24
+
25
+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.
26
+
27
+Let's start with creating a fast API project to create daily Todo’s
28
+
29
+1. Create a new project and activate the virtual environment on it and also install the dependencies needed for fastAPI
30
+
31
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*ZP1bENuFisyv4wEgxzUM-w.png)
32
+
33
+2\. Now let's create the necessary files needed for the project
34
+
35
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*Hhka_d_A9ZAqM1nuDZDqQg.png)
36
+
37
+`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
38
+
39
+`database.py` file will contain the connection with the database you want
40
+
41
+`config.py` file will have the configurations necessary for the project
42
+
43
+`main.py` file is the root level file of our project from where the project will run
44
+
45
+`schemas.py` file will have the schema pydantic schema for the Post requests
46
+
47
+`model.py` file will contain the models needed for auth and todo
48
+
49
+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.
50
+
51
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*cLexvSJm7Co2VCJEYGYPNw.png)
52
+
53
+4\. Now let's complete our database connection in the database.py file
54
+
55
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*inHMMgCv3bEN9J5UZswB6Q.png)
56
+
57
+5\. Create the schemas for the POST request now in schemas.py
58
+
59
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*tkBq7PjTTjhHZ_HLep86Ag.png)
60
+
61
+6\. Now create a .env file and add these env variables in the env file which are used in `config.py`
62
+
63
+_FastAPI internally used the dotenv module of python to fetch the env variables from the .env file_
64
+
65
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*tkBq7PjTTjhHZ_HLep86Ag.png)
66
+
67
+7\. Now let's create the apps in the routers folder, first start with auth app
68
+
69
+First import the modules which are needed here
70
+
71
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*1oxBFB1UAbct0iGmaWUp-A.png)
72
+
73
+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
74
+
75
+Now lets create a DB session function to connect with the database
76
+
77
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*mHC-qJ-94leTjYCbNSKddg.png)
78
+
79
+Now let's create the API needed to login and create a user
80
+
81
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*WJdp8_7FDrNHPBcZ7tmwFw.png)
82
+
83
+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
84
+
85
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*FzoAnbdWDSj_cLFb2MBhDg.png)
86
+
87
+9\. Now finally complete the `main.py` file to integrate all the apps and also database connection
88
+
89
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*HbvUhA_XzJ9VMWZC2W6CIg.png)
90
+
91
+So now we have our whole project ready lets run the project
92
+
93
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*kSBBFSjw9Y2IAwXIpk7Nkg.png)
94
+
95
+Here note that main is the name of the main file and app is the instance of fastapi created inside the main file.
96
+
97
+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
98
+
99
+Dockerize the application
100
+-------------------------
101
+
102
+1. Create a Dockerfile in the root folder only
103
+
104
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*IdZ7rqGC38Y6EKi-Ts-96Q.png)
105
+
106
+This will create a docker image of our application
107
+
108
+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
109
+
110
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*wf9SGrXq6gcIZaO5zxvNkQ.png)
111
+
112
+Now let's run the project
113
+
114
+> For future scope i will upload a new blog for fastAPI with SqlModel as its ORM
115
+>
116
+> _I will be posting more blogs with_ **_FastAPI_** _,_ **_Tensorflow and AWS_** _as the major content, and hope you will support me further_
117
+>
118
+> _Thank for reading. Cheers!!_
... ...
\ No newline at end of file