FastAPI-PostgreSQL-Celery-RabbitMQ-Redis bakcend with Docker containerization

Overview

FastAPI - PostgreSQL - Celery - Rabbitmq backend

This source code implements the following architecture:

architecture

All the required database endpoints are implemented and tested. These include crud operations for dog and user PostgreSQL relations. The asynchronous tasks are queued via one endpoint, and the upload of files to guane internal test server (external API) is implemented as another endpoint.

This app also executes HTTP requests to another external endpoint located at https://dog.ceo/api/breeds/image/random which returns a message with an URL to a random dog picture. The URL is stored as the field picture in the dog relation.

Deploy using Docker

To deploy this project using docker make sure you have cloned this repository

$ git clone https://github.com/jearistiz/guane-intern-fastapi

and installed Docker.

Now move to the project root directory

$ mv guane-intern-fastapi

Unless otherwise stated, all the commands should be executed from the project root directory denoted as ~/.

To run the docker images just prepare your environment variables in the ~/.env file and run

$ docker compose up --build

If you have an older Docker version which does not support the $ docker compose command, make sure you install the docker-compose CLI, then run

$ docker-compose up --build

The docker-compose.yml is configured to create an image of the application named application/backend, an image of PostgreSQL v13.3 –named postgres–, an image of RabbitMQ v3.8 –rabbitmq– and an image of Redis v6.2 –reddis. To see the application working sound and safe, visit the URI 0.0.0.0:8080/docs or the equivalent URI you defined in the ~/.env file (use the format ${BACKEND_HOST}:${BACKEND_PORT}/docs) and start sending HTTP requests to the application via this nice interactive documentation view, brought to us automatically thanks to FastAPI integration with OpenAPI

architecture

In order to use the POST, PUT or DELETE endpoints you should first authenticate at the top right of the application docs (0.0.0.0:8080/docs) in the button that reads Authorize. I have set up this two super users for you to test these endpoints, use the one you feel more comfortable with ;)

user: guane
password: ilovethori

or

user: juanes
password: ilovecharliebot

If other fields are required in the authentication form, please leave them empty.

Notes on the database relations

Please consider the following notes when trying to make requests to the app:

  • The dog and user relations are connected by the field id_user defined in the dog relation (this is done via foreign key deifinition). Make sure the entity user with id = id_example is created before the dog with id_user = id_example.
  • If you want to manually define the id field in the user and dog relations, make sure there is no other entity within the relation with the same id.
    • The best thing is to just let the backend define the id fields for you, so just don't include these in the HTTP request when trying to insert or update the entities via POST or PUT methods.

Thats it for deploying and manually testing the endpoints.

Test the application inside Docker using pytest

If you want to run the tests inside the docker container, first open another terminal window and get the <Container ID> of the app/backend container using the command

$ docker ps

This command will list information about all your docker images but you are interested only in the one named app/backend.

Afterwards, run a bash shell using this command

$ docker exec -it <Container ID> bash

When you are already inside the container's bash shell, make sure you are located in the /app directory (you can check this if $ pwd prints out /app... if not, execute $ mv /app), and execute the following command:

$ python scripts/app/run_tests.py

There are some options for this testing initialization script (which underneath uses pytest) such as --cov-html which will generate an html report of the coverage testing. If you want to see all the options just run $ python scripts/app/run_tests.py --help. And test using your own options.

Deploy without using Docker

The application can also be deployed locally in your machine. It is a bit more dificult since we need to meet more requirements and setup some stuff first, but soon I will post more info on this here. Soon.

Requirements

NOTE: you may find the scripts used for server initialization invasive. Please take into account that this scripts might start and stop the appropriate servers (postgres, celery, rabbitmq, redis), and they might create and delete users in the mentioned services. Please review the scripts before executing them and execute them only under your own responsibility.

Code quality

The developement process has been carefully monitored using the sonarcloud engine available at https://sonarcloud.io/dashboard?id=jearistiz_guane-intern-fastapi. Flake8 linter has also been used thoroughly for code style and pytest to ensure the code is working as expected.

References

This app was developed using as main reference @tiangolo's FastAPI documentation and his Full stack, modern web application generator, which are both distributed under an MIT License. Some parts of this source code are literal code blocks from the cited references.

You might also like...
Stac-fastapi built on Tile38 and Redis to support caching

stac-fastapi-caching Stac-fastapi built on Tile38 to support caching. This code is built on top of stac-fastapi-elasticsearch 0.1.0 with pyle38, a Pyt

Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python 3.6 and above with performance auto-tuning. Optionally with Alpine Linux.
Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python 3.6 and above with performance auto-tuning. Optionally with Alpine Linux.

Supported tags and respective Dockerfile links python3.8, latest (Dockerfile) python3.7, (Dockerfile) python3.6 (Dockerfile) python3.8-slim (Dockerfil

FastAPI with Docker and Traefik

Dockerizing FastAPI with Postgres, Uvicorn, and Traefik Want to learn how to build this? Check out the post. Want to use this project? Development Bui

FastAPI + Postgres + Docker Compose + Heroku Deploy Template

FastAPI + Postgres + Docker Compose + Heroku Deploy ⚠️ For educational purpose only. Not ready for production use YET Features FastAPI with Postgres s

Sample FastAPI project that uses async SQLAlchemy, SQLModel, Postgres, Alembic, and Docker.

FastAPI + SQLModel + Alembic Sample FastAPI project that uses async SQLAlchemy, SQLModel, Postgres, Alembic, and Docker. Want to learn how to build th

Docker Sample Project - FastAPI + NGINX

Docker Sample Project - FastAPI + NGINX Run FastAPI and Nginx using Docker container Installation Make sure Docker is installed on your local machine

:rocket: CLI tool for FastAPI. Generating new FastAPI projects & boilerplates made easy.
:rocket: CLI tool for FastAPI. Generating new FastAPI projects & boilerplates made easy.

Project generator and manager for FastAPI. Source Code: View it on Github Features 🚀 Creates customizable project boilerplate. Creates customizable a

Simple FastAPI Example : Blog API using FastAPI : Beginner Friendly

fastapi_blog FastAPI : Simple Blog API with CRUD operation Steps to run the project: git clone https://github.com/mrAvi07/fastapi_blog.git cd fastapi-

Пример использования GraphQL Ariadne с FastAPI и сравнение его с GraphQL Graphene FastAPI

FastAPI Ariadne Example Пример использования GraphQL Ariadne с FastAPI и сравнение его с GraphQL Graphene FastAPI - GitHub ###Запуск на локальном окру

Comments
  • No Celery service on docker-compose.yml

    No Celery service on docker-compose.yml

    Steps

    git clone https://github.com/jearistiz/guane-intern-fastapi
    cd guane-intern-fastapi
    docker-compose up --build

    Expected behaviour

    Docker compose starts building all service images from docker-compose.yml

    Actual behaviour

    $ docker-compose up -d
    ERROR: Service 'backend_app' depends on service 'celery' which is undefined.
    

    So there is something I am missing or there is no celery service in docker-compose.yml which is required on backend-app service to work. See depends-on:

    version: "3.5"
    services:
      backend_app:
        build:
          context: .
          dockerfile: Dockerfile
        container_name: backend_app
        stop_signal: SIGINT
        env_file: .env
        image: app/backend
        depends_on: 
          - postgres
          - celery
    . . .
    
    opened by moracabanas 2
  • Investigate what happens when (or if it is even possible)

    Investigate what happens when (or if it is even possible)

    • [ ] different apps consume the same celery instance.
    • [ ] different apps consume the same rabbitmq instance.
    • [ ] different apps consume the same redis instance.
    question 
    opened by jearistiz 0
  • Change the db connection framework to one that is compatible with async calls

    Change the db connection framework to one that is compatible with async calls

    Consider:

    enhancement 
    opened by jearistiz 0
Owner
Juan Esteban Aristizabal
Physicist | Universidad de Antioquia | Python Enthusiast
Juan Esteban Aristizabal
High-performance Async REST API, in Python. FastAPI + GINO + Arq + Uvicorn (w/ Redis and PostgreSQL).

fastapi-gino-arq-uvicorn High-performance Async REST API, in Python. FastAPI + GINO + Arq + Uvicorn (powered by Redis & PostgreSQL). Contents Get Star

Leo Sussan 351 Jan 4, 2023
🚀 Cookiecutter Template for FastAPI + React Projects. Using PostgreSQL, SQLAlchemy, and Docker

FastAPI + React · A cookiecutter template for bootstrapping a FastAPI and React project using a modern stack. Features FastAPI (Python 3.8) JWT authen

Gabriel Abud 1.4k Jan 2, 2023
Full stack, modern web application generator. Using FastAPI, PostgreSQL as database, Docker, automatic HTTPS and more.

Full Stack FastAPI and PostgreSQL - Base Project Generator Generate a backend and frontend stack using Python, including interactive API documentation

Sebastián Ramírez 10.8k Jan 8, 2023
🚀 Cookiecutter Template for FastAPI + React Projects. Using PostgreSQL, SQLAlchemy, and Docker

FastAPI + React · A cookiecutter template for bootstrapping a FastAPI and React project using a modern stack. Features FastAPI (Python 3.8) JWT authen

Gabriel Abud 448 Feb 19, 2021
Boilerplate code for quick docker implementation of REST API with JWT Authentication using FastAPI, PostgreSQL and PgAdmin ⭐

FRDP Boilerplate code for quick docker implementation of REST API with JWT Authentication using FastAPI, PostgreSQL and PgAdmin ⛏ . Getting Started Fe

BnademOverflow 53 Dec 29, 2022
Cache-house - Caching tool for python, working with Redis single instance and Redis cluster mode

Caching tool for python, working with Redis single instance and Redis cluster mo

Tural 14 Jan 6, 2022
Simple example of FastAPI + Celery + Triton for benchmarking

You can see the previous work from: https://github.com/Curt-Park/producer-consumer-fastapi-celery https://github.com/Curt-Park/triton-inference-server

Jinwoo Park (Curt) 37 Dec 29, 2022
REST API with FastAPI and PostgreSQL

REST API with FastAPI and PostgreSQL To have the same data in db: create table CLIENT_DATA (id SERIAL PRIMARY KEY, fullname VARCHAR(50) NOT NULL,email

Luis Quiñones Requelme 1 Nov 11, 2021
A FastAPI Framework for things like Database, Redis, Logging, JWT Authentication and Rate Limits

A FastAPI Framework for things like Database, Redis, Logging, JWT Authentication and Rate Limits Install You can install this Library with: pip instal

Tert0 33 Nov 28, 2022
Redis-based rate-limiting for FastAPI

Redis-based rate-limiting for FastAPI

Glib 6 Nov 14, 2022