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.

Owner
Juan Esteban Aristizabal
Physicist | Universidad de Antioquia | Python Enthusiast
Juan Esteban Aristizabal
A Sample App to Demonstrate React Native and FastAPI Integration

React Native - Service Integration with FastAPI Backend. A Sample App to Demonstrate React Native and FastAPI Integration UI Based on NativeBase toolk

YongKi Kim 4 Nov 17, 2022
Prometheus exporter for several chia node statistics

prometheus-chia-exporter Prometheus exporter for several chia node statistics It's assumed that the full node, the harvester and the wallet run on the

30 Sep 19, 2022
Github timeline htmx based web app rewritten from Common Lisp to Python FastAPI

python-fastapi-github-timeline Rewrite of Common Lisp htmx app _cl-github-timeline into Python using FastAPI. This project tries to prove, that with h

Jan Vlčinský 4 Mar 25, 2022
Prometheus integration for Starlette.

Starlette Prometheus Introduction Prometheus integration for Starlette. Requirements Python 3.6+ Starlette 0.9+ Installation $ pip install starlette-p

José Antonio Perdiguero 229 Dec 21, 2022
Flask-vs-FastAPI - Understanding Flask vs FastAPI Web Framework. A comparison of two different RestAPI frameworks.

Flask-vs-FastAPI Understanding Flask vs FastAPI Web Framework. A comparison of two different RestAPI frameworks. IntroductionIn Flask is a popular mic

Mithlesh Navlakhe 1 Jan 01, 2022
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
✨️🐍 SPARQL endpoint built with RDFLib to serve machine learning models, or any other logic implemented in Python

✨ SPARQL endpoint for RDFLib rdflib-endpoint is a SPARQL endpoint based on a RDFLib Graph to easily serve machine learning models, or any other logic

Vincent Emonet 27 Dec 19, 2022
Twitter API monitor with fastAPI + MongoDB

Twitter API monitor with fastAPI + MongoDB You need to have a file .env with the following variables: DB_URL="mongodb+srv://mongodb_path" DB_URL2=

Leonardo Ferreira 3 Apr 08, 2022
FastAPI native extension, easy and simple JWT auth

fastapi-jwt FastAPI native extension, easy and simple JWT auth

Konstantin Chernyshev 19 Dec 12, 2022
CURSO PROMETHEUS E GRAFANA: Observability in a real world

Curso de monitoração com o Prometheus Esse curso ensina como usar o Prometheus como uma ferramenta integrada de monitoração, entender seus conceitos,

Rafael Cirolini 318 Dec 23, 2022
A simple Blogging Backend app created with Fast API

This is a simple blogging app backend built with FastAPI. This project is created to simulate a real CRUD blogging system. It is built to be used by s

Owusu Kelvin Clark 13 Mar 24, 2022
Town / City geolocations with FastAPI & Mongo

geolocations-api United Kingdom Town / City geolocations with FastAPI & Mongo Build container To build a custom image or extend the api run the follow

Joe Gasewicz 3 Jan 26, 2022
Cookiecutter template for FastAPI projects using: Machine Learning, Poetry, Azure Pipelines and Pytests

cookiecutter-fastapi In order to create a template to FastAPI projects. 🚀 Important To use this project you don't need fork it. Just run cookiecutter

Arthur Henrique 225 Dec 28, 2022
SQLAlchemy Admin for Starlette/FastAPI

SQLAlchemy Admin for Starlette/FastAPI SQLAdmin is a flexible Admin interface for SQLAlchemy models. Main features include: SQLAlchemy sync/async engi

Amin Alaee 683 Jan 03, 2023
Ready-to-use and customizable users management for FastAPI

FastAPI Users Ready-to-use and customizable users management for FastAPI Documentation: https://fastapi-users.github.io/fastapi-users/ Source Code: ht

FastAPI Users 2.3k Dec 30, 2022
An extension library for FastAPI framework

FastLab An extension library for FastAPI framework Features Logging Models Utils Routers Installation use pip to install the package: pip install fast

Tezign Lab 10 Jul 11, 2022
A utility that allows you to use DI in fastapi without Depends()

fastapi-better-di What is this ? fastapi-better-di is a utility that allows you to use DI in fastapi without Depends() Installation pip install fastap

Maxim 9 May 24, 2022
A rate limiter for Starlette and FastAPI

SlowApi A rate limiting library for Starlette and FastAPI adapted from flask-limiter. Note: this is alpha quality code still, the API may change, and

Laurent Savaete 562 Jan 01, 2023
Mixer -- Is a fixtures replacement. Supported Django, Flask, SqlAlchemy and custom python objects.

The Mixer is a helper to generate instances of Django or SQLAlchemy models. It's useful for testing and fixture replacement. Fast and convenient test-

Kirill Klenov 871 Dec 25, 2022
A request rate limiter for fastapi

fastapi-limiter Introduction FastAPI-Limiter is a rate limiting tool for fastapi routes. Requirements redis Install Just install from pypi pip insta

long2ice 200 Jan 08, 2023