Skip to content
This repository was archived by the owner on Nov 15, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.*
!.local/*
docker-compose.yml
Dockerfile
README.md
Makefile
14 changes: 14 additions & 0 deletions .env.development.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
MONGO_INITDB_DATABASE=mapi
MONGO_INITDB_ROOT_USERNAME=user
MONGO_INITDB_ROOT_PASSWORD=pass

DATABASE_HOST=mapi_mongodb
DOCKER_MONGODB_PORT=27017

MONGODB_ENABLE_ADMIN=true
MONGODB_URL=mongodb://$MONGO_INITDB_ROOT_USERNAME:$MONGO_INITDB_ROOT_PASSWORD@$DATABASE_HOST:$DOCKER_MONGODB_PORT/$MONGO_INITDB_DATABASE

RABBITMQ_DEFAULT_USER=user
RABBITMQ_DEFAULT_PASS=pass
RABBITMQ_HOST=mapi_rabbitmq
RABBITMQ_PORT=5672
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
reports/

init-mongodb/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
59 changes: 59 additions & 0 deletions .local/entrypoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

setup_ready() {
python << END
import sys
import os
import pika
from pymongo import MongoClient

mongodb_url = os.getenv("MONGODB_URL", "Nothing was set")

try:
print ("Checking MongoDB")
client = MongoClient(host = [mongodb_url], serverSelectionTimeoutMS = 2000)
client.server_info()
print ("Mongo OK")
except Exception as e:
print (e)
sys.exit(-1)

rabbitmq_user = os.getenv("RABBITMQ_DEFAULT_USER", "Nothing was set")
rabbitmq_pass = os.getenv("RABBITMQ_DEFAULT_PASS", "Nothing was set")
rabbitmq_host = os.getenv("RABBITMQ_HOST", "Nothing was set")
rabbitmq_port = os.getenv("RABBITMQ_PORT", "Nothing was set")

try:
print ("Checking RabbitMQ")
credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_pass)
parameters = pika.ConnectionParameters(rabbitmq_host,rabbitmq_port,'/',credentials)
connection = pika.BlockingConnection(parameters)
if connection.is_open:
print ("RabbitMQ OK")
else:
print ("RabbitMQ connection is no open")
sys.exit(-1)

except Exception as e:
print (e)
sys.exit(-1)
finally:
connection.close()

sys.exit(0)

END
}

until setup_ready; do
>&2 echo 'Waiting for MongoDB and RabbitMQ to become available...'
sleep 2
done

>&2 echo 'MongoDB and RabbitMQ are available, running the application'

exec "$@"
10 changes: 10 additions & 0 deletions .local/start_api
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -o errexit
set -o pipefail

if [[ -z "${KUBERNETES_SERVICE_HOST}" ]]; then
uvicorn src.app.main:app --host 0.0.0.0 --port 8000 --reload
else
uvicorn src.app.main:app --proxy-headers --host 0.0.0.0 --port 8000 --reload
fi
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: FastAPI",
"type": "python",
"request": "launch",
"module": "uvicorn",
"args": [
"src.app.main:app",
"--reload"
],
"jinja": true,
"justMyCode": true
}
]
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"makefile.extensionOutputFolder": "./.vscode",
"python.linting.enabled": true,
"python.linting.mypyEnabled": true,
"python.formatting.provider": "black",
"python.testing.pytestArgs": ["."],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
48 changes: 48 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
FROM python:3.11-slim

SHELL ["/bin/bash", "-o", "pipefail", "-c"]


# Configure env
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
POETRY_VERSION=1.4.2 \
POETRY_HOME="/usr/src/poetry" \
POETRY_CACHE_DIR="/usr/src/poetry/cache"

ENV PATH="$POETRY_HOME/bin:$PATH"

# Install OS dependencies
RUN set -ex; \
apt-get update; \
apt-get install -y --no-install-recommends \
build-essential \
git \
vim \
openssh-server \
iputils-ping \
curl; \
\
apt-get clean; \
rm -rf /var/lib/apt/lists/*

# Install Poetry
RUN curl -sSL https://install.python-poetry.org | python3 -

WORKDIR /code

COPY pyproject.toml poetry.lock ./

RUN poetry config virtualenvs.create false \
&& poetry install $(test "$YOUR_ENV" == production && echo "--no-dev") --no-interaction --no-ansi

COPY .local/entrypoint .local/start_* /usr/src/
RUN set -ex; \
chmod +x /usr/src/entrypoint; \
chmod +x /usr/src/start_api

COPY . .

#ENTRYPOINT ["/usr/src/entrypoint"]

CMD ["/usr/src/start_api"]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# messages-api
# MAPI

This service enables the sending and receiving of events through the HTTP REST protocol, without depending on a specific implementation of an event broker or queue
12 changes: 12 additions & 0 deletions compose-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
app:
entrypoint:
- sleep
- infinity
image: docker/dev-environments-default:stable-1
init: true
volumes:
- type: bind
source: /var/run/docker.sock
target: /var/run/docker.sock

60 changes: 60 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
version: "3.5"
services:

mapi: &mapi
build:
context: .
dockerfile: Dockerfile
ports:
- "${DOCKER_APP_PORT:-8000}:8000"
depends_on:
- mapi_mongodb
- mapi_rabbitmq
env_file:
- .env
volumes:
- ./:/code:delegated
networks:
- mongodb_network
- rabbit_network

mapi_mongodb:
image: mongo:6.0.1
container_name: mapi_mongodb
env_file:
- .env
ports:
- ${DOCKER_MONGODB_PORT:-27017}:27017
volumes:
- ./init-mongodb:/docker-entrypoint-initdb.d
- ./init-mongodb/data:/tmp/data
networks:
- mongodb_network
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongo mongo:27017/mapi --quiet
interval: 10s
timeout: 10s
retries: 5
start_period: 40s

mapi_rabbitmq:
image: rabbitmq:3
container_name: mapi_rabbitmq
env_file:
- .env
networks:
- rabbit_network
healthcheck:
test: rabbitmq-diagnostics -q ping
interval: 30s
timeout: 30s
retries: 3
ports:
- ${RABBITMQ_PORT:-5672}:5672

volumes:
init-mongodb:

networks:
mongodb_network:
rabbit_network:
56 changes: 56 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# default service
service := mapi

# all our targets are phony (no files to check).
.PHONY: help up shell code build clean

help:
@echo ""
@echo "Usage: make [TARGET] [EXTRA_ARGUMENTS]"
@echo "Targets:"
@echo " build build project"
@echo " up start project"
@echo " shell start a shell inside the container"
@echo " unit-tests run unit tests"
@echo " integration-tests run integration tests"
@echo " clean remove the project, including containers, images, volumens, etc"

# start project
up:
@docker-compose up -d

# interactive shell
shell:
@docker-compose run --rm --service-ports $(service) /bin/bash

# open VS Code
code:
@echo "\033[0;32mWait for the container to start before clicking the Reopen in Container button."
@code .

create-env:
@cp ./.env.development.example ./.env


# install project
build:
@docker-compose build \
&& echo "" \
&& echo "Installation complete." \
|| echo "\033[0;31mInstallation failed."

build-cache:
@docker-compose build --no-cache \
&& echo "" \
&& echo "Installation complete." \
|| echo "\033[0;31mInstallation failed."

# run unit tests
tests:
@docker-compose run $(service) python -m pytest --cov=src/ --cov-report=html:cover/html_dir --cov-report=xml:cover/coverage.xml --feature features -vv src/tests/

# uninstall project
clean:
@docker-compose down --remove-orphans -v --rmi local 2>/dev/null \
&& echo "\033[0;32mProject removed." \
|| echo "\033[0;32mProject already removed."
Loading