r/flask 14h ago

Ask r/Flask How do i set up a SSL certificate on flask app with mod_wsgi inside a docker containter

5 Upvotes

I signed up on cloudflare and got a free SSL certificate and i have a .pem file and a .key file and here is the dockerfile i used for my image

# Start with a base Python image
FROM python:3.11

# Install necessary system packages
RUN apt-get update && \
    apt-get install -y \
    apache2 \
    apache2-dev \
    libapache2-mod-wsgi-py3 \
    locales \
    && rm -rf /var/lib/apt/lists/*

# Generate locale and set locale environment variables
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
    locale-gen && \
    update-locale LANG=en_US.UTF-8

# Create a non-root user and group
RUN groupadd -r mostafa && useradd -r -g mostafa mostafa

# Create a directory for your application
RUN mkdir /application && chown mostafa:mostafa /application

# Set the working directory
WORKDIR /application

# Copy the entire application source code into the container
COPY --chown=mostafa:mostafa . .

# Install Python dependencies
RUN pip install -r requirements.txt

# Expose the higher port 
EXPOSE 80

# Switch to the non-root user
USER mostafa

# Command to run the server on port 
CMD ["mod_wsgi-express", "start-server", "/application/wsgi.py", "--port", "80", "--processes", "1"]

How can i modify this dockerfile to use https

```


r/flask 17h ago

Ask r/Flask Simple Notification Handling Solution

1 Upvotes

I'm trying to integrate notifications into an existing website structure. Currently on the backend, when I want to create a new notification, I just create a new record in the database and my "sent_to_client" attribute is set to false. On the frontend, I'm using HTMX to create a websocket connection with the server. The problem is that I'm polling the database a couple hundred times a second. I've looked into Redis Pub/Sub models but want to avoid that complexity. I've also used polling in the past but I need data to update much quicker (and reducing the polling time leads to me the same result: - lots of queries).

Is there any workaround to achieve these <1s notifications without the extra complexity and dependencies?

# routes.py

@sock.route("/notifications")
@login_required
def notifications(ws):
    # get initial list of notifications
    all_notifications = Notification.query.filter_by(user_id=current_user.id).filter(Notification.dismissed == False).order_by(Notification.timestamp).all()
    initial_template = render_template("admin/notifications/notifications.html", all_notifications=all_notifications)
    ws.send(initial_template)
    while True:
        # check for new notifications
        new_notifications = Notification.query.filter_by(user_id=current_user.id).filter(Notification.dismissed == False, Notification.sent_to_client == False).order_by(Notification.timestamp).all()
        if new_notifications:
            for notification in new_notifications:
                notification.sent_to_client = True
            db.session.commit()
            template = render_template("admin/notifications/notification.html", all_notifications=new_notifications)
            ws.send(template)
        pass