WIP: working on db initial create #3

Open
anomalist wants to merge 2 commits from web-push-db-initialize into master
  1. 14
      Dockerfile
  2. 36
      app.py
  3. 9
      entrypoint.sh

14
Dockerfile

@ -17,7 +17,9 @@ RUN mkdir -p /app/instance/data
COPY app.py /app
COPY requirements.txt /app
COPY models.py /app
COPY init_db.py /app/init_db.py
COPY entrypoint.sh /app
COPY init_db.py /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
@ -26,6 +28,7 @@ RUN apk del .build-deps
# ---- Production Stage ----
FROM python:3.8-alpine3.18 as production
RUN apk add --upgrade --no-cache bash
# Create a user to run our application
RUN adduser -D myuser -u 1000
@ -35,10 +38,19 @@ WORKDIR /app
COPY --from=builder /app /app
COPY --from=builder /usr/local /usr/local
COPY --from=builder /usr/bin /usr/bin
RUN chown -R myuser:myuser /app
# Set script to be executable
RUN chmod +x /app/entrypoint.sh
# Switch to the created user
USER myuser
RUN ls -l /app
RUN ls -l /
# Set the script as the entrypoint
ENTRYPOINT ["/app/entrypoint.sh"]
# Start gunicorn with the appropriate options
CMD ["gunicorn", "-b", "0.0.0.0:3000", "--log-level=debug", "--workers=3", "app:app"]

36
app.py

@ -41,14 +41,15 @@ class WebPushService():
db_uri = os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite:////app/instance/data/webpush.db')
# This relative path works in docker-compose
#db_uri = os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite:///data/webpush.db')
self.app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
# self.app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
# Initializing the database with the application
db.init_app(self.app)
# db.init_app(self.app)
# db.create_all()
# Creating a context for the application and initializing services
with self.app.app_context():
self._initialize()
# with self.app.app_context():
# self._initialize()
# Creating and starting a thread to send daily notifications
self.daily_notification_thread = threading.Thread(target=self._send_daily_notifications)
@ -109,6 +110,7 @@ class WebPushService():
to web clients, hence the check and generation if they don't exist.
"""
pass
# Checking if there are any VAPID keys in the database
if not VAPIDKey.query.first():
@ -170,26 +172,26 @@ class WebPushService():
# Creating a context for the application to enable database operations
with self.app.app_context():
pass
# Retrieving all subscription data from the database
all_subscriptions = Subscription.query.all()
#all_subscriptions = Subscription.query.all()
# Retrieving the VAPID key from the database
vapid_key = VAPIDKey.query.first()
#vapid_key = VAPIDKey.query.first()
# Constructing the push notification message
message = {"title": "Daily Update", "message": "Here's your daily update!"}
#message = {"title": "Daily Update", "message": "Here's your daily update!"}
# Sending a push notification to each subscribed client
for subscription in all_subscriptions:
subscription_info = {
"endpoint": subscription.endpoint,
"keys": {
"p256dh": subscription.p256dh,
"auth": subscription.auth
}
}
WebPushService._send_push_notification(subscription_info, message, vapid_key)
# for subscription in all_subscriptions:
# subscription_info = {
# "endpoint": subscription.endpoint,
# "keys": {
# "p256dh": subscription.p256dh,
# "auth": subscription.auth
# }
# }
# WebPushService._send_push_notification(subscription_info, message, vapid_key)
# Sleeping for 24 hours before sending the next set of notifications
time.sleep(24 * 60 * 60)

9
entrypoint.sh

@ -0,0 +1,9 @@
#!/bin/sh
# Set the desired permissions
#chown -R 1000:1000 /app/instance/data
echo "Hello World"
# Execute the command provided as arguments to this script
#exec "$@"
Loading…
Cancel
Save