dns retry

This commit is contained in:
cpu
2025-03-28 01:47:05 +01:00
parent 398c6473a4
commit 228f4984d8
12 changed files with 192 additions and 81 deletions

View File

@@ -7,8 +7,9 @@ It's designed to be run as a Docker container and integrated with Traefik v3 for
## Features
* Receives POST requests on `/flic-webhook`.
* Parses `button_id` and `click_type` from the Flic request body.
* Looks up the target PWA push subscription based on `button_id` in a JSON file.
* Uses HTTP headers `Button-Name` and `Timestamp` from the Flic request.
* Parses `click_type` from the Flic request body.
* Looks up the target PWA push subscription based on the `Button-Name` header in a JSON file.
* Sends a Web Push notification containing the click details (action, button, timestamp) to the corresponding PWA subscription.
* Integrates with Traefik v3 via Docker labels.
* Configurable via environment variables (`.env` file).
@@ -27,10 +28,10 @@ It's designed to be run as a Docker container and integrated with Traefik v3 for
## System Architecture
### Subscription Flow
![Subscription Flow](/diagrams/subscription-flow.png)
<img src="images/subscription-flow.png" width="600" alt="Subscription Flow">
### Interaction Flow
![Interaction Flow](/diagrams/interaction-flow.png)
<img src="images/interaction-flow.png" width="300" alt="Interaction Flow">
## Project Structure
@@ -64,6 +65,10 @@ It's designed to be run as a Docker container and integrated with Traefik v3 for
* `ALLOWED_ORIGINS`: Comma-separated list of domains allowed by CORS. Include your PWA's domain if it needs to interact directly (e.g., for setup). Example: `https://my-pwa.com`.
* `ALLOWED_METHODS`: (Default: `POST,OPTIONS`) Standard methods needed.
* `ALLOWED_HEADERS`: (Default: `Content-Type,Authorization`) Standard headers needed.
* `MAX_NOTIFICATION_RETRIES`: (Default: `3`) Number of retry attempts for failed push notifications. Must be a number.
* `INITIAL_RETRY_DELAY_MS`: (Default: `1000`) Initial delay in milliseconds before first retry. Must be a number.
* `DNS_TIMEOUT_MS`: (Default: `5000`) DNS resolution timeout in milliseconds. Must be a number.
* `HTTP_TIMEOUT_MS`: (Default: `10000`) HTTP request timeout in milliseconds. Must be a number.
* `TRAEFIK_SERVICE_HOST`: Your public domain for this service (e.g., `webpush.virtonline.eu`).
* `TRAEFIK_CERT_RESOLVER`: The name of your TLS certificate resolver configured in Traefik (e.g., `le`, `myresolver`).
@@ -134,25 +139,27 @@ It's designed to be run as a Docker container and integrated with Traefik v3 for
In your Flic app or Flic Hub SDK interface:
1. Select your Flic button.
2. Add an "Internet Request" action (or similar HTTP request action) for Single Click, Double Click, and/or Hold events.
3. **URL:** `https://<YOUR_TRAEFIK_SERVICE_HOST>/flic-webhook` (e.g., `https://webpush.virtonline.eu/flic-webhook`)
4. **Method:** `POST`
5. **Body Type:** `JSON` (or `application/json`)
6. **Body:** Configure the JSON body to include the button's serial number and the click type. Flic usually provides variables for these. The backend expects `button_id` and `click_type`. Adapt the keys if needed, or modify `server.js` to expect different keys (e.g., `serialNumber`).
2. Add an "Internet Request" action.
3. Fill in the following details:
* Set `POST` method.
* Set URL: `https://webpush.virtonline.eu/flic-webhook`
* Add headers:
* Key: `Authorization`
* Value: `Bearer <FLIC_SECRET>` (Replace `<FLIC_SECRET>` with the actual secret from your `.env` file).
* **Body:** Configure the JSON body to include the click type.
```json
{
"button_id": "{serialNumber}",
"click_type": "{clickType}",
"timestamp": "{timestamp}"
"click_type": "SingleClick"
}
```
*(Verify the exact variable names like `{serialNumber}`, `{clickType}`, `{timestamp}` within your specific Flic interface.)*
7. **Headers:**
* Add `Content-Type: application/json`.
* **(Optional - if `FLIC_SECRET` is set):** Add an `Authorization` header:
* Key: `Authorization`
* Value: `Bearer <YOUR_FLIC_SECRET_VALUE>` (Replace `<YOUR_FLIC_SECRET_VALUE>` with the actual secret from your `.env` file).
* Set Content-Type: `application/json`.
* It should look like this:
<!-- <img src="images/flic-button-request.png" width="300" alt="Flic Button Request Configuration"> // TODO: Update image -->
* Tap on `Save action`.
4. Repeat for Double Click and/or Hold events.
## API Endpoint
* **`POST /flic-webhook`**
@@ -161,16 +168,14 @@ In your Flic app or Flic Hub SDK interface:
* **Request Body (JSON):**
```json
{
"button_id": "SERIAL_NUMBER_OF_FLIC_BUTTON",
"click_type": "SingleClick | DoubleClick | Hold",
"timestamp": "ISO_8601_TIMESTAMP_STRING (Optional)"
"click_type": "SingleClick | DoubleClick | Hold"
}
```
* **Responses:**
* `200 OK`: Webhook received, push notification sent successfully.
* `400 Bad Request`: Missing `button_id` or `click_type` in the request body.
* `400 Bad Request`: Missing `Button-Name` header or `click_type` in the request body.
* `401 Unauthorized`: Missing or invalid Bearer token (if `FLIC_SECRET` is enabled).
* `404 Not Found`: No subscription found in `subscriptions.json` for the given `button_id`.
* `404 Not Found`: No subscription found in `subscriptions.json` for the given `Button-Name`.
* `410 Gone`: The push subscription associated with the button was rejected by the push service (likely expired or revoked).
* `500 Internal Server Error`: Failed to send the push notification for other reasons.
@@ -184,6 +189,35 @@ In your Flic app or Flic Hub SDK interface:
}
```
## Testing the Webhook
Once your service is up and running, you can test the webhook endpoint using curl or any API testing tool:
**Note:** In the command below, replace `a74181969a613c545d66c1e436e75c1e4a6` with your actual FLIC_SECRET value from your .env file.
```bash
curl -X POST https://webpush.virtonline.eu/flic-webhook \
-H "Authorization: Bearer a74181969a613c545d66c1e436e75c1e4a6" \
-H "Content-Type: application/json" \
-H "Button-Name: Game" \
-H "Timestamp: 2025-03-26T01:10:20Z" \
-d '{
"click_type": "SingleClick"
}'
```
The expected response should be:
```json
{"message":"Push notification sent successfully"}
```
If successful, the above response indicates that:
1. Your webhook endpoint is properly configured
2. The button ID was found in your subscriptions.json file
3. The web push notification was successfully sent to the registered PUSH API endpoint (e.g. https://jmt17.google.com/fcm/send/cf907M...)
If you receive a different response, refer to the Troubleshooting section below.
## Troubleshooting
* **Check Backend Logs:** `docker logs flic-webhook-webpush`. Look for errors related to configuration, file access, JSON parsing, authentication, or sending push notifications.