A boilerplate FastAPI project.
.
├── main
│ ├── controllers // Controller layer - listing endpoints of each router
│ ├── engines // Data access layer - CRUD files to interact with DB
│ ├── models // Entity layer - defining table schemas
│ ├── schemas // DTO layer - schema for request body & response of endpoints
│ ├── services // Service layer - defining business logics
│ ├── config.py // Application configs and env parameters
│ └── __init__.py // FastAPI application creation and configuration
├── .env // Hidden env file
├── env.example // Example env variables
├── README.md
└── requirements.txt // Necessary packages
1.1. Install PostgresDB
- Download PostgresDB from https://www.postgresql.org/download/
1.2. Create new database
createdb <DATABASE_NAME>
2.1. Install Python
- Download Python from https://www.python.org/downloads/
- Check python version
python3 --version
2.2. Open project
- Clone the project
git clone https://github.com/nguyenhn1299/sample-fastapi.git
- Open it in any IDE
2.3. Create virtual environment for the project.
We want to create a project-specific environment. So packages installed across different projects are not conflicted in versions. In the root directory of this project, run:
python3 -m venv env
New folder env
will be created in the root directory
2.4. Activate virtual environment
source env/bin/activate
2.5. Install necessary packages
pip install -r requirements.txt
2.6. Set up server environment variables
Typically, the service needs dynamic values or secret values that shouldn't be disclosed publicly. Those variables are stored in the .env
file and are ignored by git. To configure one locally,
- Create new file named
.env
- Fill out values of those environment variables listed in
env.example
file
Start the web service by running:
uvicorn main:app --reload --port 5000
--reload
flag indicates that the service will be reloaded whenever any change is detected in codebase
The service URL is http://127.0.0.1:5000.
FastAPI auto generates interface description for HTTP APIs.
The API description is available at http://127.0.0.1:5000/redoc or http://127.0.0.1:5000/docs. To interact with APIs, use /docs
Note: When running, the service will auto convert all logical ORMs declared in models
folder to physical tables in PostgresDB.