DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Responses/Program.cs

115 lines
4.0 KiB
C#
Raw Normal View History

using DamageAssesment.Api.Responses.Db;
using DamageAssesment.Api.Responses.Interfaces;
using DamageAssesment.Api.Responses.Services;
using DamageAssesment.Api.Responses.Providers;
2023-08-15 22:52:30 -05:00
using Microsoft.EntityFrameworkCore;
using Polly;
using System.Reflection;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
2023-08-15 22:52:30 -05:00
var builder = WebApplication.CreateBuilder(args);
const int maxApiCallRetries = 3;
const int intervalToRetry = 2; //2 seconds
const int maxRetryForCircuitBraker = 5;
const int intervalForCircuitBraker = 5; //5 seconds
// Add services to the container.
var authkey = builder.Configuration.GetValue<string>("JwtSettings:securitykey");
builder.Services.AddAuthentication(item =>
{
item.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
item.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(item =>
{
item.RequireHttpsMetadata = true;
item.SaveToken = true;
item.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authkey)),
ValidateIssuer = false,
ValidateAudience = false,
ClockSkew = TimeSpan.Zero
};
});
2023-08-15 22:52:30 -05:00
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddScoped<ISurveysResponse, SurveyResponsesProvider>();
builder.Services.AddScoped<IAnswerServiceProvider, AnswerServiceProvider>();
builder.Services.AddScoped<ILocationServiceProvider, LocationServiceProvider>();
builder.Services.AddScoped<IRegionServiceProvider, RegionServiceProvider>();
builder.Services.AddScoped<IQuestionServiceProvider, QuestionServiceProvider>();
builder.Services.AddScoped<IEmployeeServiceProvider, EmployeeServiceProvider>();
builder.Services.AddScoped<IAttachmentServiceProvider, AttachmentServiceProvider>();
builder.Services.AddScoped<ISurveyServiceProvider, SurveyServiceProvider>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpClient<IHttpUtil, HttpUtil>().
2023-08-15 22:52:30 -05:00
AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(maxApiCallRetries, _ => TimeSpan.FromSeconds(intervalToRetry))).
AddTransientHttpErrorPolicy(policy => policy.CircuitBreakerAsync(maxRetryForCircuitBraker, TimeSpan.FromSeconds(intervalForCircuitBraker)));
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(options =>
{
// Include XML comments from your assembly
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
OpenApiSecurityScheme securityDefinition = new OpenApiSecurityScheme()
{
Name = "Bearer",
BearerFormat = "JWT",
Scheme = "bearer",
Description = "Specify the authorization token.",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
};
options.AddSecurityDefinition("jwt_auth", securityDefinition);
// Make sure swagger UI requires a Bearer token specified
OpenApiSecurityScheme securityScheme = new OpenApiSecurityScheme()
{
Reference = new OpenApiReference()
{
Id = "jwt_auth",
Type = ReferenceType.SecurityScheme
}
};
OpenApiSecurityRequirement securityRequirements = new OpenApiSecurityRequirement()
{
{securityScheme, new string[] { }},
};
options.AddSecurityRequirement(securityRequirements);
});
2023-08-15 22:52:30 -05:00
builder.Services.AddDbContext<SurveyResponseDbContext>(option =>
{
2023-08-18 16:14:41 -05:00
option.UseSqlServer("SurveyResponseConnection");
2023-08-15 22:52:30 -05:00
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthentication();
2023-08-15 22:52:30 -05:00
app.UseAuthorization();
app.MapControllers();
app.Run();