Files
monorepo/LCU.API.Internal.ArchiverApi/LCU.Api.Internal.ArchiverApi/Startup.cs
T
2026-03-17 10:03:23 -05:00

151 lines
6.8 KiB
C#

using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Microsoft.EntityFrameworkCore;
using Serilog;
using MassTransit;
using LCU.Library.Internal.Logging.Models;
using LCU.Library.Internal.Logging;
using LCU.Library.Internal.Logging.Middleware;
using LCU.Api.Internal.ArchiverApi.Configuration;
using LCU.Api.Internal.ArchiverApi.Models;
using LCU.Api.Internal.ArchiverApi.Services;
using LCU.Library.Internal.ArchiverApiLib.Repositories;
using LCU.Library.Internal.ArchiverApiLib.Persistence;
using LCU.Library.Internal.LcuMassTransit;
using LCU.Library.Internal.LcuMassTransitModels;
using LCU.Api.Internal.ArchiverApi.Consumers;
namespace LCU.Api.Internal.ArchiverApi;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHttpClient();
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(
Configuration.GetSection("Primary").GetSection("Connections").GetSection("SQL").GetSection("ArchiverApiDb").Value
);
});
var archiverApiConfig = new ArchiverApiConfig();
Configuration.GetSection("Primary").Bind(archiverApiConfig);
services.AddSingleton(archiverApiConfig);
var loggerConfiguration = new LoggingConfiguration(archiverApiConfig.ApplicationName,
archiverApiConfig.SeqApiKey,
archiverApiConfig.SeqUrl);
var lcuLogging = new LcuLogging(loggerConfiguration);
lcuLogging.UseLcuLogging();
var connectionConfiguration = MassTransitConfigurationSetUp.AddConnectionConfiguration(ref services, Configuration);
var receiveEndpointConfigurations = new ReceiveEndpointConfigurations()
{
ArchiverReceiveEndpoint = MassTransitConfigurationSetUp.GetReceiveEndpointConfiguration(Configuration,
"ArchiverReceiveEndpoint"),
ResponseReceiveEndpoint = MassTransitConfigurationSetUp.GetReceiveEndpointConfiguration(Configuration,
"ResponseReceiveEndpoint")
};
services.AddSingleton(receiveEndpointConfigurations);
AddMassTransitServices(ref services, connectionConfiguration, receiveEndpointConfigurations, archiverApiConfig);
services.AddScoped<IEnvironmentView, EnvironmentView>();
//Services
services.AddScoped<IReportService, ReportService>();
services.AddScoped<IApplicationMappingService, ApplicationMappingService>();
services.AddScoped<IRegexMappingService, RegexMappingService>();
services.AddScoped<IArchiverService, ArchiverService>();
services.AddScoped<IQueueArchiverService, QueueArchiverService>();
services.AddScoped<IArchiveCompletionService, ArchiveCompletionService>();
services.AddScoped<IFileOperationsService, FileOperationsService>();
services.AddScoped<INotificationService, NotificationService>();
services.AddScoped<ICoreApiWrapperService, CoreApiWrapperService>();
//Repositories
services.AddScoped<IApplicationMappingRepository, ApplicationMappingRepository>();
services.AddScoped<IRegexMappingRepository, RegexMappingRepository>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = $"{archiverApiConfig.ApplicationName}", Version = "v1" }); });
}
public void AddMassTransitServices(ref IServiceCollection services,
ConnectionConfiguration connectionConfiguration,
ReceiveEndpointConfigurations receiveEndpointConfigurations,
ArchiverApiConfig archiverApiConfig)
{
services.AddMassTransit(x =>
{
x.AddConsumer<ArchiverConsumer>();
x.AddConsumer<ResponseConsumer>();
x.UsingRabbitMq((context, cfg) =>
{
cfg.Host(connectionConfiguration.HostUrl, "/", host =>
{
host.Username(connectionConfiguration.UserName);
host.Password(connectionConfiguration.Password);
});
if (archiverApiConfig.FeatureFlags.RunArchiverConsumer)
{
cfg.ReceiveEndpoint(receiveEndpointConfigurations.ArchiverReceiveEndpoint.ReceiveEndpoint, e =>
{
e.ConfigureConsumer<ArchiverConsumer>(context);
e.PrefetchCount = receiveEndpointConfigurations.ArchiverReceiveEndpoint.PrefetchLimit;
e.SetQuorumQueue();
});
}
if (archiverApiConfig.FeatureFlags.RunResponseConsumer)
{
cfg.ReceiveEndpoint(receiveEndpointConfigurations.ResponseReceiveEndpoint.ReceiveEndpoint, e =>
{
e.ConfigureConsumer<ResponseConsumer>(context);
e.PrefetchCount = receiveEndpointConfigurations.ResponseReceiveEndpoint.PrefetchLimit;
e.SetQuorumQueue();
});
}
});
});
services.AddMassTransitHostedService();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "LCU.Api.Internal.ArchiverApi v1"));
app.UseMiddleware<RequestResponseLoggingMiddleware>();
app.UseSerilogRequestLogging(options => { options.EnrichDiagnosticContext = RequestResponseLoggingMiddleware.RequestDataParser.EnrichFromRequest; });
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}