From 1c969999badb2bdd21bf0d2c0986cb1331f7b07f Mon Sep 17 00:00:00 2001 From: Young Date: Fri, 1 Nov 2024 22:57:55 +0800 Subject: [PATCH] added use infrastructure --- .../Extensions/ApplicationBuilderExtension.cs | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Infrastructure/Extensions/ApplicationBuilderExtension.cs b/src/Infrastructure/Extensions/ApplicationBuilderExtension.cs index 489ca5d..bd967f7 100644 --- a/src/Infrastructure/Extensions/ApplicationBuilderExtension.cs +++ b/src/Infrastructure/Extensions/ApplicationBuilderExtension.cs @@ -1,13 +1,45 @@ using Asp.Versioning.ApiExplorer; -using Infrastructure.Seed; +using Infrastructure.Middlewares; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics; +using Microsoft.AspNetCore.HttpOverrides; using Serilog; namespace Infrastructure.Extensions; public static class ApplicationBuilderExtension { + public static IApplicationBuilder UseDefaultInfrastructure(this WebApplication app) + { + ArgumentNullException.ThrowIfNull(app); + if (app.Environment.IsDevelopment() || app.Environment.IsStaging()) + { + app.UseSwagger(); + app.UseDefaultSwaggerUI(); + app.MapGet("/", request => + { + request.Response.Redirect("/swagger"); + return Task.CompletedTask; + }); + } + + app.UseDefaultExceptionHandler() + .UseRouting() + .UseCors(Options.CorsOptions.SectionName) + .UseMiddleware() + .UseAuthentication() + .UseAuthorization() + .UseForwardedHeaders(new ForwardedHeadersOptions + { + ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto + }); + app.MapControllers(); + + app.Run(); + + return app; + } + public static IApplicationBuilder UseDefaultSwaggerUI(this IApplicationBuilder app) { ArgumentNullException.ThrowIfNull(app); @@ -27,9 +59,9 @@ public static class ApplicationBuilderExtension public static IApplicationBuilder UseDefaultExceptionHandler(this IApplicationBuilder app) { ArgumentNullException.ThrowIfNull(app); - app.UseExceptionHandler(options => + app.UseExceptionHandler(options => { - options.Run(async context=> + options.Run(async context => { context.Response.ContentType = "application/json"; context.Response.StatusCode = StatusCodes.Status200OK; @@ -38,7 +70,7 @@ public static class ApplicationBuilderExtension var message = new MessageData(false, "unknown error", 500); Log.Logger.Error(exceptionHandlerPathFeature?.Error.Message!); await context.Response.WriteAsync(message.Serialize()); - }); + }); }); return app;