feat(engine): WebhookRegistry — (workflowId, path) → invocation lookup
This commit is contained in:
36
src/VolcanicCore.Api/Engine/WebhookRegistry.cs
Normal file
36
src/VolcanicCore.Api/Engine/WebhookRegistry.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace VolcanicCore.Api.Engine;
|
||||
|
||||
public delegate Task WebhookInvocation(JsonElement body, Guid answerChannelId, CancellationToken ct);
|
||||
|
||||
public sealed class WebhookRegistry
|
||||
{
|
||||
private sealed record Key(string WorkflowId, string Path);
|
||||
private readonly ConcurrentDictionary<Key, WebhookInvocation> _routes = new();
|
||||
|
||||
// Returns an IDisposable that removes the registration when disposed —
|
||||
// that's what WebhookNode.StartAsync will hand back to TriggerHost.
|
||||
public IDisposable Register(string workflowId, string path, WebhookInvocation invoke)
|
||||
{
|
||||
var key = new Key(workflowId, Normalize(path));
|
||||
if (!_routes.TryAdd(key, invoke))
|
||||
throw new InvalidOperationException(
|
||||
$"Webhook path '{path}' is already registered for workflow {workflowId}.");
|
||||
return new Unregister(this, key);
|
||||
}
|
||||
|
||||
public WebhookInvocation? Lookup(string workflowId, string path)
|
||||
=> _routes.TryGetValue(new Key(workflowId, Normalize(path)), out var cb) ? cb : null;
|
||||
|
||||
private static string Normalize(string path) => path.Trim('/');
|
||||
|
||||
private sealed class Unregister : IDisposable
|
||||
{
|
||||
private readonly WebhookRegistry _owner;
|
||||
private readonly Key _key;
|
||||
public Unregister(WebhookRegistry o, Key k) { _owner = o; _key = k; }
|
||||
public void Dispose() => _owner._routes.TryRemove(_key, out _);
|
||||
}
|
||||
}
|
||||
@@ -118,6 +118,7 @@ builder.Services.AddSingleton<IConnectionInstanceRegistry>(sp =>
|
||||
sp.GetRequiredService<ConnectionInstanceRegistry>());
|
||||
|
||||
builder.Services.AddSingleton<WebhookRouter>();
|
||||
builder.Services.AddSingleton<WebhookRegistry>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user