using Hangfire; using Hangfire.MemoryStorage; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Newsbot.Collector.Domain.Consts; using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Services.Jobs; using Serilog; Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); Log.Information("Starting up"); var builder = WebApplication.CreateBuilder(args); // Define Logger builder.Host.UseSerilog(); // <-- Add this line // Build the config var config = GetConfiguration(); builder.Configuration.AddConfiguration(config); // 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")); var app = builder.Build(); // Configure the HTTP request pipeline. //if (app.Environment.IsDevelopment()) //{ app.UseSwagger(); app.UseSwaggerUI(); //} app.UseHttpsRedirection(); app.UseHangfireDashboard(); SetupRecurringJobs(config, Log.Logger); app.UseAuthorization(); app.MapHealthChecks("/health", new HealthCheckOptions { Predicate = _ => true, ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); app.MapControllers(); app.Run(); static IConfiguration GetConfiguration() { return new ConfigurationBuilder() .AddJsonFile("appsettings.json", true) .AddEnvironmentVariables() .Build(); } static void SetupRecurringJobs(IConfiguration configuration, Serilog.ILogger logger) { //RecurringJob.AddOrUpdate("Example", x => x.InitAndExecute(new HelloWorldJobOptions //{ // Message = "Hello from the background!" //}), "0/1 * * * *"); RecurringJob.AddOrUpdate("RSS", x => x.InitAndExecute(new RssWatcherJobOptions { ConnectionString = configuration.GetSection(ConfigConnectionStringConst.Database).Value ?? "" }), "15 0-23 * * *"); RecurringJob.AddOrUpdate("Discord Alerts", x => x.InitAndExecute(new DiscordNotificationJobOptions { DatabaseConnectionString = configuration.GetSection(ConfigConnectionStringConst.Database).Value ?? "" }), "5/10 * * * *"); }