Welcome to the FastAPI Base Project! This foundational template is designed for building API services using FastAPI, MongoDB, and Docker. It provides a robust starting point with functionalities for user management, task handling, authentication, and authorization.
- FastAPI: Utilizes FastAPI for high-performance, asynchronous APIs with automatic interactive documentation (Swagger UI).
- MongoDB: Uses MongoDB as the NoSQL database, ensuring flexible and scalable data storage.
- Docker: Simplifies development, deployment, and scaling through containerization.
- CRUD User Management: Includes complete Create, Read, Update, and Delete operations for user management.
- Task Module: Features a module for creating, updating, and tracking tasks.
- Authentication and Authorization:
- Two User Roles: Supports 'user' and 'admin' roles with different levels of access.
- Secure Authentication: Implements secure authentication mechanisms to verify user identities.
- Role-Based Authorization: Ensures that users can only access data relevant to them and perform allowed actions. Admins have unrestricted access to all operations and data.
First, clone the repository from GitLab and navigate into the project directory:
git clone https://github.com/ellyx13/fastapi-base-project.git
cd fastapi-base-project
- Create a folder named
.env
:
mkdir .env
- Create a file named
dev.env
inside the.env
folder and fill it with the following content. You can get theSECRET_KEY
from this link by selectingHS512
and clicking "Generate JWS Keys". Then, copy theSharedSecret
:
environment=dev
app_database_name=app
database_url=mongodb://db?retryWrites=true
# Authentication
SECRET_KEY={ENTER_YOUR_SECRET_KEY}
ALGORITHM="HS512"
DEFAULT_ADMIN_EMAIL={ENTER_YOUR_DEFAULT_EMAIL}
DEFAULT_ADMIN_PASSWORD={ENTER_YOUR_DEFAULT_PASSWORD}
- Create a file named
test.env
inside the.env
folder, and add the following content:
ENVIRONMENT=test
database_url=mongodb://db-test?retryWrites=true
- Download pre-commit:
pip install pre-commit
- Install pre-commit
pre-commit install
To start the project on a Linux machine:
- Open your terminal.
- Navigate to the project directory.
- Run the following command to make the script executable and start the Docker containers:
chmod +x bin/linux/start.sh
bin/linux/start.sh
To start the project on a Windows machine:
- Open Command Prompt or PowerShell.
- Navigate to the project directory.
- Run the following command to start the Docker containers:
bin\windows\start.bat
This script will build and start the Docker containers for the FastAPI application and MongoDB database.
Once the containers are up and running, you can access the FastAPI application by navigating to:
http://localhost:8005
You can explore the automatically generated API documentation at:
- Swagger UI: http://localhost:8005/docs
- ReDoc: http://localhost:8005/redoc
To stop the Docker containers, use the following commands based on your operating system:
Press Ctrl + C
This will stop and remove the containers, networks, and volumes created by Docker Compose.