From 46bf8705d66143377f685155e1efc73b8732e8f6 Mon Sep 17 00:00:00 2001 From: Christian Krakau-Louis Date: Thu, 27 Mar 2025 15:16:12 +0100 Subject: [PATCH] Add initial setup for Dockerized application - Added GitHub Actions workflow for building Docker images (.github/workflows/docker-image.yml) - Created Dockerfile for containerizing the application - Implemented main application logic in app.py - Added requirements.txt for managing Python dependencies --- .github/workflows/docker-image.yml | 26 ++++++++++++ Dockerfile | 18 ++++++++ app.py | 66 ++++++++++++++++++++++++++++++ requirements.txt | 4 ++ 4 files changed, 114 insertions(+) create mode 100644 .github/workflows/docker-image.yml create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 requirements.txt diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..c831f92 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,26 @@ +name: Build and Push Docker Image + +on: + push: + branches: [ "main" ] + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push + uses: docker/build-push-action@v3 + with: + context: . + push: true + tags: your-dockerhub-username/random-pdf:latest diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3ee0e50 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# Use an official Python runtime as a parent image +FROM python:3.9-slim + +# Create a working directory +WORKDIR /app + +# Copy requirements and install +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the application code +COPY app.py . + +# Expose the port Flask will run on (5000 by default) +EXPOSE 5000 + +# Run the app +CMD ["python", "app.py"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..d95dbca --- /dev/null +++ b/app.py @@ -0,0 +1,66 @@ +import io +import requests +from flask import Flask, send_file, request +from reportlab.pdfgen import canvas +from reportlab.lib.pagesizes import A4 +from reportlab.lib.units import inch +from faker import Faker + +app = Flask(__name__) + +# We can seed 'faker' with 'en_US' or 'de_DE' for random text generation +fake_en = Faker('en_US') +fake_de = Faker('de_DE') + +@app.route("/") +def index(): + return ("

Random PDF Generator

" + "

Try the /generate endpoint " + "with a 'lang' query param (en or de) for random text.

") + +@app.route("/generate") +def generate_pdf(): + # Decide which language to use based on the query parameter; default to English + lang = request.args.get('lang', 'en').lower() + if lang == 'de': + text = fake_de.text() + else: + text = fake_en.text() + + # Fetch a random image from picsum (you could also use Unsplash or other services) + image_url = "https://picsum.photos/600/400" + response = requests.get(image_url) + image_data = response.content # raw bytes + + # Create a PDF in memory + pdf_buffer = io.BytesIO() + pdf_canvas = canvas.Canvas(pdf_buffer, pagesize=A4) + + # Add text + pdf_canvas.setFont("Helvetica", 12) + pdf_canvas.drawString(50, 780, "Random PDF Generator") + pdf_canvas.drawString(50, 760, f"Language: {lang}") + text_lines = text.split('\n') + y_position = 740 + for line in text_lines: + pdf_canvas.drawString(50, y_position, line) + y_position -= 15 + + # Embed the image + # First, write the downloaded image to an in-memory buffer + # Note: For certain image formats, we might need to use PIL to decode properly, + # but if the response is a jpeg from picsum, this simple approach works. + img_buffer = io.BytesIO(image_data) + # let's place the image below the text + pdf_canvas.drawImage(img_buffer, 50, 400, width=4*inch, height=3*inch) + + # finalize + pdf_canvas.showPage() + pdf_canvas.save() + pdf_buffer.seek(0) + + # Return the PDF to the client + return send_file(pdf_buffer, as_attachment=True, download_name="random.pdf", mimetype='application/pdf') + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=5000, debug=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..68a2ccb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +Flask==2.2.5 +requests==2.31.0 +reportlab==3.6.12 +Faker==18.9.0