-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
70 lines (55 loc) · 2.77 KB
/
Program.cs
File metadata and controls
70 lines (55 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Text;
using OrderFlow.Core.Configuration;
using OrderFlow.Core.Infrastructure.RabbitMQ;
using OrderFlow.Core.Services.Subscribers;
using HealthChecks.UI.Client;
using HealthChecks.RabbitMQ; // Add this using directive
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
// Enable UTF-8 encoding for console output to display emojis/icons
Console.OutputEncoding = Encoding.UTF8;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Configure RabbitMQ settings. builder.Configuration.GetSection("RabbitMq") retrieves the RabbitMQ configuration section from all the configuration sources (appsettings.json, environment variables, etc.) and binds it to the RabbitMqSettings class.
builder.Services.Configure<RabbitMqSettings>(
builder.Configuration.GetSection("RabbitMq"));
// Register RabbitMQ services
builder.Services.AddSingleton<IRabbitMqConnectionFactory, RabbitMqConnectionFactory>();
builder.Services.AddScoped<IMessagePublisher, RabbitMqPublisher>();
// Add RabbitMQ subscribers
// AddHostedService is used to register background services that run alongside the main application.
// Register subscribers as hosted services. These will run in the background when the application starts.
// Each subscriber listens to specific order events and processes them accordingly.
builder.Services.AddHostedService<OrderProcessingSubscriber>();
builder.Services.AddHostedService<PaymentVerificationSubscriber>();
builder.Services.AddHostedService<ShippingSubscriber>();
builder.Services.AddHostedService<NotificationSubscriber>();
// Get RabbitMQ settings to build proper connection string
var rabbitMqSettings = builder.Configuration.GetSection("RabbitMq").Get<RabbitMqSettings>();
var rabbitConnectionString = $"amqp://{rabbitMqSettings.UserName}:{rabbitMqSettings.Password}@{rabbitMqSettings.HostName}:{rabbitMqSettings.Port}";
// Configure Health Checks
builder.Services.AddHealthChecks()
.AddRabbitMQ(
rabbitConnectionString: rabbitConnectionString,
name: "rabbitmq",
failureStatus: HealthStatus.Degraded);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
// Map health check endpoint to /health for services like Kubernetes or monitoring tools to check application health
app.MapHealthChecks("/health", new HealthCheckOptions()
{
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.Run();