208 lines
8.6 KiB
C#
208 lines
8.6 KiB
C#
using System;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.OpenApi.Models;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Newtonsoft.Json;
|
|
using MassTransit;
|
|
using Serilog;
|
|
using LCU.API.Internal.MaidBotApi.HostedService;
|
|
using LCU.Api.Internal.MaidBotApi.Services;
|
|
using LCU.Api.Internal.MaidBotApi.EnvironmentView;
|
|
using LCU.Api.Internal.MaidBotLib.Repositories;
|
|
using LCU.Api.Internal.MaidBotLib.Persistence;
|
|
using LCU.Library.Internal.LcuMassTransitModels;
|
|
using LCU.Library.Internal.LcuMassTransit;
|
|
using LCU.Api.Internal.MaidBotApi.Consumers;
|
|
using LCU.Api.Internal.MaidBotApi.Configurations;
|
|
using LCU.Library.Internal.Logging.Models;
|
|
using LCU.Library.Internal.Logging;
|
|
using LCU.Library.Internal.Logging.Middleware;
|
|
using LCU.Library.Internal.LcuHttpClient;
|
|
using LCU.Api.Internal.MaidBotApi.DataAccesses;
|
|
namespace LCU.API.Internal.MaidBot;
|
|
|
|
public class Program
|
|
{
|
|
static string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
|
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = BuildBuilder(args);
|
|
BuildServicesAndConfigurations(builder);
|
|
BuildAppAndRun(builder);
|
|
}
|
|
|
|
private static WebApplicationBuilder BuildBuilder(string[] args)
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
builder.Host.UseSerilog();
|
|
builder.Configuration.AddJsonFile("/app/config/environment-appsettings/appsettings.json", optional: true, reloadOnChange: true);
|
|
builder.Configuration.AddJsonFile("/app/config/workload-appsettings/appsettings.json", optional: true, reloadOnChange: true);
|
|
builder.Configuration.AddJsonFile("/vault/secrets/maidbot-api-seq-api-key.json", optional: true, reloadOnChange: true);
|
|
builder.Configuration.AddJsonFile("/vault/secrets/maidbot-api-connection-configuration-password.json", optional: true, reloadOnChange: true);
|
|
builder.Configuration.AddJsonFile("/vault/secrets/maidbot-api-connection-configuration-username.json", optional: true, reloadOnChange: true);
|
|
builder.Configuration.AddJsonFile("/vault/secrets/maidbot-database-connection.json", optional: true, reloadOnChange: true);
|
|
return builder;
|
|
}
|
|
|
|
static void BuildServicesAndConfigurations(WebApplicationBuilder builder)
|
|
{
|
|
var services = builder.Services;
|
|
var configuration = builder.Configuration;
|
|
services.AddControllers();
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
services.AddEndpointsApiExplorer();
|
|
services.AddSwaggerGen();
|
|
|
|
services.AddDbContext<AppDbContext>(options =>
|
|
{
|
|
options.UseSqlServer(configuration.GetSection("Connections").GetSection("SQL").GetSection("MaidBotDB").Value);
|
|
});
|
|
|
|
services.AddHttpClient();
|
|
services.AddLCUHttpServices();
|
|
|
|
//Configurations
|
|
var apiConfiguration = new ApiConfiguration();
|
|
configuration.GetSection("Primary").Bind(apiConfiguration);
|
|
services.AddSingleton(apiConfiguration);
|
|
//debugging
|
|
Console.WriteLine(JsonConvert.SerializeObject(apiConfiguration));
|
|
|
|
var connectionConfiguration = MassTransitConfigurationSetUp.AddConnectionConfiguration(ref services, configuration);
|
|
//debugging
|
|
Console.WriteLine(JsonConvert.SerializeObject(connectionConfiguration));
|
|
|
|
var receiveEndpointConfiguration = new ReceiveEndpointConfiguration();
|
|
receiveEndpointConfiguration = MassTransitConfigurationSetUp.GetReceiveEndpointConfiguration(configuration, "ReceiveEndpoint");
|
|
services.AddSingleton(receiveEndpointConfiguration);
|
|
//debugging
|
|
Console.WriteLine(JsonConvert.SerializeObject(receiveEndpointConfiguration));
|
|
|
|
//EnvironmentView
|
|
services.AddScoped<IEnvironmentView, EnvironmentView>();
|
|
|
|
var loggerConfiguration = new LoggingConfiguration(apiConfiguration.AppName,
|
|
apiConfiguration.SeqApiKey,
|
|
apiConfiguration.SeqUrl);
|
|
var lcuLogging = new LcuLogging(loggerConfiguration);
|
|
lcuLogging.UseLcuLogging();
|
|
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddPolicy(MyAllowSpecificOrigins,
|
|
builder =>
|
|
{
|
|
builder.WithOrigins(
|
|
apiConfiguration.CorsAllowedOrigins
|
|
).AllowAnyHeader().AllowAnyMethod();
|
|
});
|
|
});
|
|
|
|
AddMaidBotServices(ref services);
|
|
AddMaidBotLib(ref services);
|
|
AddMaidBotDataAccesses(ref services);
|
|
AddMassTransitServices(ref services, connectionConfiguration, receiveEndpointConfiguration, apiConfiguration);
|
|
|
|
// The Hosted
|
|
services.AddHostedService<SchedulerHostedService>();
|
|
|
|
// Register the Swagger generator, defining 1 or more Swagger documents
|
|
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = $"{apiConfiguration.AppName}", Version = "v1" }); });
|
|
|
|
services.Configure<ApiBehaviorOptions>(options =>
|
|
{
|
|
options.SuppressModelStateInvalidFilter = true;
|
|
});
|
|
}
|
|
|
|
static void AddMaidBotServices(ref IServiceCollection services)
|
|
{
|
|
services.AddScoped<ILogDistributionService, LogDistributionService>();
|
|
services.AddScoped<IQueueEventLogService, QueueEventLogService>();
|
|
services.AddScoped<INotificationsApiService, NotificationsApiService>();
|
|
services.AddScoped<ILoggingMatrixService, LoggingMatrixService>();
|
|
services.AddScoped<IEventLogService, EventLogService>();
|
|
services.AddScoped<IDebugEventLogService, DebugEventLogService>();
|
|
}
|
|
|
|
static void AddMaidBotLib(ref IServiceCollection services)
|
|
{
|
|
services.AddScoped<ILoggingMatrixRepository, LoggingMatrixRepository>();
|
|
services.AddScoped<IEventLogRepository, EventLogRepository>();
|
|
services.AddScoped<IDebugEventLogRepository, DebugEventLogRepository>();
|
|
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
|
}
|
|
|
|
static void AddMaidBotDataAccesses(ref IServiceCollection services)
|
|
{
|
|
services.AddScoped<INotificationsApiDao, NotificationsApiDao>();
|
|
}
|
|
|
|
static void AddMassTransitServices(ref IServiceCollection services,
|
|
ConnectionConfiguration connectionConfiguration,
|
|
ReceiveEndpointConfiguration receiveEndpointConfiguration,
|
|
ApiConfiguration apiConfiguration)
|
|
{
|
|
services.AddMassTransit(x =>
|
|
{
|
|
x.AddConsumer<LoggingEventConsumer>();
|
|
|
|
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.LoggingEventConsumer)
|
|
{
|
|
cfg.ReceiveEndpoint(receiveEndpointConfiguration.ReceiveEndpoint, e =>
|
|
{
|
|
e.ConfigureConsumer<LoggingEventConsumer>(context);
|
|
e.SetQuorumQueue();
|
|
e.PrefetchCount = receiveEndpointConfiguration.PrefetchLimit;
|
|
e.UseTimeout(t => t.Timeout = TimeSpan.FromSeconds(420));
|
|
});
|
|
}
|
|
});
|
|
});
|
|
services.AddMassTransitHostedService();
|
|
}
|
|
|
|
static void BuildAppAndRun(WebApplicationBuilder builder)
|
|
{
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "LCU.Api.Internal.MaidBotApi v1"));
|
|
app.UseMiddleware<RequestResponseLoggingMiddleware>();
|
|
app.UseSerilogRequestLogging(options =>
|
|
{
|
|
options.EnrichDiagnosticContext = RequestResponseLoggingMiddleware.RequestDataParser.EnrichFromRequest;
|
|
});
|
|
|
|
app.UseRouting();
|
|
app.UseCors(MyAllowSpecificOrigins);
|
|
app.UseHttpsRedirection();
|
|
app.UseAuthorization();
|
|
app.MapControllers();
|
|
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
|
|
|
app.Run();
|
|
}
|
|
}
|