Skip to content

Commit

Permalink
refactor: startup services configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-shirbandi committed Dec 4, 2024
1 parent 7f659f2 commit 2a9fa61
Show file tree
Hide file tree
Showing 28 changed files with 114 additions and 147 deletions.
32 changes: 16 additions & 16 deletions src/1-BuildingBlocks/Web.MVC/Configuration/MVC/MvcConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Metric;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Observation.OpenTelemetry;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Observation.Serilog;
using TaskoMask.BuildingBlocks.Web.MVC.Services.AuthenticatedUser;

namespace TaskoMask.BuildingBlocks.Web.MVC.Configuration.MVC;
Expand All @@ -17,44 +17,44 @@ public static class MvcConfiguration
/// <summary>
///
/// </summary>
public static void AddMvcPreConfigured(this IServiceCollection services, IConfiguration configuration)
public static void AddMvcPreConfigured(this WebApplicationBuilder builder)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
ArgumentNullException.ThrowIfNull(builder);

services.AddControllersWithViews();
builder.AddCustomSerilog();

services.AddHttpContextAccessor();
builder.Services.AddControllersWithViews();

services.AddAuthenticatedUserService();
builder.Services.AddHttpContextAccessor();

services.AddWebServerOptions();
builder.Services.AddAuthenticatedUserService();

services.AddMetrics(configuration);
builder.Services.AddWebServerOptions();

builder.AddOpenTelemetry();
}

/// <summary>
///
/// </summary>
public static void UseMvcPreConfigured(this IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration)
public static void UseMvcPreConfigured(this WebApplication app)
{
if (app == null)
throw new ArgumentNullException(nameof(app));
ArgumentNullException.ThrowIfNull(app);

if (!env.IsDevelopment())
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error/Unknown");
app.UseHsts();
}

app.UseCustomSerilog();

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseMetrics(configuration);

app.UseAuthentication();

app.UseAuthorization();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Metric;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Observation.OpenTelemetry;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Observation.Serilog;
using TaskoMask.BuildingBlocks.Web.MVC.Services.AuthenticatedUser;

namespace TaskoMask.BuildingBlocks.Web.MVC.Configuration.MVC;
Expand All @@ -16,39 +16,39 @@ public static class RazorPagesConfiguration
/// <summary>
///
/// </summary>
public static void AddRazorPagesPreConfigured(this IServiceCollection services, IConfiguration configuration)
public static void AddRazorPagesPreConfigured(this WebApplicationBuilder builder)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
ArgumentNullException.ThrowIfNull(builder);

services.AddRazorPages();
builder.AddCustomSerilog();

services.AddAuthentication();
builder.Services.AddRazorPages();

services.AddHttpContextAccessor();
builder.Services.AddAuthentication();

services.AddAuthenticatedUserService();
builder.Services.AddHttpContextAccessor();

services.AddMetrics(configuration);
builder.Services.AddAuthenticatedUserService();

builder.AddOpenTelemetry();
}

