|
@ -23,7 +23,11 @@ CONTACT_EMAIL = "mailto:info@timesafari.app" |
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebPushService(): |
|
|
class WebPushService(): |
|
|
|
|
|
|
|
|
|
|
|
latest_subscription_run = None |
|
|
|
|
|
|
|
|
""" |
|
|
""" |
|
|
This class provides services for sending web push notifications. |
|
|
This class provides services for sending web push notifications. |
|
|
""" |
|
|
""" |
|
@ -44,10 +48,12 @@ class WebPushService(): |
|
|
# Setting the application instance |
|
|
# Setting the application instance |
|
|
self.app = app |
|
|
self.app = app |
|
|
self.app.add_url_rule('/web-push/regenerate-vapid', view_func=self.regenerate_vapid, methods=['POST']) |
|
|
self.app.add_url_rule('/web-push/regenerate-vapid', view_func=self.regenerate_vapid, methods=['POST']) |
|
|
|
|
|
self.app.add_url_rule('/web-push/ping', view_func=self.ping, methods=['GET']) |
|
|
|
|
|
|
|
|
# Setting the database URI for the application |
|
|
# Setting the database URI for the application |
|
|
db_uri = os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite:////app/instance/data/webpush.db') |
|
|
db_uri = os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite:////app/instance/data/webpush.db') |
|
|
# This relative path works in docker-compose |
|
|
# This relative path works in docker-compose |
|
|
|
|
|
# This relative path works on a local run if you link to this dir from "var/app-instance" |
|
|
#db_uri = os.getenv('SQLALCHEMY_DATABASE_URI', 'sqlite:///data/webpush.db') |
|
|
#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 |
|
|
|
|
|
|
|
@ -230,10 +236,24 @@ class WebPushService(): |
|
|
print(f"Result from sub {subscription.id}: success={result['success']} text={result['message']}", flush=True) |
|
|
print(f"Result from sub {subscription.id}: success={result['success']} text={result['message']}", flush=True) |
|
|
print(f"{now} - Finished sending {len(all_subscriptions)} subscriptions.", flush=True) |
|
|
print(f"{now} - Finished sending {len(all_subscriptions)} subscriptions.", flush=True) |
|
|
|
|
|
|
|
|
|
|
|
self.latest_subscription_run = now |
|
|
|
|
|
|
|
|
# Sleeping for 24 hours before sending the next set of notifications |
|
|
# Sleeping for 24 hours before sending the next set of notifications |
|
|
time.sleep(24 * 60 * 60) |
|
|
time.sleep(24 * 60 * 60) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This is an endpoint, routed in __init__ |
|
|
|
|
|
def ping(self) -> Response: |
|
|
|
|
|
""" |
|
|
|
|
|
Endpoint to show liveness info |
|
|
|
|
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
- Response: Text with some subscription-run info |
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
return f"pong ... with latest subscription run at {self.latest_subscription_run}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This is an endpoint, routed in __init__ |
|
|
# This is an endpoint, routed in __init__ |
|
|
def regenerate_vapid(self) -> Tuple[str, int]: |
|
|
def regenerate_vapid(self) -> Tuple[str, int]: |
|
|
""" |
|
|
""" |
|
|