Files
Alchegos.Webhook/Program.cs
2025-05-01 23:01:29 +01:00

38 lines
1.3 KiB
C#

using System.Text.Json.Nodes;
using Alchegos.Core;
using Alchegos.Core.Services.RabbitMQ;
using Alchegos.Webhook;
GlobalRegistry.Instance.Start();
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Services.Configure<RabbitConnectionOptions>(
builder.Configuration.GetSection("RabbitMQ")
);
builder.Services.AddSingleton<IRabbitService, RabbitService>();
builder.Services.AddSingleton<IRabbitPublisher, RabbitPublisher>();
builder.Services.AddSingleton<IRabbitPublisher, RabbitPublisher>();
builder.Services.AddSingleton<GiteaEventDispatcher>();
WebApplication app = builder.Build();
ILogger<Program> logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Starting web hook /webhook/gitea");
app.MapPost("/webhook/gitea",
async (
HttpRequest request,
HttpResponse response,
IRabbitPublisher publisher,
GiteaEventDispatcher dispatcher
) =>
{
string jsonBody = await new StreamReader(request.Body).ReadToEndAsync();
string giteaEvent = request.Headers["X-Gitea-Event"].ToString() ?? "unknown";
JsonNode payload = JsonNode.Parse(jsonBody);
if(payload is not null)
await dispatcher.DispatchAsync(giteaEvent, payload, publisher);
return Results.Ok();
});
app.Run();