Files
monorepo/LCU.API.Internal.LoanRate/LCU.API.Internal.LoanRate/Program.cs
T
2026-04-07 14:21:04 -05:00

329 lines
14 KiB
C#

using System.Text.Json.Serialization;
using Microsoft.OpenApi.Models;
using Microsoft.EntityFrameworkCore;
using MassTransit;
using Newtonsoft.Json;
using LCU.Library.Internal.Configuration;
using LCU.Library.Internal.Logging;
using LCU.Library.Internal.Logging.Middleware;
using LCU.Library.Internal.Logging.Models;
using LCU.Library.Internal.Status;
using LCU.Library.Internal.LcuMassTransitModels;
using LCU.Library.Internal.LcuMassTransit;
using LCU.Library.Internal.LcuHttpClient;
using LCU.API.Internal.LoanRate.Configurations;
using LoadTestingLibAppDbContext = LCU.API.Internal.LoanRateLoadTestingLib.Persistence.AppDbContext;
using LoadTestingLibRepository = LCU.API.Internal.LoanRateLoadTestingLib.Repositories;
using LCU.API.Internal.LoanRateLoadTestingLib.Repositories;
using LCU.API.Internal.LoanRate.Services;
using LCU.API.Internal.LoanRate.Services.LoanRateOperationServices;
using LoanRateLibRepository = LCU.API.Internal.LoanRateLib.Repositories;
using LoanRateLibPersistence = LCU.API.Internal.LoanRateLib.Persistence;
using LCU.API.Internal.LoanRateLib.Repositories;
using LCU.API.Internal.LoanRate.Services.ValidateLoanRateServices;
using LCU.API.Internal.LoanRate.Consumers;
using LCU.API.Internal.LoanRate.Services.LoadTestingServices;
using LCU.API.Internal.LoanRate.Services.QueueServices;
using LCU.API.Internal.LoanRate.DataAccess;
using LCU.API.Internal.LoanRate.Services.MicroApiServices;
using LCU.API.Internal.LoanRateLib.Configurations;
using LCU.Library.Internal.LcuClassPatterns.Crud;
using LCU.Library.Internal.LcuEvaluation;
string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
var builder = BuildBuilder(args);
BuildServicesAndConfigurations(builder);
BuildAppAndRun(builder);
WebApplicationBuilder BuildBuilder(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("/app/config/environment-appsettings/appsettings.json", optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile("/app/config/mapped-appsettings/appsettings.json", optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile("/vault/secrets/loan-rate-queue-password.json", optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile("/vault/secrets/loan-rate-queue-username.json", optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile("/vault/secrets/loan-rate-evaluation-connection-string.json", optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile("/vault/secrets/loan-rate-load-test-connection-string.json", optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile("/vault/secrets/loan-rate-loan-rate-connection-string.json", optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile("/vault/secrets/loan-rate-seq-api-key.json", optional: true, reloadOnChange: true);
builder.Host.UseSerilog();
return builder;
}
void BuildServicesAndConfigurations(WebApplicationBuilder builder)
{
var services = builder.Services;
var configuration = builder.Configuration;
var apiConfiguration = configuration.Bind<ApiConfiguration>(services, "Primary");
configuration.Bind<FileOperationsApiConfiguration>(services);
configuration.Bind<RateCacheConfiguration>(services);
configuration.Bind<RateSheetCacheConfiguration>(services);
var connectionConfiguration = MassTransitConfigurationSetUp.AddConnectionConfiguration(ref services, configuration);
//debugging
Console.WriteLine(JsonConvert.SerializeObject(connectionConfiguration));
var receiveEndpointConfigurations = new ReceiveEndpointConfigurations()
{
LoadTestInitiateReceiveEndpoint = MassTransitConfigurationSetUp.GetReceiveEndpointConfiguration(configuration,
"LoadTestInitiateReceiveEndpoint"),
LoadTestProcessRateReceiveEndpoint = MassTransitConfigurationSetUp.GetReceiveEndpointConfiguration(configuration,
"LoadTestProcessRateReceiveEndpoint"),
LoadTestRateCompletionReceiveEndpoint = MassTransitConfigurationSetUp.GetReceiveEndpointConfiguration(configuration,
"LoadTestRateCompletionReceiveEndpoint")
};
services.AddSingleton(receiveEndpointConfigurations);
//debugging
Console.WriteLine(JsonConvert.SerializeObject(receiveEndpointConfigurations));
var loggerConfiguration = new LoggingConfiguration(apiConfiguration.AppName,
apiConfiguration.SeqApiKey,
apiConfiguration.SeqUrl);
var lcuLogging = new LcuLogging(loggerConfiguration);
lcuLogging.UseLcuLogging();
var loanRateSqlConnectionConfiguration = configuration.Bind<LoanRateSqlConnectionConfiguration>(services);
services.AddDbContext<LoanRateLibPersistence.AppDbContext>(options =>
{
options.UseSqlServer
(
loanRateSqlConnectionConfiguration.ConnectionString,
sql => sql.EnableRetryOnFailure(loanRateSqlConnectionConfiguration.MaxRetryCount,
loanRateSqlConnectionConfiguration.MaxRetryDelay,
null)
);
});
var loanRateLoadTestingSqlConnectionConfiguration = configuration.Bind<LoanRateLoadTestingSqlConnectionConfiguration>(services);
services.AddDbContext<LoadTestingLibAppDbContext>(options =>
{
options.UseSqlServer
(
loanRateLoadTestingSqlConnectionConfiguration.ConnectionString,
sql => sql.EnableRetryOnFailure(loanRateLoadTestingSqlConnectionConfiguration.MaxRetryCount,
loanRateLoadTestingSqlConnectionConfiguration.MaxRetryDelay,
null)
);
});
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins(
apiConfiguration.CorsAllowedOrigins
).AllowAnyHeader().AllowAnyMethod();
});
});
AddMassTransitServices(ref services, connectionConfiguration, receiveEndpointConfigurations, apiConfiguration);
services.AddLCUHttpServices();
AddLoanRateLib(services);
AddLoanRateLoadTestingLib(services);
AddDataAccess(services);
AddApiServices(services);
services.AddLcuEvaluationServices();
services.AddLcuEvaluationCrudServices(configuration);
services.AddLCUStatusServicesWithApiHealthCheck(configuration);
services.AddLcuCrudServices();
services.AddHttpClient();
services.AddMemoryCache();
services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = $"{apiConfiguration.AppName}", Version = "v1" });
});
}
void AddMassTransitServices(ref IServiceCollection services,
ConnectionConfiguration connectionConfiguration,
ReceiveEndpointConfigurations receiveEndpointConfigurations,
ApiConfiguration apiConfiguration)
{
services.AddMassTransit(x =>
{
x.AddConsumer<LoadTestInitiateConsumer>();
x.AddConsumer<LoadTestProcessRateConsumer>();
x.AddConsumer<LoadTestRateCompletionConsumer>();
x.UsingRabbitMq((context, cfg) =>
{
cfg.Host(connectionConfiguration.HostUrl, "/", host =>
{
host.Username(connectionConfiguration.UserName);
host.Password(connectionConfiguration.Password);
});
cfg.UseTimeout(t => t.Timeout = TimeSpan.FromSeconds(420));
if (apiConfiguration.FeatureFlags.RunLoadTestInitiateConsumer)
{
cfg.ReceiveEndpoint(receiveEndpointConfigurations.LoadTestInitiateReceiveEndpoint.ReceiveEndpoint, e =>
{
e.ConfigureConsumer<LoadTestInitiateConsumer>(context);
e.SetQuorumQueue();
e.PrefetchCount = receiveEndpointConfigurations.LoadTestInitiateReceiveEndpoint.PrefetchLimit;
e.UseTimeout(t => t.Timeout = TimeSpan.FromSeconds(420));
});
}
if (apiConfiguration.FeatureFlags.RunLoadTestProcessRateConsumer)
{
cfg.ReceiveEndpoint(receiveEndpointConfigurations.LoadTestProcessRateReceiveEndpoint.ReceiveEndpoint, e =>
{
e.ConfigureConsumer<LoadTestProcessRateConsumer>(context);
e.SetQuorumQueue();
e.PrefetchCount = receiveEndpointConfigurations.LoadTestProcessRateReceiveEndpoint.PrefetchLimit;
e.UseTimeout(t => t.Timeout = TimeSpan.FromSeconds(420));
});
}
if (apiConfiguration.FeatureFlags.RunLoadTestRateCompletionConsumer)
{
cfg.ReceiveEndpoint(receiveEndpointConfigurations.LoadTestRateCompletionReceiveEndpoint.ReceiveEndpoint, e =>
{
e.ConfigureConsumer<LoadTestRateCompletionConsumer>(context, cons =>
{
//TODO: Will need to add optional configuration in our LCU Masstransit Library for these configurables.
cons.Options<BatchOptions>(options => options
.SetMessageLimit(100)
.SetTimeLimit(5000)
.SetConcurrencyLimit(1));
});
e.SetQuorumQueue();
e.PrefetchCount = receiveEndpointConfigurations.LoadTestRateCompletionReceiveEndpoint.PrefetchLimit;
e.UseTimeout(t => t.Timeout = TimeSpan.FromSeconds(420));
});
}
});
});
services.AddMassTransitHostedService();
}
void AddApiServices(IServiceCollection services)
{
services.AddScoped<IStatusService, StatusService>();
AddLoadTestingServices(services);
AddLoanRateOperationServices(services);
AddValidateLoanRateServices(services);
AddMicroApiServices(services);
AddQueueServices(services);
services.AddScoped<IRateUploadInfoService, RateUploadInfoService>();
services.AddScoped<IRateSheetService, RateSheetService>();
services.AddScoped<IRateSheetCellService, RateSheetCellService>();
services.AddScoped<IRateService, RateService>();
services.AddScoped<IRateEvaluationService, RateEvaluationService>();
services.AddScoped<IComplexEvaluationDetailService, ComplexEvaluationDetailService>();
services.AddScoped<ILoadTestRunInfoService, LoadTestRunInfoService>();
services.AddScoped<IRateProcessingInfoService, RateProcessingInfoService>();
}
void AddLoadTestingServices(IServiceCollection services)
{
services.AddScoped<ILoadTestInitiationService, LoadTestInitiationService>();
services.AddScoped<ILoadTestProcessRateService, LoadTestProcessRateService>();
services.AddScoped<ILoadTestRateCompletionService, LoadTestRateCompletionService>();
}
void AddLoanRateOperationServices(IServiceCollection services)
{
services.AddScoped<IRateFinderService, RateFinderService>();
services.AddScoped<IRateValidationService, RateValidationService>();
services.AddScoped<IRateSheetCellUploaderService, RateSheetCellUploaderService>();
}
void AddValidateLoanRateServices(IServiceCollection services)
{
services.AddScoped<IValidateRateEvaluationsService, ValidateRateEvaluationsService>();
}
void AddMicroApiServices(IServiceCollection services)
{
services.AddScoped<IFileOperationsApiService, FileOperationsApiService>();
}
void AddQueueServices(IServiceCollection services)
{
services.AddScoped<IQueueLoadTestInitiateService, QueueLoadTestInitiateService>();
services.AddScoped<IQueueLoadTestProcessRateService, QueueLoadTestProcessRateService>();
services.AddScoped<IQueueLoadTestRateCompletionService, QueueLoadTestRateCompletionService>();
}
void AddDataAccess(IServiceCollection services)
{
services.AddScoped<IFileOperationsApiDao, FileOperationsApiDao>();
}
void AddLoanRateLib(IServiceCollection services)
{
services.AddScoped<LoanRateLibRepository.IUnitOfWork, LoanRateLibRepository.UnitOfWork>();
services.AddScoped<IRateSheetRepository, RateSheetRepository>();
services.AddScoped<IRateSheetCellRepository, RateSheetCellRepository>();
services.AddScoped<IRateUploadInfoRepository, RateUploadInfoRepository>();
services.AddScoped<IRateRepository, RateRepository>();
services.AddScoped<IRateEvaluationRepository, RateEvaluationRepository>();
}
void AddLoanRateLoadTestingLib(IServiceCollection services)
{
services.AddScoped<LoadTestingLibRepository.IUnitOfWork, LoadTestingLibRepository.UnitOfWork>();
services.AddScoped<ILoadTestRunInfoRepository, LoadTestRunInfoRepository>();
services.AddScoped<IRateProcessingInfoRepository, RateProcessingInfoRepository>();
}
void BuildAppAndRun(WebApplicationBuilder builder)
{
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((swaggerDoc, request) =>
{
const string prefixHeader = "X-Forwarded-Prefix";
if (!request.Headers.ContainsKey(prefixHeader)) return;
var serverUrl = request.Headers[prefixHeader];
if (!string.IsNullOrWhiteSpace(serverUrl))
{
swaggerDoc.Servers = new List<OpenApiServer>
{
new() { Description = serverUrl, Url = serverUrl }
};
}
});
});
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "swagger";
c.SwaggerEndpoint("v1/swagger.json", "LCU.API.Internal.LoanRate v1");
});
app.UseMiddleware<RequestResponseLoggingMiddleware>();
app.UseSerilogRequestLogging(options =>
{
options.EnrichDiagnosticContext = RequestResponseLoggingMiddleware.RequestDataParser.EnrichFromRequest;
});
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseCors(MyAllowSpecificOrigins);
app.UseHttpsRedirection();
app.MapControllers();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
app.Run();
}