added use infrastructure

master
Young 6 months ago
parent f4b16bd7a3
commit 1c969999ba

@ -1,13 +1,45 @@
using Asp.Versioning.ApiExplorer; using Asp.Versioning.ApiExplorer;
using Infrastructure.Seed; using Infrastructure.Middlewares;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.HttpOverrides;
using Serilog; using Serilog;
namespace Infrastructure.Extensions; namespace Infrastructure.Extensions;
public static class ApplicationBuilderExtension 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<NotFoundMiddleware>()
.UseAuthentication()
.UseAuthorization()
.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.MapControllers();
app.Run();
return app;
}
public static IApplicationBuilder UseDefaultSwaggerUI(this IApplicationBuilder app) public static IApplicationBuilder UseDefaultSwaggerUI(this IApplicationBuilder app)
{ {
ArgumentNullException.ThrowIfNull(app); ArgumentNullException.ThrowIfNull(app);
@ -27,9 +59,9 @@ public static class ApplicationBuilderExtension
public static IApplicationBuilder UseDefaultExceptionHandler(this IApplicationBuilder app) public static IApplicationBuilder UseDefaultExceptionHandler(this IApplicationBuilder app)
{ {
ArgumentNullException.ThrowIfNull(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.ContentType = "application/json";
context.Response.StatusCode = StatusCodes.Status200OK; context.Response.StatusCode = StatusCodes.Status200OK;
@ -38,7 +70,7 @@ public static class ApplicationBuilderExtension
var message = new MessageData(false, "unknown error", 500); var message = new MessageData(false, "unknown error", 500);
Log.Logger.Error(exceptionHandlerPathFeature?.Error.Message!); Log.Logger.Error(exceptionHandlerPathFeature?.Error.Message!);
await context.Response.WriteAsync(message.Serialize()); await context.Response.WriteAsync(message.Serialize());
}); });
}); });
return app; return app;

Loading…
Cancel
Save