Skip to content

FastAPI-based backend with Docker, PostgreSQL, and MinIO. Includes asynchronous programming, testing with pytest, and code quality tools.

License

Notifications You must be signed in to change notification settings

nhesusrz/am_crm_service

Repository files navigation

AM CRM Service API

Unit Tests Status codecov Pre-commit Hooks Status

Development Overview

This project uses a modern stack for building and testing a robust backend application. I use FastAPI for fast, asynchronous web API development, with SQLAlchemy for database interactions, and PostgreSQL as the database. File storage is managed with MinIO, an S3-compatible storage service. The application is containerized using Docker and managed with Docker Compose. For development and testing, I employ pytest for testing with pytest-cov for coverage reports. Code quality tools like black and isort ensure code formatting and organization, while ruff helps with linting. Asynchronous programming is leveraged to handle multiple tasks concurrently, improving performance.

Installation

Navigate to http://localhost:8000/docs find documentation and resources to test the application locally.

Setting Up Pre-commit Hooks

Pre-commit hooks help ensure code quality by running checks and formatting tools before commits. Follow these steps to set up pre-commit hooks in your repository: Install the hooks defined in the .pre-commit-config.yaml file:

  pre-commit install

To run all configured hooks on all files, use:

  pre-commit run --all-files

To update the hooks to their latest versions, use:

  pre-commit autoupdate

Alembic Migrations

Auto generating migrations

  • Run alembic revision --autogenerate -m "migration message" to auto generate migrations.
  • Run alembic upgrade head to apply migrations.
  • Run alembic downgrade -1 to downgrade migrations.
  • NOTE: Do not modify the imports from alembic/env.py file.

Testing with Pytest

This project uses poetry for dependency management.

  1. cd am_crm_service
  2. poetry install
  3. poetry shell
  4. poetry run pytest

Project Setup and Usage

Prerequisites

Before deploying the application, make sure you have Docker and Docker Compose installed.

Installing Docker

Follow the instructions on the official Docker website to install Docker engine in your operating system.

Installing Docker Compose

Starting from Docker version 1.13, Docker Compose is included as part of Docker installations.

Installation Requirements for Earlier Versions

To install Docker Compose from the official repository:

  1. Download Docker Compose: Find the latest release version from the Docker Compose GitHub releases page.

    sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  1. Apply Executable Permissions: Make the Docker Compose binary executable.

    sudo chmod +x /usr/local/bin/docker-compose
  1. Verify Installation: Check that Docker Compose is installed correctly. You should see the version of Docker Compose that you installed.

    docker-compose --version

Deployment with Docker Compose

  1. Clone the repo https://github.com/nhesusrz/am_crm_service.git
  2.   cd am_crm_service
    
  3.   cp .env.example .env
    
  4. Run Docker Compose to build and start the services. This command will build the Docker images and start the containers of the application, PostgreSQL, and MinIO:

     docker compose up -d --build
    
    Or for earlier versions

     docker-compose up -d --build
    

Access the Application

The FastAPI application will be available at http://localhost:8000. Use this URL to interact with the API.

Accessing MinIO WebUI

To view and manage your buckets and their contents, you can access the MinIO WebUI: Open your web browser and go to http://127.0.0.1:9001.

Log in using the credentials specified in your .env file:

  • Access Key: <your-minio-access-key>
  • Secret Key: <your-minio-secret-key>

This interface allows you to view, create, and manage buckets and their contents.

Troubleshooting

  • Container Status: Check the status of your containers:

      docker compose ps
    
  • Logs: To view the logs for debugging, use:

      docker compose logs
    

Using Postman

To test the API, you can use Postman. Import the provided Postman collection file into Postman: Download Postman Collection

Cleaning Up Docker Resources

To clean up everything related to the containers, including volumes and images, follow these steps:

  1. Stop and remove containers:

     docker stop app_container minio_container postgres_container
    

     docker rm app_container minio_container postgres_container
    
  2. The volumes defined in your docker-compose.yml file are postgres_data and minio_data. To remove these volumes, use:

     docker volume rm postgres_data minio_data
    
  3. To remove the images postgres:latest and minio/minio use:

     docker rmi postgres:latest minio/minio
    

About

FastAPI-based backend with Docker, PostgreSQL, and MinIO. Includes asynchronous programming, testing with pytest, and code quality tools.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages