-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
159 lines (133 loc) · 5.89 KB
/
Program.cs
File metadata and controls
159 lines (133 loc) · 5.89 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using StateMachineMapper.Commands;
using StateMachineMapper.Database;
using StateMachineMapper.Interfaces;
using StateMachineMapper.StateMachine.Data;
using StateMachineMapper.Services;
using StateMachineMapper.StateMachine;
using MassTransit;
using MassTransit.Logging;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using StateMachineMapper.Constants;
using StateMachineMapper.Manager;
using StateMachineMapper.StateMachine.Data;
using StateMachineMapper.StateMachine.Manager;
using StateMachineMapper.StateMachine.Manager.Interfaces;
using StateMachineMapper.StateMachine.TransientRegistrationConfigurator;
namespace StateMachineMapper;
public static class Program
{
public static int Main(string[] args)
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
builder.Services.AddNpgsqlDataSource(builder.Configuration.GetConnectionString("Database")!, npgsqlDataSourceBuilder =>
{
npgsqlDataSourceBuilder.UseNetTopologySuite();
npgsqlDataSourceBuilder.EnableDynamicJson();
});
var wDataSourceBuilder =
new NpgsqlDataSourceBuilder(builder.Configuration.GetConnectionString("Database")!);
wDataSourceBuilder.UseNetTopologySuite();
wDataSourceBuilder.EnableDynamicJson();
var wDataSource = wDataSourceBuilder.Build();
builder.Services.AddDbContext<DefaultDatabaseContext>(ctx =>
{
ctx.UseNpgsql(wDataSource, npgsqlDbContextOptionsBuilder =>
{
npgsqlDbContextOptionsBuilder.UseQuerySplittingBehavior(QuerySplittingBehavior.SingleQuery);
npgsqlDbContextOptionsBuilder.UseNetTopologySuite();
});
ctx.ConfigureWarnings(wc => wc.Ignore(RelationalEventId.PendingModelChangesWarning).Ignore(RelationalEventId.BoolWithDefaultWarning).Ignore(CoreEventId.ManyServiceProvidersCreatedWarning));
});
builder.Services.AddScoped<DbContext>(provider => provider.GetService<DefaultDatabaseContext>());
builder.Services.AddTransientMassTransit(busConfigurator =>
{
busConfigurator.SetKebabCaseEndpointNameFormatter();
busConfigurator.AddConsumers(typeof(Program).Assembly);
busConfigurator.AddTransientSagaStateMachine<OnboardingStateMachine, OnboardingStateMachineData>()
.EntityFrameworkRepository(r =>
{
r.ExistingDbContext<DefaultDatabaseContext>();
r.UsePostgres();
}).ExcludeFromConfigureEndpoints();
busConfigurator.UsingRabbitMq((context, cfg) =>
{
cfg.Host(builder.Configuration["AppSettings:RabbitMq:Host"], builder.Configuration["AppSettings:RabbitMq:VHost"], h =>
{
h.Username(builder.Configuration["AppSettings:RabbitMq:Username"]!);
h.Password(builder.Configuration["AppSettings:RabbitMq:Password"]!);
});
cfg.ConfigureEndpoints(context);
});
builder.Services.AddOpenTelemetry()
.ConfigureResource(ConfigureResource)
.WithTracing(b => b
.AddSource(DiagnosticHeaders.DefaultListenerName)
.SetResourceBuilder(ResourceBuilder.CreateDefault()
.AddService(builder.Environment.ApplicationName)
.AddTelemetrySdk()
.AddAttributes(
new KeyValuePair<string, object>[]
{
new("deployment.environment", builder.Environment.EnvironmentName),
})
.AddEnvironmentVariableDetector())
.AddAspNetCoreInstrumentation(o =>
{
o.RecordException = true;
})
.AddHttpClientInstrumentation(o =>
{
o.RecordException = true;
})
.AddConsoleExporter()
);
});
builder.Services.AddSingleton<EndpointManager>();
builder.Services.AddSingleton<RabbitMqManager>();
builder.Services.AddSingleton<IEmailService, EmailService>();
builder.Services.AddTransient<IDynamicStateMachineManager, DynamicStateMachineManager>();
builder.Services.AddMvc();
builder.Services.AddControllers();
builder.Services.AddSwaggerGen();
builder.Services.AddControllersWithViews();
var app = builder.Build();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
if (endpoints != null)
{
endpoints.MapControllers();
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action=Index}/{id?}");
}
});
app.UseSwagger();
app.UseSwaggerUI();
using var scope = app.Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<DefaultDatabaseContext>();
context.Database.Migrate();
app.UseHttpsRedirection();
app.Run();
return 0;
}
private static void ConfigureResource(this ResourceBuilder r)
{
r.AddService("State Machine Mapper",
serviceVersion: "0.0.1",
serviceInstanceId: Environment.MachineName);
}
}