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
This commit is contained in:
@@ -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
|
||||||
+18
@@ -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"]
|
||||||
@@ -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 ("<h1>Random PDF Generator</h1>"
|
||||||
|
"<p>Try the <a href='/generate?lang=en'>/generate</a> endpoint "
|
||||||
|
"with a 'lang' query param (en or de) for random text.</p>")
|
||||||
|
|
||||||
|
@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)
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
Flask==2.2.5
|
||||||
|
requests==2.31.0
|
||||||
|
reportlab==3.6.12
|
||||||
|
Faker==18.9.0
|
||||||
Reference in New Issue
Block a user