project/
│
├── .venv/ # Virtual environment
│
├── apps/ # Application-specific modules
│ ├── user/ # User-related module
│ │ ├── models.py # MongoDB models (e.g., User model)
│ │ ├── repository.py # Data access logic (CRUD operations)
│ │ ├── route.py # Routes for user-related endpoints
│ │ ├── schema.py # Pydantic schemas for User validation
│ │ └── services/ # Services directory (business logic)
│ │ └── auth_service.py # Authentication logic
│ │
│ └── todo/ # Todo-related module
│ ├── models.py # MongoDB models (e.g., Todo model)
│ ├── repository.py # Data access logic (CRUD operations)
│ ├── route.py # Routes for todo-related endpoints
│ └── schema.py # Pydantic schemas for Todo validation
│
├── config.py # Configuration settings for the project
├── main.py # FastAPI application entry point
├── requirements.txt # List of project dependencies
├── .env # Environment variables (should not be in version control)
├── .gitignore # Git ignore file to exclude unwanted files/folders
└── README.md # Project documentation
- Installation
- Running the Project
- API Endpoints
- Authentication
- Environment Variables
- Database
- Testing
- Contributing
- License
Ensure the following are installed on your system:
- Python 3.6 or higher
- MongoDB (it can be cloud also)
-
Clone the repository
Clone the project repository to your local machine:git clone https://github.com/your-repo-name.git cd your-repo-name -
Create and Activate virtual enviroment
To do that run the following command# Create virtual enviroment python -m venv .venv # Acticate virtual enviroment source .venv/bin/activate # on mac and linux venv\Scripts\activate # on windows
-
Install dependencies
Install the required Python packages:pip install -r requirements.txt
Once the installation is complete, follow these steps to run the FastAPI application:
- Start the server
Useuvicornto start the FastAPI server:uvicorn main:app --reload
- The
--reloadflag enables automatic code reload during development.
-
Access the application
By default, access point is:- Base URL
127.0.0.1:8000
- Base URL
-
Explore Api documentations
Fast api provides interactive API documentation at these endpoints:- Swagger UI : http://127.0.0.1:8000/docs
- Redoc UI : http://127.0.0.1:8000/redoc
Use the --port flag to run the server on a custom port:
uvicorn main:app --reload --port 8080POST /auth/signup
- Description: Create a new user.
- Parameters: None
- Request body:
{
"username": "string",
"password": "string"
}- Response:
{
"username":
}POST /auth/login
- Description: Login as user.
- Parameters: None
- Request body:
{
"username": "string",
"password": "string"
}- Response:
{
"username":
}- Mechanism: Bearer Token (JWT)
- Endpoints:
POST /auth/loginto get token- Use the token in the
Authorizationheader:
Authorization: Bearer <your_token>
Create a .env file in the root directory with the following keys:
MONGO_URL="mongodb://localhost:27017/"
DB_NAME="db_name"
# JWT Configuration
JWT_SECRET="mysecrect"
JWT_ALGORITHM="Algorithm"
JWT_EXPIRY=30 # Can be "30m", "1h", "15m", etc.
- Config the settings using
.envfile
- Database Type: MongoDB
- Driver:
motor - Setup: Use database
URIfrom.envfile