-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
75 lines (57 loc) · 2.42 KB
/
Program.cs
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
using Service_bus.Services;
using Service_bus.Configurations;
using Service_bus.Middlewares;
using Service_bus.Filters;
using Service_bus.Volumes;
using Service_bus.LeaderElection;
using Service_bus.Consul;
using Consul;
using Service_bus.ServiceRegistry;
using Service_bus.DataReplication;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddEnvironmentVariables();
builder.Services.Configure<EventOptions>(
builder.Configuration.GetSection("EventOptions"));
builder.Services.Configure<ConsulOptions>(
builder.Configuration.GetSection("ConsulOptions"));
// Consul
builder.Services.AddSingleton<IHostedService, ConsulHostedService>();
builder.Services.AddSingleton<IConsulClient, ConsulClient>(p => new ConsulClient(consulConfig =>
{
var address = builder.Configuration["ConsulOptions:ConnectionString"];
consulConfig.Address = new Uri(address);
}));
builder.Services.AddSingleton<Func<IConsulClient>>(p => () => new ConsulClient(consulConfig =>
{
var address = builder.Configuration["ConsulOptions:ConnectionString"];
consulConfig.Address = new Uri(address);
}));
builder.Services.AddControllers(options =>
{
//options.Filters.Add<LeaderElectionChecker>();
});
// Http Client
builder.Services.AddHttpClient();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<IEventLogger<Service_bus.Models.Event>, EventLogger<Service_bus.Models.Event>>();
builder.Services.AddSingleton<IEventDispatcher<Service_bus.Models.Event>, EventDispatcher<Service_bus.Models.Event>>();
builder.Services.AddSingleton<IEventBus, EventBus>();
builder.Services.AddSingleton<IEventsLoader, EventsLoader>();
builder.Services.AddSingleton<LeaderElectionChecker, LeaderElectionChecker>();
// Leader Election
builder.Services.AddSingleton<ILeaderElectionClient, ConsulLeaderElection>();
// Service Registry
builder.Services.AddSingleton<IServiceBusRegistry, ServiceBusRegistry>();
// Data Replication
builder.Services.AddSingleton<ILeaderToFollowersDataReplication, LeaderToFollowersDataReplication>();
builder.Services.AddSingleton<IMiddleware, ExceptionMiddleware>();
builder.Services.AddHostedService<EventProcessor>();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseMiddleware<IMiddleware>();
app.UseSwagger();
app.UseSwaggerUI();
app.MapControllers();
app.Run();