Skip to content

Commit

Permalink
feat: Fix rabbitmq registration
Browse files Browse the repository at this point in the history
  • Loading branch information
alex289 committed Nov 21, 2024
1 parent 425da0b commit 693ca45
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
41 changes: 41 additions & 0 deletions CleanArchitecture.Api/Extensions/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using CleanArchitecture.Domain.Rabbitmq;
using Microsoft.Extensions.Configuration;

namespace CleanArchitecture.Api.Extensions;

public static class ConfigurationExtensions
{
public static RabbitMqConfiguration GetRabbitMqConfiguration(
this IConfiguration configuration)
{
var isAspire = configuration["ASPIRE_ENABLED"] == "true";

var rabbitEnabled = configuration["RabbitMQ:Enabled"];
var rabbitHost = configuration["RabbitMQ:Host"];
var rabbitPort = configuration["RabbitMQ:Port"];
var rabbitUser = configuration["RabbitMQ:Username"];
var rabbitPass = configuration["RabbitMQ:Password"];

if (isAspire)
{
rabbitEnabled = "true";
var connectionString = configuration["ConnectionStrings:RabbitMq"];

var rabbitUri = new Uri(connectionString!);
rabbitHost = rabbitUri.Host;
rabbitPort = rabbitUri.Port.ToString();
rabbitUser = rabbitUri.UserInfo.Split(':')[0];
rabbitPass = rabbitUri.UserInfo.Split(':')[1];
}

return new RabbitMqConfiguration()
{
Host = rabbitHost ?? "",
Port = int.Parse(rabbitPort ?? "0"),
Enabled = bool.Parse(rabbitEnabled ?? "false"),
Username = rabbitUser ?? "",
Password = rabbitPass ?? "",
};
}
}
14 changes: 3 additions & 11 deletions CleanArchitecture.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,8 @@

var isAspire = builder.Configuration["ASPIRE_ENABLED"] == "true";

var rabbitHost = builder.Configuration["RabbitMQ:Host"];
var rabbitPort = builder.Configuration["RabbitMQ:Port"];
var rabbitUser = builder.Configuration["RabbitMQ:Username"];
var rabbitPass = builder.Configuration["RabbitMQ:Password"];
var rabbitConnectionString =
isAspire ? builder.Configuration["ConnectionStrings:RabbitMq"] :
$"amqp://{rabbitUser}:{rabbitPass}@{rabbitHost}:{rabbitPort}";

var rabbitConfiguration = builder.Configuration.GetRabbitMqConfiguration();
var redisConnectionString = isAspire ? builder.Configuration["ConnectionStrings:Redis"] : builder.Configuration["RedisHostName"];

var dbConnectionString = isAspire ? builder.Configuration["ConnectionStrings:Database"] : builder.Configuration["ConnectionStrings:DefaultConnection"];

if (builder.Environment.IsProduction())
Expand All @@ -51,7 +43,7 @@
.AddSqlServer(dbConnectionString!)
.AddRedis(redisConnectionString!, "Redis")
.AddRabbitMQ(
rabbitConnectionString!,
rabbitConfiguration.ConnectionString,
name: "RabbitMQ");
}

Expand All @@ -72,7 +64,7 @@
builder.Services.AddNotificationHandlers();
builder.Services.AddApiUser();

builder.Services.AddRabbitMqHandler(builder.Configuration, "RabbitMQ");
builder.Services.AddRabbitMqHandler(rabbitConfiguration);

builder.Services.AddHostedService<SetInactiveUsersService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection AddRabbitMqHandler(
this IServiceCollection services,
IConfiguration configuration,
string rabbitMqConfigSection)
RabbitMqConfiguration configuration)
{
var rabbitMq = new RabbitMqConfiguration();
configuration.Bind(rabbitMqConfigSection, rabbitMq);
services.AddSingleton(rabbitMq);
services.AddSingleton(configuration);

services.AddSingleton<RabbitMqHandler>();
services.AddHostedService(serviceProvider => serviceProvider.GetService<RabbitMqHandler>()!);
Expand Down
2 changes: 2 additions & 0 deletions CleanArchitecture.Domain/Rabbitmq/RabbitMqConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public sealed class RabbitMqConfiguration
public bool Enabled { get; set; }
public string Username { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;

public string ConnectionString => $"amqp://{Username}:{Password}@{Host}:{Port}";
}
4 changes: 4 additions & 0 deletions CleanArchitecture.Domain/Rabbitmq/RabbitMqHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public override async Task StartAsync(CancellationToken cancellationToken)
return;
}

_logger.LogInformation("Starting RabbitMQ connection");

var factory = new ConnectionFactory
{
AutomaticRecoveryEnabled = true,
Expand All @@ -49,6 +51,8 @@ public override async Task StartAsync(CancellationToken cancellationToken)

var connection = await factory.CreateConnectionAsync(cancellationToken);
_channel = await connection.CreateChannelAsync(null, cancellationToken);

await base.StartAsync(cancellationToken);
}


Expand Down

0 comments on commit 693ca45

Please sign in to comment.