Created
December 1, 2022 18:52
-
-
Save SuperJC710e/cdf5fde65e3eb229d1b5ee07efec3dcc to your computer and use it in GitHub Desktop.
background-code.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FGSyspro.Api; | |
using FGSyspro.Api.Startup; | |
using FGSyspro.Core; | |
using FGSyspro.Infrastructure; | |
using FGSyspro.Infrastructure.ENET; | |
using Hellang.Middleware.ProblemDetails; | |
using Microsoft.AspNetCore.Authentication.JwtBearer; | |
using Microsoft.Identity.Web; | |
using System.Reflection; | |
using System.Text.Json.Serialization; | |
WebApplicationBuilder? builder = WebApplication.CreateBuilder(args); | |
// Add services to the container. | |
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | |
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd")); | |
builder.Services.AddProblemDetails(options => | |
{ | |
options.MapToStatusCode<BusinessObjectException>(StatusCodes.Status400BadRequest); | |
}); | |
builder.Services.AddControllers() | |
.AddJsonOptions(options => | |
{ | |
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); | |
}); | |
builder.Services.AddCors(opt => | |
opt.AddDefaultPolicy(cfg => | |
{ | |
if (builder.Environment.IsDevelopment()) | |
{ | |
cfg | |
.AllowAnyOrigin() | |
.AllowAnyHeader(); | |
return; | |
} | |
cfg | |
.WithOrigins(builder.Configuration.GetSection("Cors:Origins").Get<string[]>()); | |
})); | |
builder.Services.AddDefaultCore();//can register any | |
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); | |
InfrastructureOptions? infraOptions = builder.Configuration.GetRequiredSection("Infrastructure").Get<InfrastructureOptions>(); | |
builder.Services.AddSingleton(infraOptions); | |
builder.Services.AddScoped<IContextAcessor, ContextAcessor>(); | |
builder.Services.AddDefaultInfrastructure(infraOptions); | |
AzureAdOptions? azureAdOptions = builder.Configuration.GetRequiredSection("AzureAd").Get<AzureAdOptions>(); | |
SwaggerOptions? swaggerOptions = builder.Configuration.GetRequiredSection("SwaggerClient").Get<SwaggerOptions>(); | |
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | |
builder.Services.AddEndpointsApiExplorer(); | |
builder.Services.AddSwaggerGen(opt => | |
{ | |
string? xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | |
opt.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); | |
opt.AddMicrosoftIdentity(azureAdOptions); | |
}); | |
builder.Services.AddApplicationInsightsTelemetry(options => | |
{ | |
options.ConnectionString = builder.Configuration["APPINSIGHTS_CONNECTIONSTRING"]; | |
}); | |
EnetOptions? enetOptions = builder.Configuration.GetRequiredSection("ENet").Get<EnetOptions>(); | |
builder.Services.AddSingleton<EnetOptions>(enetOptions); | |
WebApplication? app = builder.Build(); | |
// Configure the HTTP request pipeline. | |
if (!app.Environment.IsProduction()) | |
{ | |
app.UseSwagger(); | |
app.UseSwaggerUI(opt => | |
opt.ConfigureMicrosoftIdentity(swaggerOptions)); | |
} | |
app.UseHttpsRedirection(); | |
app.UseCors(); | |
app.UseAuthentication(); | |
app.UseAuthorization(); | |
app.UseProblemDetails(); | |
app.MapControllers(); | |
app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment