-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
124 lines (106 loc) · 4.38 KB
/
Program.cs
File metadata and controls
124 lines (106 loc) · 4.38 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using MassTransit;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System.Reflection;
using XRPService.Consumers;
using XRPService.Events;
using XRPService.Features.Payments;
using XRPService.Features.Wallets;
using XRPService.Services;
var builder = WebApplication.CreateBuilder(args);
var instanceId = Guid.NewGuid().ToString("N")[..8];
Console.WriteLine($"Starting XRPService with instance ID: {instanceId}");
// Configure services
ConfigureOpenTelemetry(builder.Services, builder.Environment.EnvironmentName, instanceId, builder.Configuration);
ConfigureServices(builder.Services);
ConfigureMassTransit(builder.Services, builder.Configuration, instanceId);
var app = builder.Build();
// Configure middleware pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
// Map endpoints
app.MapControllers();
app.MapPaymentEndpoints();
app.MapWalletEndpoints();
app.MapHealthChecks("/health");
app.Run();
// Configuration methods
void ConfigureOpenTelemetry(IServiceCollection services, string environmentName, string serviceInstanceId, IConfiguration configuration)
{
services.AddOpenTelemetry()
.ConfigureResource(resource => resource
.AddService("XRPService")
.AddAttributes(new Dictionary<string, object>
{
["deployment.environment"] = environmentName,
["service.instance.id"] = serviceInstanceId
}))
.WithTracing(tracing => tracing
.AddSource("XRPService")
.AddSource("XRPService.Payments")
.AddSource("XRPService.Wallets")
.AddSource("XRPService.MassTransit")
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter(options => options.Endpoint = new Uri(
configuration["OpenTelemetry:OtlpExporter:Endpoint"] ?? "http://localhost:4317"))
.AddConsoleExporter())
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter(options => options.Endpoint = new Uri(
configuration["OpenTelemetry:OtlpExporter:Endpoint"] ?? "http://localhost:4317"))
.AddConsoleExporter());
}
void ConfigureServices(IServiceCollection services)
{
// API and documentation
services.AddControllers();
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
// HttpClient for XRPL
services.AddHttpClient("XRPL", client =>
client.DefaultRequestHeaders.Add("Accept", "application/json"));
// XRPL Services
services.AddSingleton<IXRPLService, XRPLService>();
services.AddScoped<IWalletService, WalletService>();
services.AddScoped<IPaymentService, PaymentService>();
// Health checks
services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy())
.AddCheck("xrpledger", () => HealthCheckResult.Healthy());
}
void ConfigureMassTransit(IServiceCollection services, IConfiguration configuration, string serviceInstanceId)
{
services.AddMassTransit(x =>
{
// Register specific consumers
x.AddConsumer<EnergyUpdateConsumer>();
// Register all consumers in the assembly
x.AddConsumers(Assembly.GetExecutingAssembly());
x.UsingAzureServiceBus((context, cfg) =>
{
cfg.Host(configuration.GetConnectionString("ServiceBus"));
// Add middleware to propagate activity context
cfg.ConfigureSend(sendCfg =>
{
sendCfg.UseExecuteAsync(_ => Task.CompletedTask);
});
// Configure message types
cfg.Message<EnergyUpdateEvent>(m => m.SetEntityName("energy-updates"));
cfg.Message<PaymentConfirmedEvent>(m => m.SetEntityName("payment-confirmations"));
cfg.Message<PaymentFailedEvent>(m => m.SetEntityName("payment-failures"));
cfg.Message<SessionFinalizedEvent>(m => m.SetEntityName("session-finalizations"));
// Configure endpoint naming with instance ID
cfg.ConfigureEndpoints(context,
new DefaultEndpointNameFormatter($"xrp-{serviceInstanceId}-", false));
});
});
}