using Hangfire; using Hangfire.MemoryStorage; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.OpenApi.Models; using Newsbot.Collector.Api; using Newsbot.Collector.Api.Authentication; using Newsbot.Collector.Domain.Consts; using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Domain.Models.Config; using Serilog; using Serilog.Events; using ILogger = Serilog.ILogger; var builder = WebApplication.CreateBuilder(args); // Define Logger builder.Host.UseSerilog(); // Build the config var config = GetConfiguration(); builder.Configuration.AddConfiguration(config); Log.Logger = GetLogger(config); Log.Information("Starting up"); // Configure Hangfire builder.Services.AddHangfire(f => f.UseMemoryStorage()); builder.Services.AddHangfireServer(); GlobalConfiguration.Configuration.UseSerilogLogProvider(); builder.Services.AddHealthChecks() .AddNpgSql(config.GetValue(ConfigConnectionStringConst.Database) ?? ""); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.Configure(config.GetSection("ConnectionStrings")); builder.Services.Configure(config.GetSection(ConfigSectionsConst.ConnectionStrings)); builder.Services.Configure(config.GetSection(ConfigSectionsConst.Rss)); builder.Services.Configure(config.GetSection(ConfigSectionsConst.Youtube)); //builder.Services.Configure< builder.Services.AddSwaggerGen(cfg => { cfg.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme { Description = "The API key to access the API", Type = SecuritySchemeType.ApiKey, Name = "x-api-key", In = ParameterLocation.Header, Scheme = "ApiKeyScheme" }); var scheme = new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "ApiKey" }, In = ParameterLocation.Header }; var requirement = new OpenApiSecurityRequirement { { scheme, new List() } }; cfg.AddSecurityRequirement(requirement); }); var app = builder.Build(); // Configure the HTTP request pipeline. if (config.GetValue("EnableSwagger")) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseHangfireDashboard(); BackgroundJobs.SetupRecurringJobs(config); app.UseAuthorization(); app.UseMiddleware(); app.MapHealthChecks("/health", new HealthCheckOptions { Predicate = _ => true, ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); app.MapControllers(); app.Run(); static IConfiguration GetConfiguration() { return new ConfigurationBuilder() .AddJsonFile("appsettings.json", true) .AddJsonFile("config/appsettings.json", true) .AddEnvironmentVariables() .Build(); } static ILogger GetLogger(IConfiguration configuration) { var otel = configuration.GetValue(ConfigConnectionStringConst.OpenTelemetry) ?? ""; if (otel == "") return Log.Logger = new LoggerConfiguration() .WriteTo.Console() .MinimumLevel.Information() .CreateLogger(); return Log.Logger = new LoggerConfiguration() .WriteTo.Console() .MinimumLevel.Information() .WriteTo.OpenTelemetry( otel, resourceAttributes: new Dictionary { { "Application", "newsbot-collector-api"}, { "service.name", "newsbot-collector-api" } }) .CreateLogger(); }