finish send-test endpoint which now works
This commit is contained in:
@@ -32,7 +32,7 @@ sudo docker run -d -p 8900:3000 -v ~/py-push-server-db:/app/instance/data --name
|
|||||||
|
|
||||||
On a production server for security (eg /web-push/generate_vapid): set an environment variable `ADMIN_PASSWORD` for permissions; one way is to add this to the `docker run` command: `-e ADMIN_PASSWORD=<anything secure>`
|
On a production server for security (eg /web-push/generate_vapid): set an environment variable `ADMIN_PASSWORD` for permissions; one way is to add this to the `docker run` command: `-e ADMIN_PASSWORD=<anything secure>`
|
||||||
|
|
||||||
Finally, after it's started, generate a new VAPID by hitting the `regenerate_vapid` endpoint with a POST, eg. `curl -X POST localhost:8080/web-push/regenerate_vapid`
|
Finally, after it's started, generate a new VAPID by hitting the `regenerate-vapid` endpoint with a POST, eg. `curl -X POST localhost:8080/web-push/regenerate-vapid`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -244,7 +244,8 @@ pip install gunicorn
|
|||||||
|
|
||||||
source venv/bin/activate
|
source venv/bin/activate
|
||||||
|
|
||||||
gunicorn -b 0.0.0.0:3000 --log-level=debug --workers=1 app:app # 3 workers trigger 3 daily subscription runs
|
# 3 workers would trigger 3 daily subscription runs
|
||||||
|
gunicorn -b 0.0.0.0:3000 --log-level=debug --workers=1 app:app
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
24
app.py
24
app.py
@@ -43,7 +43,7 @@ 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'])
|
||||||
|
|
||||||
# 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')
|
||||||
@@ -152,8 +152,9 @@ class WebPushService():
|
|||||||
vapid_private_key=vapid_key.private_key,
|
vapid_private_key=vapid_key.private_key,
|
||||||
vapid_claims={"sub": CONTACT_EMAIL}
|
vapid_claims={"sub": CONTACT_EMAIL}
|
||||||
)
|
)
|
||||||
|
# "because sometimes that's what I had to do to make it work!" - Matthew
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
return {"success": True, "result": result}
|
return {"success": True, "message": result.text, "result": result}
|
||||||
|
|
||||||
except WebPushException as ex:
|
except WebPushException as ex:
|
||||||
now = datetime.datetime.now().isoformat()
|
now = datetime.datetime.now().isoformat()
|
||||||
@@ -236,7 +237,7 @@ class WebPushService():
|
|||||||
1. Deletes the current VAPID keys from the database.
|
1. Deletes the current VAPID keys from the database.
|
||||||
2. Generates and stores new VAPID keys using the `_generate_and_save_vapid_keys` method.
|
2. Generates and stores new VAPID keys using the `_generate_and_save_vapid_keys` method.
|
||||||
|
|
||||||
URL: /web-push/regenerate_vapid
|
URL: /web-push/regenerate-vapid
|
||||||
Method: POST
|
Method: POST
|
||||||
Header: Authentication: Basic ...
|
Header: Authentication: Basic ...
|
||||||
|
|
||||||
@@ -248,7 +249,7 @@ class WebPushService():
|
|||||||
- If there's an error during the operation, a JSON response with the error message is returned with a 500 status code.
|
- If there's an error during the operation, a JSON response with the error message is returned with a 500 status code.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# This default can be invoked thus: curl -X POST -H "Authorization: Basic YWRtaW46YWRtaW4=" localhost:3000/web-push/regenerate_vapid
|
# This default can be invoked thus: curl -X POST -H "Authorization: Basic YWRtaW46YWRtaW4=" localhost:3000/web-push/regenerate-vapid
|
||||||
envPassword = os.getenv('ADMIN_PASSWORD', 'admin')
|
envPassword = os.getenv('ADMIN_PASSWORD', 'admin')
|
||||||
auth = request.authorization
|
auth = request.authorization
|
||||||
if (auth is None
|
if (auth is None
|
||||||
@@ -366,7 +367,7 @@ class WebPushService():
|
|||||||
result = WebPushService._send_push_notification(subscription_info, message, vapid_key)
|
result = WebPushService._send_push_notification(subscription_info, message, vapid_key)
|
||||||
|
|
||||||
# Returning the operation status
|
# Returning the operation status
|
||||||
return jsonify(success=result.ok, message=result.text, result=result)
|
return jsonify(success=result["status_code"]==201, message=result["text"])
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -449,9 +450,16 @@ class WebPushService():
|
|||||||
"auth": subscription.auth
|
"auth": subscription.auth
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vapid_key = VAPIDKey.query.filter_by(id = subscription.vapid_key_id)
|
vapid_key = VAPIDKey.query.filter_by(id=subscription.vapid_key_id).first()
|
||||||
result = WebPushService._send_push_notification(subscription_info, {"title": "Test Notification", "message": "This is a test notification"}, vapid_key.private_key)
|
result = WebPushService._send_push_notification(
|
||||||
return jsonify(result)
|
subscription_info,
|
||||||
|
{"title": "Test Notification", "message": "This is a test notification"},
|
||||||
|
vapid_key
|
||||||
|
)
|
||||||
|
return jsonify(
|
||||||
|
success=result["result"].status_code==201,
|
||||||
|
message=result["result"].text,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
return jsonify({"success": False, "message": "Subscription not found"}), 404
|
return jsonify({"success": False, "message": "Subscription not found"}), 404
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user