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(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(); //Services services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); //Repositories services.AddScoped(); services.AddScoped(); services.AddScoped(); 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(); x.AddConsumer(); 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(context); e.PrefetchCount = receiveEndpointConfigurations.ArchiverReceiveEndpoint.PrefetchLimit; e.SetQuorumQueue(); }); } if (archiverApiConfig.FeatureFlags.RunResponseConsumer) { cfg.ReceiveEndpoint(receiveEndpointConfigurations.ResponseReceiveEndpoint.ReceiveEndpoint, e => { e.ConfigureConsumer(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(); app.UseSerilogRequestLogging(options => { options.EnrichDiagnosticContext = RequestResponseLoggingMiddleware.RequestDataParser.EnrichFromRequest; }); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }