Skip to content

Commit

Permalink
added Publishing Persistent Messages to the rabbitmq_utils.py and com…
Browse files Browse the repository at this point in the history
…pleted the README.md
  • Loading branch information
sepehr toof authored and sepehr toof committed Jun 17, 2024
1 parent 4b396c8 commit eaef0f1
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 42 deletions.
45 changes: 25 additions & 20 deletions Backend/auth_service/auth_service/user_auth/rabbitmq_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
from django.conf import settings

def get_rabbitmq_connection():
credentials = pika.PlainCredentials(settings.RABBITMQ_USER, settings.RABBITMQ_PASS)
parameters = pika.ConnectionParameters(settings.RABBITMQ_HOST, settings.RABBITMQ_PORT, '/', credentials)
return pika.BlockingConnection(parameters)
credentials = pika.PlainCredentials(settings.RABBITMQ_USER, settings.RABBITMQ_PASS)
parameters = pika.ConnectionParameters(settings.RABBITMQ_HOST, settings.RABBITMQ_PORT, '/', credentials)
return pika.BlockingConnection(parameters)

def publish_message(queue_name, message):
connection = get_rabbitmq_connection()
channel = connection.channel()
channel.queue_declare(queue=queue_name, durable=True)
# Ensure the message is a JSON string
message = json.dumps(message) if isinstance(message, dict) else message
channel.basic_publish(exchange='', routing_key=queue_name, body=message)
connection.close()
connection = get_rabbitmq_connection()
channel = connection.channel()
channel.queue_declare(queue=queue_name, durable=True)
# Ensure the message is a JSON string
message = json.dumps(message) if isinstance(message, dict) else message
channel.basic_publish(
exchange='',
routing_key=queue_name,
body=message,
properties=pika.BasicProperties(delivery_mode=2)
)
connection.close()

def consume_message(queue_name, callback):
connection = get_rabbitmq_connection()
channel = connection.channel()
channel.queue_declare(queue=queue_name, durable=True)
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
try:
channel.start_consuming()
except Exception as e:
print(f"Error consuming message: {e}")
finally:
connection.close()
connection = get_rabbitmq_connection()
channel = connection.channel()
channel.queue_declare(queue=queue_name, durable=True)
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
try:
channel.start_consuming()
except Exception as e:
print(f"Error consuming message: {e}")
finally:
connection.close()
45 changes: 25 additions & 20 deletions Backend/user_management/user_management/users/rabbitmq_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@
from django.conf import settings

def get_rabbitmq_connection():
credentials = pika.PlainCredentials(settings.RABBITMQ_USER, settings.RABBITMQ_PASS)
parameters = pika.ConnectionParameters(settings.RABBITMQ_HOST, settings.RABBITMQ_PORT, '/', credentials)
return pika.BlockingConnection(parameters)
credentials = pika.PlainCredentials(settings.RABBITMQ_USER, settings.RABBITMQ_PASS)
parameters = pika.ConnectionParameters(settings.RABBITMQ_HOST, settings.RABBITMQ_PORT, '/', credentials)
return pika.BlockingConnection(parameters)

def publish_message(queue_name, message):
connection = get_rabbitmq_connection()
channel = connection.channel()
channel.queue_declare(queue=queue_name, durable=True)
# Ensure the message is a JSON string
message = json.dumps(message) if isinstance(message, dict) else message
channel.basic_publish(exchange='', routing_key=queue_name, body=message)
connection.close()
connection = get_rabbitmq_connection()
channel = connection.channel()
channel.queue_declare(queue=queue_name, durable=True)
# Ensure the message is a JSON string
message = json.dumps(message) if isinstance(message, dict) else message
channel.basic_publish(
exchange='',
routing_key=queue_name,
body=message,
properties=pika.BasicProperties(delivery_mode=2)
)
connection.close()

def consume_message(queue_name, callback):
connection = get_rabbitmq_connection()
channel = connection.channel()
channel.queue_declare(queue=queue_name, durable=True)
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
try:
channel.start_consuming()
except Exception as e:
print(f"Error consuming message: {e}")
finally:
connection.close()
connection = get_rabbitmq_connection()
channel = connection.channel()
channel.queue_declare(queue=queue_name, durable=True)
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
try:
channel.start_consuming()
except Exception as e:
print(f"Error consuming message: {e}")
finally:
connection.close()
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
This project consists of two endpoints that allows users to view, add, update, and delete user records. The application is built using the Django framework in Python. Every endpoint is using Postgresql as a database and Django Rest Framework to serialize the data and return it in JSON format.

The address of the first endpoint user_management API is http://127.0.0.1:8000/user/register and http://127.0.0.1:8000/user/<int:pk> "without angel brackets" for the second address.
The address of the first endpoint user_management API is
http://127.0.0.1:8000/user/register "create user record using POST method"
http://127.0.0.1:8000/user/ "retrieve all user records using GET method only super user or staff can access"
http://127.0.0.1:8000/user/<int:pk> "without angel brackets" "retrieve, update and delete user record using GET, PUT and DELETE methods respectively"

The first endpoint allows users to login and receive a token that is used to authenticate the user. The token is valid for 60 minutes. The token is used to authenticate the user when they want to edit, update, or delete user records. The token is also used to authenticate the user when they want to view user records.
The address of the second endpoint user_auth API is http://127.0.0.1:8000/auth/api/token/

The first endpoint allows users to create a user, update, delete and retrieve. The token is valid for 60 minutes.

User table consists of the following fields:
You can find it in user_management/user_management/users/models.py

| Field Name | Data Type | Description |
|------------|-----------|-------------|
| id | Integer | Primary Key |
| username | String | User Name |
| email | String | User Email |
| password | String | User Password (Password is hashed)|

The user_auth table consists of the following fields:

| Field Name | Data Type | Description |
|------------|-----------|-------------|
| username | String | User Name |
| token_data | JSON | Token Data |

token_data is a JSON object that consists of two dictionaries:
1. refresh: Refresh Token

| Field Name | Data Type | Description |
|------------|-----------|-------------|
| token | String | Refresh Token |
| exp | Integer | Expiry Time |

2. access: Access Token

| Field Name | Data Type | Description |
|------------|-----------|-------------|
| token | String | Access Token |
| exp | Integer | Expiry Time |


For now I have routed the user_management API to port 8000 and user_auth API to port 8001. The Rabbitmq management is routed to port 15672. You can change the port number in the docker-compose.yml file.
The address of all of them is http://127.0.0.1 or http://localhost

0 comments on commit eaef0f1

Please sign in to comment.