Skip to content

Commit 0b5e103

Browse files
authored
Aspire defaults (#44)
* Clean program.cs in project with better service defaults * Add masstransit trace and metrics * Some more cleaning
1 parent 652d042 commit 0b5e103

File tree

16 files changed

+94
-281
lines changed

16 files changed

+94
-281
lines changed

src/Ubik.Accounting.SalesOrVatTax.Api/Program.cs

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using MassTransit;
2-
using Microsoft.AspNetCore.Mvc;
32
using Microsoft.AspNetCore.Mvc.Infrastructure;
43
using Microsoft.EntityFrameworkCore;
54
using System.Reflection;
6-
using System.Text.Json.Serialization;
75
using Ubik.Accounting.SalesOrVatTax.Api.Data;
86
using Ubik.Accounting.SalesOrVatTax.Api.Data.Init;
97
using Ubik.Accounting.SalesOrVatTax.Api.Features.Accounts.Services;
@@ -19,7 +17,9 @@
1917

2018
var builder = WebApplication.CreateBuilder(args);
2119

20+
// From Aspire.ServiceDefaults.Extensions
2221
builder.AddServiceDefaults();
22+
builder.AddServiceApiDefaults();
2323

2424
//Options
2525
var authOptions = new AuthServerOptions();
@@ -29,19 +29,13 @@
2929
var swaggerUIOptions = new SwaggerUIOptions();
3030
builder.Configuration.GetSection(SwaggerUIOptions.Position).Bind(swaggerUIOptions);
3131

32-
//Default httpclient - Aspire now
33-
//builder.Services.ConfigureHttpClientDefaults(http =>
34-
//{
35-
// http.AddStandardResilienceHandler();
36-
//});
37-
3832
builder.Services.AddDbContextFactory<AccountingSalesTaxDbContext>(
3933
options => options.UseNpgsql(builder.Configuration.GetConnectionString("AccountingSalesTaxDbContext")), ServiceLifetime.Scoped);
4034
builder.EnrichNpgsqlDbContext<AccountingSalesTaxDbContext>();
4135

36+
//Dapper
4237
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
4338

44-
//TODO: remove useless parts (only pub/sub)
4539
//MessageBroker with masstransit + outbox
4640
builder.Services.AddMassTransit(config =>
4741
{
@@ -76,21 +70,6 @@
7670

7771
});
7872

79-
//Api versioning
80-
builder.Services.AddApiVersionAndExplorer();
81-
82-
//TODO: Cors
83-
builder.Services.AddCustomCors();
84-
85-
//Tracing and metrics
86-
//builder.Logging.AddOpenTelemetry(logging =>
87-
//{
88-
// logging.IncludeFormattedMessage = true;
89-
// logging.IncludeScopes = true;
90-
//});
91-
92-
//builder.Services.AddTracingAndMetrics();
93-
9473
//Swagger config
9574
var xmlPath = Path.Combine(AppContext.BaseDirectory,
9675
$"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
@@ -107,27 +86,6 @@
10786
builder.Services.AddScoped<IApplicationCommandService, ApplicationCommandService>();
10887
builder.Services.AddTransient<ProblemDetailsFactory, CustomProblemDetailsFactory>();
10988

110-
111-
//Strandard API things
112-
builder.Services.AddControllers(o =>
113-
{
114-
o.Filters.Add(new ProducesAttribute("application/json"));
115-
}).AddJsonOptions(options =>
116-
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
117-
118-
builder.Services.AddHttpContextAccessor();
119-
120-
//Route config
121-
builder.Services.Configure<RouteOptions>(options =>
122-
{
123-
options.LowercaseUrls = true;
124-
});
125-
126-
builder.Services.AddControllers();
127-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
128-
builder.Services.AddEndpointsApiExplorer();
129-
builder.Services.AddSwaggerGen();
130-
13189
var app = builder.Build();
13290

13391
app.MapDefaultEndpoints();
@@ -163,10 +121,6 @@
163121
subApp => subApp.UseMiddleware<UserInHeaderMiddleware>()
164122
);
165123

166-
//app.UseHttpsRedirection();
167-
168-
//app.UseAuthorization();
169-
170124
app.MapControllers();
171125

172126
app.Run();

src/Ubik.Accounting.SalesOrVatTax.Api/Ubik.Accounting.SalesOrVatTax.Api.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
18-
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
1917
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.2.2" />
2018
<PackageReference Include="Dapper" Version="2.1.35" />
2119
<PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />

src/Ubik.Accounting.Structure.Api/Program.cs

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public static async Task Main(string[] args)
2727
{
2828
var builder = WebApplication.CreateBuilder(args);
2929
builder.AddServiceDefaults();
30-
31-
//Log
32-
//TODO: Begin to log usefull things
33-
//builder.Host.UseSerilog((context, configuration) => configuration.ReadFrom.Configuration(context.Configuration));
30+
builder.AddServiceApiDefaults();
3431

3532
//Options
3633
var authOptions = new AuthServerOptions();
@@ -40,22 +37,12 @@ public static async Task Main(string[] args)
4037
var swaggerUIOptions = new SwaggerUIOptions();
4138
builder.Configuration.GetSection(SwaggerUIOptions.Position).Bind(swaggerUIOptions);
4239

43-
//Auth server and JWT (no need, no auth)
44-
//builder.Services.AddAuthServerAndJwt(authOptions);
45-
46-
////Default httpclient - Aspire now
47-
//builder.Services.ConfigureHttpClientDefaults(http =>
48-
//{
49-
// http.AddStandardResilienceHandler();
50-
//});
51-
5240
//DB
5341
builder.Services.AddDbContextFactory<AccountingDbContext>(
5442
options => options.UseNpgsql(builder.Configuration.GetConnectionString("AccountingContext")), ServiceLifetime.Scoped);
5543

5644
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
5745

58-
//TODO: remove useless parts (only pub/sub)
5946
//MessageBroker with masstransit + outbox
6047
builder.Services.AddMassTransit(config =>
6148
{
@@ -90,28 +77,9 @@ public static async Task Main(string[] args)
9077
});
9178

9279
//Add all consumers
93-
//config.AddConsumers(Assembly.GetExecutingAssembly());
94-
95-
//Add commands clients
96-
80+
config.AddConsumers(Assembly.GetExecutingAssembly());
9781
});
9882

99-
100-
//Api versioning
101-
builder.Services.AddApiVersionAndExplorer();
102-
103-
//TODO: Cors
104-
builder.Services.AddCustomCors();
105-
106-
////Tracing and metrics - Aspire now
107-
//builder.Logging.AddOpenTelemetry(logging =>
108-
//{
109-
// logging.IncludeFormattedMessage = true;
110-
// logging.IncludeScopes = true;
111-
//});
112-
113-
//builder.Services.AddTracingAndMetrics();
114-
11583
//Swagger config
11684
var xmlPath = Path.Combine(AppContext.BaseDirectory,
11785
$"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
@@ -131,22 +99,6 @@ public static async Task Main(string[] args)
13199
builder.Services.AddScoped<ICurrentUser, CurrentUser>();
132100
builder.Services.AddTransient<ProblemDetailsFactory, CustomProblemDetailsFactory>();
133101

134-
//Strandard API things
135-
builder.Services.AddControllers(o =>
136-
{
137-
o.Filters.Add(new ProducesAttribute("application/json"));
138-
}).AddJsonOptions(options =>
139-
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
140-
141-
builder.Services.AddHttpContextAccessor();
142-
builder.Services.AddEndpointsApiExplorer();
143-
144-
//Route config
145-
builder.Services.Configure<RouteOptions>(options =>
146-
{
147-
options.LowercaseUrls = true;
148-
});
149-
150102
//Build the app
151103
var app = builder.Build();
152104

@@ -186,10 +138,6 @@ public static async Task Main(string[] args)
186138
subApp => subApp.UseMiddleware<UserInHeaderMiddleware>()
187139
);
188140

189-
//app.UseHttpsRedirection();
190-
//app.UseAuthentication();
191-
//app.UseAuthorization();
192-
193141
app.MapControllers();
194142
app.Run();
195143
}

src/Ubik.Accounting.Structure.Api/Ubik.Accounting.Structure.Api.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
</ItemGroup>
2222

2323
<ItemGroup>
24-
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
25-
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
2624
<PackageReference Include="Dapper" Version="2.1.35" />
2725
<PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />
2826
<PackageReference Include="LanguageExt.Core" Version="4.4.9" />
@@ -38,7 +36,6 @@
3836
</PackageReference>
3937
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
4038
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.10" />
41-
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
4239
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.0.0" />
4340
</ItemGroup>
4441

src/Ubik.Accounting.Structure.Api/appsettings.json

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,5 @@
2626
"ClientId": "ubik_app",
2727
"ClientSecret": "Ye6Y36ocA4SaGqYzd0HgmqMhVaM2jlkE"
2828
},
29-
"AllowedHosts": "*",
30-
"Serilog": {
31-
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Console" ],
32-
"MinimumLevel": {
33-
"Default": "Information",
34-
"Override": {
35-
"Microsoft": "Warning",
36-
"System": "Warning",
37-
"Microsoft.Hosting.Lifetime": "Warning",
38-
"Serilog.AspNetCore.RequestLoggingMiddleware": "Warning",
39-
"Microsoft.EntityFrameworkCore.Database": "Information",
40-
"Microsoft.EntityFrameworkCore.Migrations": "Warning"
41-
}
42-
},
43-
"WriteTo": [
44-
{
45-
"Name": "Console"
46-
},
47-
{
48-
"Name": "File",
49-
"Args": {
50-
"path": "/logs/log-.txt",
51-
"rollOnFileSizeLimit": true,
52-
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter,Serilog.Formatting.Compact",
53-
"rollingInterval": "Day"
54-
}
55-
}
56-
],
57-
"Enrich": [ "FromLogContext", "WithThreadId", "WithMachineName" ]
58-
}
29+
"AllowedHosts": "*"
5930
}

