Skip to content

Commit

Permalink
webhook ef core config, WebhooksService.cs invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
charconstpointer committed Feb 2, 2021
1 parent b492b73 commit a7248d3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
13 changes: 12 additions & 1 deletion Gadget.Notifications/BackgroundServices/WebhooksService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
Expand Down Expand Up @@ -44,8 +46,17 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

_logger.LogInformation(
$"Received new webhook notification request for service {notification.ServiceName}");
await _client.PostAsync(notification.Webhook, null!, stoppingToken);
await _client.PostAsJsonAsync(notification.Webhook, new InvokeWebhook
{
Content =
$"Service : {notification.ServiceName} Agent : {notification.Agent} Status : {notification.Status}"
}, cancellationToken: stoppingToken);
}
}

public class InvokeWebhook
{
[JsonPropertyName("content")] public string Content { get; set; }
}
}
}
27 changes: 23 additions & 4 deletions Gadget.Notifications/Consumers/ServiceStatusChangedConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using Gadget.Messaging.SignalR.v1;
using Gadget.Notifications.Domain.ValueObjects;
using Gadget.Notifications.Hubs;
using Gadget.Notifications.Persistence;
using MassTransit;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace Gadget.Notifications.Consumers
Expand All @@ -16,12 +18,14 @@ public class ServiceStatusChangedConsumer : IConsumer<IServiceStatusChanged>
private readonly ILogger<ServiceStatusChangedConsumer> _logger;
private readonly IHubContext<NotificationsHub> _hub;
private readonly ChannelWriter<Notification> _channel;
private readonly NotificationsContext _notificationsContext;

public ServiceStatusChangedConsumer(ILogger<ServiceStatusChangedConsumer> logger,
IHubContext<NotificationsHub> hub, Channel<Notification> channel)
IHubContext<NotificationsHub> hub, Channel<Notification> channel, NotificationsContext notificationsContext)
{
_logger = logger;
_hub = hub;
_notificationsContext = notificationsContext;
_channel = channel.Writer;
}

Expand All @@ -38,9 +42,24 @@ public async Task Consume(ConsumeContext<IServiceStatusChanged> context)
Status = context.Message.Status
}, context.CancellationToken);
_logger.LogInformation("Trying to enqueue webhook notification");
await _channel.WriteAsync(new Notification(context.Message.Agent, context.Message.Name,
context.Message.Status, new Uri("http://localhost:5000/webhooks")));
_logger.LogInformation("Enqueued webhook notification");

var agent = await _notificationsContext.Services
.Include(s=>s.Webhooks)
.FirstOrDefaultAsync(s =>
s.Agent == context.Message.Agent && s.Name == context.Message.Name);

if (agent is null)
{
_logger.LogInformation("There are not webhooks registered for this event, skipping");
return;
}

foreach (var agentWebhook in agent.Webhooks)
{
await _channel.WriteAsync(new Notification(context.Message.Agent, context.Message.Name,
context.Message.Status, agentWebhook.Uri));
_logger.LogInformation("Enqueued webhook notification");
}
}
catch (Exception e)
{
Expand Down
4 changes: 3 additions & 1 deletion Gadget.Notifications/Controllers/WebhooksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public async Task<IActionResult> TestWebhook()
public async Task<IActionResult> CreateNewWebhook(string agentName, string serviceName,
CreateWebhook createWebhook)
{
var agent = await _context.Services.FirstOrDefaultAsync(s => s.Agent == agentName);
var agent = await _context.Services
.Include(s=>s.Webhooks)
.FirstOrDefaultAsync(s => s.Agent == agentName);
if (agent is null)
{
agent = new Service(agentName, serviceName);
Expand Down
4 changes: 4 additions & 0 deletions Gadget.Notifications/Persistence/NotificationsContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Gadget.Notifications.Domain.Entities;
using Gadget.Notifications.Domain.ValueObjects;
using Microsoft.EntityFrameworkCore;

namespace Gadget.Notifications.Persistence
Expand All @@ -20,8 +21,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
w.WithOwner().HasForeignKey("OwnerId");
w.Property<Guid>("Id");
w.Property(wh => wh.Uri);
w.Property(wh => wh.CreatedAt);
w.HasKey("Id");
}));

}

public DbSet<Service> Services { get; set; }
Expand Down
7 changes: 4 additions & 3 deletions Gadget.Notifications/requests.http
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
POST http://localhost:5000/webhooks/DESKTOP-6PDIBOR/BTAGService
Content-Type: application/json

//hackme
{
"Uri":"http://localhost:5000/webhooks"
"Uri":"https://discord.com/api/webhooks/806050278683049984/EJSocoVKoFRZ1Z9bLqK-QEYJCj3ZlphONoiGlAs7vKJj2VV8m1tAdwUONq9h1BCK11cC"
}

###
###

0 comments on commit a7248d3

Please sign in to comment.