aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Backend/Controllers/PushController.cs
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2026-04-02 12:38:10 +0200
committerAriel Costas Guerrero <ariel@costas.dev>2026-04-02 12:45:33 +0200
commit1b4f4a674ac533c0b51260ba35ab91dd2cf9486d (patch)
tree9fdaf418bef86c51737bcf203483089c9e2b908b /src/Enmarcha.Backend/Controllers/PushController.cs
parent749e04d6fc2304bb29920db297d1fa4d73b57648 (diff)
Basic push notification system for service alerts
Co-authored-by: Copilot <copilot@github.com>
Diffstat (limited to 'src/Enmarcha.Backend/Controllers/PushController.cs')
-rw-r--r--src/Enmarcha.Backend/Controllers/PushController.cs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/Enmarcha.Backend/Controllers/PushController.cs b/src/Enmarcha.Backend/Controllers/PushController.cs
new file mode 100644
index 0000000..9df6b48
--- /dev/null
+++ b/src/Enmarcha.Backend/Controllers/PushController.cs
@@ -0,0 +1,50 @@
+using Enmarcha.Backend.Configuration;
+using Enmarcha.Backend.Services;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Options;
+
+namespace Enmarcha.Backend.Controllers;
+
+[Route("api/push")]
+[ApiController]
+public class PushController(IPushNotificationService pushService, IOptions<AppConfiguration> config) : ControllerBase
+{
+ /// <summary>Returns the VAPID public key for the browser to use when subscribing.</summary>
+ [HttpGet("vapid-public-key")]
+ public IActionResult GetVapidPublicKey()
+ {
+ var vapid = config.Value.Vapid;
+ if (vapid is null)
+ return StatusCode(StatusCodes.Status503ServiceUnavailable, "Push notifications are not configured on this server.");
+
+ return Ok(new { publicKey = vapid.PublicKey });
+ }
+
+ /// <summary>Registers a new push subscription.</summary>
+ [HttpPost("subscribe")]
+ public async Task<IActionResult> Subscribe([FromBody] SubscribeRequest request)
+ {
+ if (!Uri.TryCreate(request.Endpoint, UriKind.Absolute, out var uri) || uri.Scheme != Uri.UriSchemeHttps)
+ return BadRequest("Invalid push endpoint: must be an absolute HTTPS URL.");
+
+ if (string.IsNullOrWhiteSpace(request.P256Dh) || string.IsNullOrWhiteSpace(request.Auth))
+ return BadRequest("Missing encryption keys.");
+
+ await pushService.SubscribeAsync(request.Endpoint, request.P256Dh, request.Auth);
+ return NoContent();
+ }
+
+ /// <summary>Removes a push subscription.</summary>
+ [HttpDelete("unsubscribe")]
+ public async Task<IActionResult> Unsubscribe([FromBody] UnsubscribeRequest request)
+ {
+ if (string.IsNullOrWhiteSpace(request.Endpoint))
+ return BadRequest("Endpoint is required.");
+
+ await pushService.UnsubscribeAsync(request.Endpoint);
+ return NoContent();
+ }
+}
+
+public record SubscribeRequest(string Endpoint, string P256Dh, string Auth);
+public record UnsubscribeRequest(string Endpoint);