Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

CarlosAndreo/fastapi-mongodb

Repository files navigation

🧠 About

This is a ready-to-use project template for building FastAPI applications with MongoDB as the database. It's designed to help you get started quickly with a clean architecture, pre-configured settings, and best practices for scalability and maintainability.

πŸ“ Project Structure

fastapi-mongodb/
β”œβ”€β”€ app/
β”‚ β”œβ”€β”€ core/ # Core configuration and utilities
β”‚ β”‚ β”œβ”€β”€ config.py # Environment variables and configuration
β”‚ β”‚ β”œβ”€β”€ constants.py # Application constants
β”‚ β”‚ β”œβ”€β”€ jwt.py # JWT token handling
β”‚ β”‚ β”œβ”€β”€ logger.py # Logging configuration
β”‚ β”‚ └── security.py # Security utilities
β”‚ β”œβ”€β”€ database/
β”‚ β”‚ β”œβ”€β”€ init-db.js # MongoDB initialization script
β”‚ β”‚ └── mongodb.py # MongoDB connection and configuration
β”‚ β”œβ”€β”€ middlewares/
β”‚ β”‚ └── logging.py # Logging middleware
β”‚ β”œβ”€β”€ repositories/
β”‚ β”‚ └── user.py # User data access layer
β”‚ β”œβ”€β”€ routers/
β”‚ β”‚ β”œβ”€β”€ auth.py # Authentication endpoints
β”‚ β”‚ └── me.py # Current user endpoints
β”‚ β”œβ”€β”€ schemas/
β”‚ β”‚ └── user.py # Pydantic user schemas
β”‚ β”œβ”€β”€ services/
β”‚ β”‚ β”œβ”€β”€ auth.py # Authentication business logic
β”‚ β”‚ └── user.py # User business logic
β”‚ └── main.py # Application entry point
β”œβ”€β”€ docker/
β”‚ └── fastapi/
β”‚ └── Dockerfile # FastAPI Docker image
β”œβ”€β”€ tests/
β”‚ β”œβ”€β”€ conftest.py # Pytest configuration
β”‚ β”œβ”€β”€ test_auth.py # Authentication tests
β”‚ └── test_me.py # User endpoints tests
β”œβ”€β”€ .env.template # Environment variables template
β”œβ”€β”€ docker-compose.yaml # Docker Compose configuration
β”œβ”€β”€ pyproject.toml # Project dependencies and configuration
β”œβ”€β”€ pytest.toml # Pytest configuration
└── ruff.toml # Linter configuration

Features

  • JWT-based authentication.
  • Refresh token.
  • Middleware.
  • Tests with pytest.
  • Log handler.
  • Indexes to MongoDB.

Roadmap

  • Email verification.
  • Handler exceptions.

πŸ› οΈ Stack

  • Python - Programming language.
  • FastAPI - Python framework for web applications to expose the API.
  • MongoDB - NoSQL database.

πŸš€ Getting Started Locally

Install Docker following the instructions for your operating system:

Clone the repository:

git clone https://github.com/CarlosAndreo/fastapi-mongodb.git
cd fastapi-mongodb

Create the .env file from the template:

cp .env.template .env

Note

Edit the .env file and update the values according to your needs. Make sure to set a strong SECRET_KEY (you can generate one with openssl rand -hex 32).

Build the Docker image:

docker compose build

Run the Docker container:

docker compose up -d

The application will be available at http://localhost:8000/api/v1/docs.

The mongo-express UI will be available at http://localhost:8081.

πŸ§ͺ Test

Warning

When run the tests, the database is cleaned.

To test the application, access to the fastapi container:

docker exec -it fastapi-mongodb sh

Finally, execute:

uv run pytest

Contributors

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /