commit cc44699bcf4354a08a375d28d2e3a6af5917b6a2 Author: shane ubento Date: Tue Mar 4 09:34:28 2025 +0100 first assinement submition diff --git a/z1/README.md b/z1/README.md new file mode 100644 index 0000000..92036a7 --- /dev/null +++ b/z1/README.md @@ -0,0 +1,40 @@ +# Visitor Counter Application + +This project demonstrates a simple web application using Apache (web server), Flask (backend) and Redis(database), deployed with Docker Compose. + +## Prerequisites + +- Docker Engine v20.10 or newer. +- Docker Compose v2.33.1 or newer. + +## Application Description + +The application consists of two services: +1. Web server (Apache) serving an HTML page. +2. Backend (flask): Handles requests to '/counter'. +2. Database (Redis) storing visitor counts persistently. + +## Instructions + +1. Prepare the application: +./prepare-app.sh + +2. Start the application: +./start-app.sh + +3. Access the application at: +http://localhost:5000 +or +run "hostname -I " and copy the ip address and Access the application at : +http://[ip address you copied]:5000 + +4. Stop the application: +./stop-app.sh + +5. Remove all resources: +./remove-app.sh + +## Virtual netwok and volumes + +- A default network is created by docker compose for communication between these services +- A named volum ('redis_data') ensures Redis data persists across restarts. diff --git a/z1/backend/Dockerfile b/z1/backend/Dockerfile new file mode 100644 index 0000000..717a23c --- /dev/null +++ b/z1/backend/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.9-slim + +WORKDIR /app + +COPY app.py /app/ + +RUN pip install flask redis + +EXPOSE 5000 + +CMD ["python", "app.py"] diff --git a/z1/backend/app.py b/z1/backend/app.py new file mode 100644 index 0000000..da96927 --- /dev/null +++ b/z1/backend/app.py @@ -0,0 +1,16 @@ +from flask import Flask, jsonify +import redis + +app = Flask(__name__) + +# Connect to Redis (running in a separate container) +redis_client = redis.StrictRedis(host='redis', port=6379, decode_responses=True) + +@app.route('/counter', methods=['GET']) +def counter(): + # Increment visitor count in Redis + visits = redis_client.incr('visits') + return jsonify({"visits": visits}) + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000) diff --git a/z1/compose.yaml b/z1/compose.yaml new file mode 100644 index 0000000..b2c82d1 --- /dev/null +++ b/z1/compose.yaml @@ -0,0 +1,28 @@ +version: '3.8' + +services: + web: + build: ./web + ports: + - "5000:80" + depends_on: + - backend + restart: always + + backend: + build: ./backend + ports: + - "5001:5000" + depends_on: + - redis + restart: always + + redis: + image: redis:alpine + command: ["redis-server", "--appendonly", "yes"] + volumes: + - redis_data:/data + restart: always + +volumes: + redis_data: diff --git a/z1/prepare-app.sh b/z1/prepare-app.sh new file mode 100755 index 0000000..81a9be4 --- /dev/null +++ b/z1/prepare-app.sh @@ -0,0 +1,3 @@ +#!/bin/bash +echo "Preparing app..." +docker compose build diff --git a/z1/remove-app.sh b/z1/remove-app.sh new file mode 100755 index 0000000..e24a859 --- /dev/null +++ b/z1/remove-app.sh @@ -0,0 +1,3 @@ +#!/bin/bash +echo "Removing app..." +docker compose down -v --remove-orphans diff --git a/z1/start-app.sh b/z1/start-app.sh new file mode 100755 index 0000000..53cbc1e --- /dev/null +++ b/z1/start-app.sh @@ -0,0 +1,6 @@ +#!/bin/bash +echo "Starting app..." +docker compose up -d +echo "The app is available at http://localhost:5000" +echo "You can also open in windows by running hostname -I in linux and copying the ip address " +echo "Then you can open you main OS broswer and type http://[ip you copied]:5000" diff --git a/z1/stop-app.sh b/z1/stop-app.sh new file mode 100755 index 0000000..0fdad68 --- /dev/null +++ b/z1/stop-app.sh @@ -0,0 +1,3 @@ +#!/bin/bash +echo "Stopping app..." +docker compose stop diff --git a/z1/web/Dockerfile b/z1/web/Dockerfile new file mode 100644 index 0000000..40b11bd --- /dev/null +++ b/z1/web/Dockerfile @@ -0,0 +1,6 @@ +FROM httpd:2.4 + +# Copies your static HTML file into the Apache document root +COPY index.html /usr/local/apache2/htdocs/ + +EXPOSE 80 diff --git a/z1/web/index.html b/z1/web/index.html new file mode 100644 index 0000000..241da34 --- /dev/null +++ b/z1/web/index.html @@ -0,0 +1,28 @@ + + + + + + Visitor Counter + + +

Welcome to the Visitor Counter App!

+

Made by ALI

+

Also made for assinment 1

+

This page has been visited 0 times.

+ + + +