/// <summary>
///
/// </summary>
public static void UseRazorPagesPreConfigured(this IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration)
public static void UseRazorPagesPreConfigured(this WebApplication app)
{
if (app == null)
throw new ArgumentNullException(nameof(app));
ArgumentNullException.ThrowIfNull(app);

app.UseCustomSerilog();

if (env.IsDevelopment())
if (app.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();

app.UseStaticFiles();

app.UseRouting();

app.UseMetrics(configuration);

app.UseAuthorization();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Jwt;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Metric;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Observation.OpenTelemetry;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Observation.Serilog;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Swagger;
using TaskoMask.BuildingBlocks.Web.MVC.Exceptions;
using TaskoMask.BuildingBlocks.Web.MVC.Services.AuthenticatedUser;
Expand All @@ -19,42 +20,46 @@ public static class WebApiConfiguration
/// <summary>
///
/// </summary>
public static void AddWebApiPreConfigured(this IServiceCollection services, IConfiguration configuration)
public static void AddWebApiPreConfigured(this WebApplicationBuilder builder)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(builder);

services.AddControllers().WithPreventAutoValidation();
builder.AddCustomSerilog();

services.AddSwaggerPreConfigured(options =>
builder.Services.AddControllers().WithPreventAutoValidation();

builder.Services.AddSwaggerPreConfigured(options =>
{
configuration.GetSection("Swagger").Bind(options);
builder.Configuration.GetSection("Swagger").Bind(options);
});

services.AddHttpContextAccessor();
builder.Services.AddHttpContextAccessor();

services.AddAuthenticatedUserService();
builder.Services.AddAuthenticatedUserService();

services.AddWebServerOptions();
builder.Services.AddWebServerOptions();

services.AddJwtAuthentication(configuration);
builder.Services.AddJwtAuthentication(builder.Configuration);

services.AddCors();
builder.Services.AddCors();

services.AddMetrics(configuration);
builder.AddOpenTelemetry();

services.AddGlobalExceptionHandler();
builder.Services.AddGlobalExceptionHandler();
}

/// <summary>
///
/// </summary>
public static void UseWebApiPreConfigured(this IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration)
public static void UseWebApiPreConfigured(this WebApplication app)
{
ArgumentNullException.ThrowIfNull(app);

if (env.IsDevelopment())
if (app.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();

app.UseCustomSerilog();

app.UseGlobalExceptionHandler();

app.UseSwaggerPreConfigured();
Expand All @@ -65,8 +70,6 @@ public static void UseWebApiPreConfigured(this IApplicationBuilder app, IWebHost

app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());

app.UseMetrics(configuration);

app.UseAuthentication();

app.UseAuthorization();
Expand Down
8 changes: 6 additions & 2 deletions src/1-BuildingBlocks/Web.MVC/Web.MVC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
<PackageReference Include="Serilog.Exceptions" Version="8.1.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.1.1" />
<PackageReference Include="Grpc.AspNetCore" Version="2.49.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.49.0" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.0.1" />
<PackageReference Include="Grpc.Net.Client" Version="2.67.0" />
<PackageReference Include="OpenTelemetry" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.10.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/2-Services/Boards/Api/Boards.Read.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ private static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

var app = builder.ConfigureServices().ConfigurePipeline(builder.Configuration);
var app = builder.ConfigureServices().ConfigurePipeline();

app.Run();
}
Expand Down
10 changes: 3 additions & 7 deletions src/2-Services/Boards/Api/Boards.Read.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Serilog;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Grpc;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.MVC;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Serilog;
using TaskoMask.Services.Boards.Read.Api.Configuration;
using TaskoMask.Services.Boards.Read.Api.Infrastructure.DbContext;
using TaskoMask.Services.Boards.Read.Api.Infrastructure.DI;
Expand All @@ -17,11 +15,9 @@ internal static class Startup
/// </summary>
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
{
builder.AddCustomSerilog();

builder.Services.AddModules(builder.Configuration);

builder.Services.AddWebApiPreConfigured(builder.Configuration);
builder.AddWebApiPreConfigured();

builder.Services.AddGrpcPreConfigured();

Expand All @@ -33,11 +29,11 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
/// <summary>
///
/// </summary>
public static WebApplication ConfigurePipeline(this WebApplication app, IConfiguration configuration)
public static WebApplication ConfigurePipeline(this WebApplication app)
{
app.UseSerilogRequestLogging();

app.UseWebApiPreConfigured(app.Environment, configuration);
app.UseWebApiPreConfigured();

app.Services.InitialDatabase();

Expand Down
15 changes: 8 additions & 7 deletions src/2-Services/Boards/Api/Boards.Read.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"UserName": "guest",
"Password": "guest"
},
"Metric": {
"StandAloneKestrelServerEnabled": false,
"Port": 5025,
"Url": "/metrics",
"Hostname": "+",
"HttpMetricsEnabled": true,
"SuppressDefaultMetrics": false
"OpenTelemetry": {
"ServiceName": "TaskoMask",
"ExportMetrics": true,
"MetricsEndpoint": "http://localhost:4317",
"ExportTracing": true,
"TracingEndpoint": "http://localhost:4317",
"ExportLogging": false,
"LoggingEndpoint": "http://localhost:4317"
},
"Caching": {
"CacheTimeInMinutes": 60,
Expand Down
2 changes: 1 addition & 1 deletion src/2-Services/Boards/Api/Boards.Write.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ private static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

var app = builder.ConfigureServices().ConfigurePipeline(builder.Configuration);
var app = builder.ConfigureServices().ConfigurePipeline();

app.Run();
}
Expand Down
10 changes: 3 additions & 7 deletions src/2-Services/Boards/Api/Boards.Write.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Serilog;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.MVC;
using TaskoMask.BuildingBlocks.Web.MVC.Configuration.Serilog;
using TaskoMask.Services.Boards.Write.Api.Infrastructure.CrossCutting.DI;
using TaskoMask.Services.Boards.Write.Api.Infrastructure.Data.DbContext;

Expand All @@ -15,23 +13,21 @@ internal static class Startup
/// </summary>
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
{
builder.AddCustomSerilog();

builder.Services.AddModules(builder.Configuration);

builder.Services.AddWebApiPreConfigured(builder.Configuration);
builder.AddWebApiPreConfigured();

return builder.Build();
}

/// <summary>
///
/// </summary>
public static WebApplication ConfigurePipeline(this WebApplication app, IConfiguration configuration)
public static WebApplication ConfigurePipeline(this WebApplication app)
{
app.UseSerilogRequestLogging();

app.UseWebApiPreConfigured(app.Environment, configuration);
app.UseWebApiPreConfigured();

app.Services.InitialDatabasesAndSeedEssentialData();

Expand Down
15 changes: 8 additions & 7 deletions src/2-Services/Boards/Api/Boards.Write.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
"Password": "Password",
"KeyNamespace": "TaskoMaskEventStore"
},
"Metric": {
"StandAloneKestrelServerEnabled": false,
"Port": 5023,
"Url": "/metrics",
"Hostname": "+",
"HttpMetricsEnabled": true,
"SuppressDefaultMetrics": false
"OpenTelemetry": {
"ServiceName": "TaskoMask",
"ExportMetrics": true,
"MetricsEndpoint": "http://localhost:4317",
"ExportTracing": true,
"TracingEndpoint": "http://localhost:4317",
"ExportLogging": false,
"LoggingEndpoint": "http://localhost:4317"
},
"Jwt": {
"Authority": "https://identity.taskomask.ir",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<AssemblyName>TaskoMask.Services.Identity.Api</AssemblyName>
<RootNamespace>TaskoMask.Services.Identity.Api</RootNamespace>
<UserSecretsId>755a7a1d-10e7-4626-9a60-81b74933b0b3</UserSecretsId>
<NoWarn>NETSDK1206</NoWarn>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..\..\..\..</DockerfileContext>
<DockerComposeProjectPath>..\..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
Expand Down
2 changes: 1 addition & 1 deletion src/2-Services/Identity/Api/Identity.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ private static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

var app = builder.ConfigureServices().ConfigurePipeline(builder.Configuration);
var app = builder.ConfigureServices().ConfigurePipeline();

app.Run();
}
Expand Down
Loading

0 comments on commit 2a9fa61

Please sign in to comment.