Skip to content
Snippets Groups Projects
Commit 00ed983b authored by Chude, Chiamaka A (PG/T - Comp Sci & Elec Eng)'s avatar Chude, Chiamaka A (PG/T - Comp Sci & Elec Eng)
Browse files

Final commit for Advanced Web Project. Added docker and docker compose files...

Final commit for Advanced Web Project. Added docker and docker compose files for containerisation. Bug were also removed after testing.
parent 5442f370
No related branches found
No related tags found
1 merge request!9Final commit for User Microservice. Added docker and docker compose files for...
Showing
with 302 additions and 0 deletions
version: "3.2"
services:
zookeeper:
image: wurstmeister/zookeeper
container_name: zookeeper
ports:
- "2181:2181"
networks:
- kafka_network
kafka:
image: wurstmeister/kafka
container_name: kafka
ports:
- "9092:9092"
depends_on:
- zookeeper
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_BOOTSTRAP_SEVERS: kafka:9092
networks:
- kafka_network
networks:
kafka_network:
external: true
#driver: bridge
FROM python:3.11.4
#Copy requirements
COPY requirements.txt /order_ms/
COPY app /order_ms/
#Set working direcroty
WORKDIR /order_ms
#Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
#Install other libraries in container
RUN apt-get update && apt-get install -y \
unixodbc \
unixodbc-dev \
iputils-ping\
apt-transport-https \
gcc\
curl \
gnupg \
freetds-dev \
tdsodbc \
&& rm -rf /var/lib/apt/lists/*
#Install ODBC Driver for Microsoft SQL Server Section...
#Source: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16&tabs=debian18-install%2Calpine17-install%2Cdebian8-install%2Credhat7-13-install%2Crhel7-offline
#Add Microsoft repository GPG key
RUN curl https://packages.microsoft.com/keys/microsoft.asc | tee /etc/apt/trusted.gpg.d/microsoft.asc
#Add the Microsoft SQL Server repository for Debian 12
RUN curl https://packages.microsoft.com/config/debian/12/prod.list | tee /etc/apt/sources.list.d/mssql-release.list
#Add Microsoft GPG key
RUN curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o /usr/share/keyrings/microsoft-archive-keyring.gpg
#Add the Microsoft SQL Server repository for Debian 12
RUN echo "deb [arch=amd64 signed-by=/usr/share/keyrings/microsoft-archive-keyring.gpg] https://packages.microsoft.com/debian/12/prod bookworm main" > /etc/apt/sources.list.d/mssql-release.list
#Update package list
RUN apt-get update
#Install ODBC Driver 17 for SQL Server
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17
#Install mssql-tools
RUN ACCEPT_EULA=Y apt-get install -y mssql-tools
RUN bash -c "echo 'export PATH=\"$PATH:/opt/mssql-tools/bin\"' >> ~/.bashrc && source ~/.bashrc"
#RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc && source ~/.bashrc
#Install unixodbc-dev and kerberos library
RUN apt-get install -y libgssapi-krb5-2
#Set environment variables for ODBC configuration
ENV ODBCINI=/etc/odbc.ini
ENV ODBCSYSINI=/etc
CMD ["python", "index.py"]
\ No newline at end of file
File added
# from flask import Flask
# from .controllers.AddToCartController import AddToCart_bp
# from .controllers.CheckOutController import checkout_bp
# from .controllers.DeleteFromItemController import DeleteCart_bp
# from .controllers.FetchCartController import cart_bp
# def create_app():
# app = Flask(__name__)
# app.register_blueprint(AddToCart_bp)
# app.register_blueprint(checkout_bp)
# app.register_blueprint(DeleteCart_bp)
# app.register_blueprint(cart_bp)
# return app
from flask import Flask
from flask_cors import CORS
import os
from app.controllers.AddToCartController import AddToCart_bp
from app.controllers.CheckOutController import checkout_bp
from .controllers.DeleteFromItemController import DeleteCart_bp
from .controllers.FetchCartController import cart_bp
from .subscriber.UpdateProductSubscriber import start_kafka_consumer
def create_app():
app = Flask(__name__)
CORS(app)
app.config.from_object('config')
app.register_blueprint(AddToCart_bp)
app.register_blueprint(checkout_bp)
app.register_blueprint(DeleteCart_bp)
app.register_blueprint(cart_bp)
# Optionally start Kafka consumer based on configuration
if app.config.get('START_KAFKA_CONSUMER', False):
start_kafka_consumer()
return app
\ No newline at end of file
File added
File added
File added
DEBUG = True
SECRET_KEY = 'Group3'
PORT = 5003
START_KAFKA_CONSUMER = True
HOST = '0.0.0.0'
KAFKA_SERVER = "kafka:9092"
\ No newline at end of file
from flask import Blueprint, jsonify, request, json, session
from models.AddToCart import add_item_shopping_cart
from models.CheckPriceAndQuantity import check_availability_and_price
import requests
AddToCart_bp = Blueprint("AddToCart", __name__)
@AddToCart_bp.route("/cart/AddToCart", methods=["POST"])
def add_item():
#user_id = 1 # Temporarily bypass authentication for testing
user_id = session.get("user_id")
if not user_id:
return jsonify({"error": "User not logged in"}), 401
data = request.get_json()
ProductID = data.get("ProductID")
CartQuantity = data.get("CartQuantity")
UnitPrice = data.get("UnitPrice")
# Check for required data
if not ProductID or not CartQuantity or UnitPrice is None:
return jsonify({"error": "Product ID, quantity, and unit price are required."}), 400
# Call the CheckPriceAndQuantity function
inventory_check = check_availability_and_price(ProductID, CartQuantity, UnitPrice, user_id)
# If there's an issue with availability or price, return the message from the inventory check
if not inventory_check.get("available") or not inventory_check.get("price_correct"):
return jsonify({
"error": inventory_check.get("message"), # Message from inventory check
"available_quantity": inventory_check.get("available_quantity"),
"current_price": inventory_check.get("current_price")
}), 400
cart_item_data = {
"UserID": user_id,
"ProductID": ProductID,
"UnitPrice": UnitPrice,
"CartQuantity": CartQuantity
}
# Update cart item data with the current price
cart_item_data["UnitPrice"] = inventory_check["current_price"]
# If the checks pass, add the item to the shopping cart
add_cart_item_message = add_item_shopping_cart(cart_item_data)
return jsonify(add_cart_item_message)
from flask import Blueprint, request, jsonify, session
from models.CheckOut import checkout_cart
from publishers.kafkaPublishers import publish_product_updated_event
checkout_bp = Blueprint('checkout', __name__)
@checkout_bp.route('/cart/checkout', methods=['POST'])
def handle_checkout():
user_id = session.get("user_id") # Retrieve the user ID from session; ensure the user is logged in
#user_id = 1
if not user_id:
return jsonify({"error": "User not logged in", "UserID" : user_id}), 401
data = request.get_json()
if not data or 'address' not in data:
return jsonify({"error": "Missing required fields: address"}), 400
address = data['address']
# Call the checkout function with the provided data
checkout_result = checkout_cart(user_id, address)
# Check for errors in the result and return appropriate responses
if 'error' in checkout_result:
return jsonify({"error": checkout_result["error"]}), 500 # or another appropriate status code
# If checkout is successful, publish an event to Kafka for each item purchased
for item in checkout_result.get('item_details', []): # Ensure that item_details are included in checkout_result
event_data = {
"ProductID": item['ProductID'],
"QuantityPurchased": item['Quantity']
}
publish_product_updated_event(event_data)
return jsonify({
"message": "Checkout completed successfully",
"TransactionID": checkout_result.get("TransactionID", "Unknown")
}), 200
from models.DeleteItemCartModel import delete_item_from_cart
from flask import Blueprint, jsonify, request, json, session
DeleteCart_bp = Blueprint("delete", __name__)
@DeleteCart_bp.route("/cart/delete/<int:cart_item_id>", methods=['DELETE'])
def delete_item(cart_item_id):
#user_id = 1
user_id = session.get("user_id")
if not user_id:
return jsonify({"error": "You need to be logged in to delete items from the cart."}), 401
# Call the function to delete the item from the cart
result = delete_item_from_cart(user_id, cart_item_id)
if "error" in result:
return jsonify(result), 404 # Or another appropriate status code based on the error
return jsonify(result), 200
\ No newline at end of file
from flask import Blueprint, jsonify, request, json, session
from models.FetchKart import get_user_cart_items
import requests
cart_bp = Blueprint("cart", __name__)
@cart_bp.route("/cart", methods=["GET"])
def fetch_user_cart():
#user_id = 5
user_id = session.get("user_id")
if not user_id:
return jsonify({"error": "User ID is required."}), 400
cart_data = get_user_cart_items(user_id)
if "error" in cart_data:
return jsonify({"error": cart_data["error"]}), 500
return jsonify(cart_data)
\ No newline at end of file
from flask import Blueprint, jsonify, session
from models.order_history import fetch_order_history
order_history_bp = Blueprint('order_history', __name__)
@order_history_bp.route('/order/history', methods=['GET'])
def order_history():
user_id = session.get('user_id')
#user_id = 1
if not user_id:
return jsonify({"error": "Authentication required to view order history."}), 401
order_history_data = fetch_order_history(user_id)
if 'error' in order_history_data:
return jsonify({"error": order_history_data["error"]}), 500
return jsonify({"order_history": order_history_data}), 200
\ No newline at end of file
from flask import Blueprint, jsonify, request, json, session
from models.UpdateProduct import UpdateProduct
from subscriber.UpdateProductSubscriber import consume_product_updated_event
import requests
File added
File added
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment