This is a RESTful API built using Python and FastAPI. The application leverages Docker for containerization, MongoDB as the database, pyenv, poetry for managing Python versions, and Swagger for API documentation and testing.
This REST API serves as a robust backend solution using FastAPI and MongoDB. It facilitates key functionalities including CRUD operations, user authentication, data validation, and seamless interaction between the FastAPI framework and MongoDB database.
- Python version 3.12.x (LTS)
- MongoDB
- Poetry
- Docker
- Docker Compose
- Pyenv (optional, for managing Python versions)
Follow these steps to set up and run the application:
-
Clone the repository:
git clone https://github.com/pashamakhilkumarreddy/fastapi-mongodb
-
Change into the project directory:
cd fastapi-mongodb
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
or
pyenv local 3.12.4 poetry shell
-
Create a
.env
file in the project root directory and add the required environment variables (see.env.example
as a reference):cp .env.example .env
-
Install the dependencies:
pip install -r requirements.txt
or
poetry install
-
Install pre-commit hooks:
-
Install
pre-commit
: https://pre-commit.com/ (should be installed globally) -
Install pre-commit hooks:
make install-git-hooks
-
Run the application:
uvicorn app.main:app --reload
-
For additional details, please refer to the Makefile for this project. It includes various commands to manage the setup, running, and maintenance of the application.
This application uses MongoDB as its database. Follow these steps to set up and configure the database for the FastAPI project:
-
Ensure that you have MongoDB installed on your machine. You can download it from MongoDB Downloads.
-
Start the MongoDB server by running the following command:
mongod
-
Connect to MongoDB and create a new database and user for the FastAPI project. You can run these commands in the MongoDB shell (
mongo
):# Connect to MongoDB shell mongo # Switch to the new database (it will be created if it doesn't exist) use fs-mongo; # Create a new user with roles db.createUser({ user: "admin", pwd: "your_password", roles: [{ role: "readWrite", db: "fs-mongo" }] }); # Exit the MongoDB shell exit;
This setup will create a MongoDB database named fs-mongo
and a user named admin
with read and write privileges on the fs-mongo
database.
This project includes Docker Compose files for production and staging environments. Before using Docker, ensure you have the required environment variables set in the corresponding .env
files (see .env.example
as a reference).
To build and run Docker containers:
-
Ensure Docker is installed and running on your system.
-
Build and run the MongoDB Docker image:
docker run -it -p 27017:27017 -d mongo
-
Build and run the API using Docker Compose:
docker-compose up
-
To stop and clean Docker containers, use the following command:
docker-compose down
Access the Swagger UI at Swagger API Docs.
├── .github
├── nginx
├── app
│ ├── config
│ ├── helpers
│ ├── models
│ ├── routes
│ ├── schemas
│ ├── services
│ ├── main.py
├── .commitlintrc.json
├── .dockerignore
├── .editorconfig
├── .env
├── .env.example
├── .pre-commit-config.yaml
├── .secrets.baseline
├── CONTRIBUTING.md
├── docker-compose.yml
├── Dockerfile
├── LICENSE
├── Makefile
├── poetry.lock
├── pyproject.toml
├── README.md
├── requirements.txt
This repository follows a structured organization to facilitate clarity, maintainability, and scalability. Below is an overview of the key directories and files:
nginx
: Configuration files for NGINX, if applicable.app
: The main source code directory containing the application's codebase.config
: Contains additional configuration files for the application.helpers
: Helper functions and utilities.models
: Database entities representing data models.routes
: FastAPI route definitions.schemas
: Pydantic schemas for data validation.services
: Service layer for business logic.main.py
: The entry point of the application.
static
: Static files such as images, if applicable.tests
: Unit and integration tests for the application..commitlintrc.json
: Configuration file for Commitlint..dockerignore
: Files and directories to be ignored when building Docker images..editorconfig
: Configuration for maintaining consistent coding styles across different editors..env
and.env.example
: Environment variables configuration..gitignore
: Files and directories to be ignored by Git..pre-commit-config.yaml
: Configuration for pre-commit hooks.CONTRIBUTING.md
: Guidelines for contributing to the project.docker-compose.yml
: Docker Compose files for production and development.Dockerfile
: Dockerfile for building Docker images.Makefile
: Contains various commands for setting up, building, and managing the project.poetry.lock
: Poetry lock file that locks the dependencies to specific versions.pyproject.toml
: Configuration file for Poetry, specifying project dependencies and metadata.README.md
: This file contains information about the project and instructions on how to use it.requirements.txt
: List of dependencies for the project, used by pip for installing the necessary packages.
To run tests, use the following command:
poetry run pytest
To run ESLint, use the following command:
poetry run blue .
Linting and formatting are enforced through the pre-commit hook using Husky and lint-staged. Make sure to address any issues reported by the hook. If you want to temporarily disable pre-commit hooks, you can use the --no-verify
or -n
option with your Git commit command. This option skips the pre-commit and commit-msg hooks for that specific commit.
git commit --no-verify -m "Your commit message"
We welcome contributions to enhance the functionality of this API. To contribute, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature
). - Implement your changes.
- Test your changes thoroughly.
- Commit your changes (
git commit -am 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature
). - Create a new Pull Request.
For additional contributing guidelines, see the Contributing guide.
This project is licensed under the [License Name]. See the LICENSE file for details.