src/Ubik.Accounting.Transaction.Api/Program.cs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
var builder = WebApplication.CreateBuilder(args);
2121

2222
builder.AddServiceDefaults();
23+
builder.AddServiceApiDefaults();
2324

2425
//Options
2526
var authOptions = new AuthServerOptions();
@@ -29,12 +30,6 @@
2930
var swaggerUIOptions = new SwaggerUIOptions();
3031
builder.Configuration.GetSection(SwaggerUIOptions.Position).Bind(swaggerUIOptions);
3132

32-
//Default httpclient - Aspire now
33-
//builder.Services.ConfigureHttpClientDefaults(http =>
34-
//{
35-
// http.AddStandardResilienceHandler();
36-
//});
37-
3833
builder.Services.AddDbContextFactory<AccountingTxContext>(
3934
options => options.UseNpgsql(builder.Configuration.GetConnectionString("AccountingTxContext")), ServiceLifetime.Scoped);
4035

@@ -78,19 +73,6 @@
7873

7974
});
8075

81-
//Api versioning
82-
builder.Services.AddApiVersionAndExplorer();
83-
84-
//TODO: Cors
85-
builder.Services.AddCustomCors();
86-
87-
//Tracing and metrics - Aspire now
88-
//builder.Logging.AddOpenTelemetry(logging =>
89-
//{
90-
// logging.IncludeFormattedMessage = true;
91-
// logging.IncludeScopes = true;
92-
//});
93-
9476
//Services
9577
builder.Services.AddScoped<ICurrentUser, CurrentUser>();
9678
builder.Services.AddScoped<IAccountCommandService, AccountCommandService>();
@@ -99,26 +81,6 @@
9981
builder.Services.AddScoped<IApplicationCommandService, ApplicationCommandService>();
10082
builder.Services.AddTransient<ProblemDetailsFactory, CustomProblemDetailsFactory>();
10183

102-
//builder.Services.AddTracingAndMetrics();
103-
104-
builder.Services.AddControllers(o =>
105-
{
106-
o.Filters.Add(new ProducesAttribute("application/json"));
107-
}).AddJsonOptions(options =>
108-
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
109-
110-
builder.Services.AddHttpContextAccessor();
111-
builder.Services.AddEndpointsApiExplorer();
112-
113-
//Route config
114-
builder.Services.Configure<RouteOptions>(options =>
115-
{
116-
options.LowercaseUrls = true;
117-
});
118-
119-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
120-
builder.Services.AddEndpointsApiExplorer();
121-
12284
//Swagger config
12385
var xmlPath = Path.Combine(AppContext.BaseDirectory,
12486
$"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
@@ -162,8 +124,6 @@
162124

163125
app.UseHttpsRedirection();
164126

165-
//app.UseAuthorization();
166-
167127
app.MapControllers();
168128

169129
app.Run();

src/Ubik.Accounting.WebApp/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,19 @@
144144
});
145145

146146
builder.Services.AddAuthorization();
147+
148+
//Services
147149
builder.Services.AddSingleton<IRenderContext, ServerRenderContext>();
148150
builder.Services.AddScoped<BreakpointsService>();
149151
builder.Services.AddScoped<AuthenticationStateProvider, PersistingRevalidatingAuthenticationStateProvider>();
152+
builder.Services.AddScoped<ClassificationStateService>();
150153

151154
//User service with circuit
152155
builder.Services.AddScoped<UserService>();
153-
//TODO:check I think this thing is never used
154156
builder.Services.TryAddEnumerable(
155157
ServiceDescriptor.Scoped<CircuitHandler, UserCircuitHandler>());
156158

159+
//Typed clients
157160
builder.Services.AddHttpClient<IAccountingApiClient, AccountingApiClient>();
158161

159162
builder.Services.Configure<ApiOptions>(
@@ -181,8 +184,6 @@
181184
return httpClientHandler;
182185
});
183186

184-
builder.Services.AddScoped<ClassificationStateService>();
185-
186187
builder.Services.Configure<ForwardedHeadersOptions>(options =>
187188
{
188189
options.ForwardedHeaders =

0 commit comments

Comments
 (0)