2023-02-14 17:51:22 -08:00
|
|
|
using Hangfire;
|
|
|
|
using Hangfire.MemoryStorage;
|
2023-03-14 07:21:34 -07:00
|
|
|
using HealthChecks.UI.Client;
|
|
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
2023-07-23 16:20:16 -07:00
|
|
|
using Newsbot.Collector.Api.Startup;
|
2023-02-26 09:40:04 -08:00
|
|
|
using Newsbot.Collector.Domain.Consts;
|
2023-03-05 22:33:41 -08:00
|
|
|
using Newsbot.Collector.Domain.Models;
|
2023-03-31 22:49:39 -07:00
|
|
|
using Newsbot.Collector.Domain.Models.Config;
|
2023-07-06 22:22:55 -07:00
|
|
|
using Newsbot.Collector.Domain.Models.Config.Sources;
|
2023-03-05 22:33:41 -08:00
|
|
|
using Serilog;
|
2023-03-19 22:54:17 -07:00
|
|
|
using ILogger = Serilog.ILogger;
|
2023-02-14 17:51:22 -08:00
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
// Define Logger
|
2023-03-19 22:54:17 -07:00
|
|
|
builder.Host.UseSerilog();
|
2023-02-14 17:51:22 -08:00
|
|
|
|
2023-03-11 10:43:06 -08:00
|
|
|
// Build the config
|
2023-02-19 21:39:03 -08:00
|
|
|
var config = GetConfiguration();
|
2023-02-14 17:51:22 -08:00
|
|
|
builder.Configuration.AddConfiguration(config);
|
|
|
|
|
2023-03-31 22:49:39 -07:00
|
|
|
Log.Logger = GetLogger(config);
|
2023-03-19 22:54:17 -07:00
|
|
|
Log.Information("Starting up");
|
2023-07-06 22:22:55 -07:00
|
|
|
|
|
|
|
// configure Entity Framework
|
2023-07-14 22:23:45 -07:00
|
|
|
// Allow the controllers to access all the table repositories based on the interface
|
2023-07-23 16:20:16 -07:00
|
|
|
DatabaseStartup.BuildDatabase(builder.Services, config);
|
|
|
|
DatabaseStartup.InjectTableClasses(builder.Services);
|
|
|
|
DatabaseStartup.InjectIdentityService(builder.Services);
|
2023-07-06 22:22:55 -07:00
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
// Configure Hangfire
|
2023-02-14 17:51:22 -08:00
|
|
|
builder.Services.AddHangfire(f => f.UseMemoryStorage());
|
|
|
|
builder.Services.AddHangfireServer();
|
2023-02-26 09:40:04 -08:00
|
|
|
GlobalConfiguration.Configuration.UseSerilogLogProvider();
|
2023-02-14 17:51:22 -08:00
|
|
|
|
2023-07-14 22:23:45 -07:00
|
|
|
// Build Health Checks
|
2023-03-13 22:25:45 -07:00
|
|
|
builder.Services.AddHealthChecks()
|
2023-07-23 22:55:20 -07:00
|
|
|
.AddNpgSql(config.GetValue<string>(ConfigConst.ConnectionStringDatabase) ?? "");
|
2023-03-13 22:25:45 -07:00
|
|
|
|
2023-07-06 22:22:55 -07:00
|
|
|
|
2023-02-14 17:51:22 -08:00
|
|
|
builder.Services.AddControllers();
|
2023-07-06 22:22:55 -07:00
|
|
|
|
2023-07-23 16:20:16 -07:00
|
|
|
SwaggerStartup.ConfigureSwagger(builder.Services);
|
2023-02-14 17:51:22 -08:00
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
builder.Services.Configure<ConnectionStrings>(config.GetSection("ConnectionStrings"));
|
2023-03-31 22:49:39 -07:00
|
|
|
builder.Services.Configure<ConfigSectionConnectionStrings>(config.GetSection(ConfigSectionsConst.ConnectionStrings));
|
|
|
|
builder.Services.Configure<ConfigSectionRssModel>(config.GetSection(ConfigSectionsConst.Rss));
|
|
|
|
builder.Services.Configure<ConfigSectionYoutubeModel>(config.GetSection(ConfigSectionsConst.Youtube));
|
|
|
|
|
2023-07-23 16:20:16 -07:00
|
|
|
IdentityStartup.DefineJwtRequirements(builder.Services, config);
|
2023-06-25 21:05:12 -07:00
|
|
|
|
2023-02-14 17:51:22 -08:00
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
2023-07-14 22:23:45 -07:00
|
|
|
|
|
|
|
// Enable Swagger if requested based on config
|
|
|
|
if (config.GetValue<bool>(ConfigConst.EnableSwagger))
|
2023-03-19 22:54:17 -07:00
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
}
|
2023-02-14 17:51:22 -08:00
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
2023-07-14 22:23:45 -07:00
|
|
|
// Enable Hangfire background jobs
|
2023-02-14 17:51:22 -08:00
|
|
|
app.UseHangfireDashboard();
|
2023-03-31 22:49:39 -07:00
|
|
|
BackgroundJobs.SetupRecurringJobs(config);
|
2023-02-14 17:51:22 -08:00
|
|
|
|
2023-07-14 22:23:45 -07:00
|
|
|
app.UseAuthorization();
|
2023-07-06 22:22:55 -07:00
|
|
|
app.UseAuthentication();
|
2023-02-14 17:51:22 -08:00
|
|
|
|
2023-07-14 22:23:45 -07:00
|
|
|
// Add middleware
|
2023-07-06 22:22:55 -07:00
|
|
|
//app.UseMiddleware<ApiKeyAuthAuthentication>();
|
2023-06-12 15:15:04 -07:00
|
|
|
|
2023-07-14 22:23:45 -07:00
|
|
|
// Add HealthChecks
|
2023-03-14 07:21:34 -07:00
|
|
|
app.MapHealthChecks("/health", new HealthCheckOptions
|
|
|
|
{
|
|
|
|
Predicate = _ => true,
|
|
|
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
|
|
|
});
|
2023-02-14 17:51:22 -08:00
|
|
|
app.MapControllers();
|
|
|
|
|
2023-07-14 22:23:45 -07:00
|
|
|
// Run Database Migrations if requested
|
|
|
|
using var serviceScope = app.Services.CreateScope();
|
|
|
|
if (config.GetValue<bool>(ConfigConst.RunDatabaseMigrationsOnStartup))
|
|
|
|
{
|
2023-07-23 16:20:16 -07:00
|
|
|
await DatabaseStartup.RunDatabaseMigrationsAsync(serviceScope);
|
2023-07-14 22:23:45 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log.Warning("Database Migrations have been skipped. Make sure you run them on your own");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject the roles
|
2023-07-23 16:20:16 -07:00
|
|
|
await DatabaseStartup.InjectIdentityRolesAsync(serviceScope);
|
2023-07-14 22:23:45 -07:00
|
|
|
|
|
|
|
// Start the application
|
2023-02-14 17:51:22 -08:00
|
|
|
app.Run();
|
2023-02-19 21:39:03 -08:00
|
|
|
|
2023-02-26 09:40:04 -08:00
|
|
|
|
2023-02-19 21:39:03 -08:00
|
|
|
static IConfiguration GetConfiguration()
|
|
|
|
{
|
|
|
|
return new ConfigurationBuilder()
|
|
|
|
.AddJsonFile("appsettings.json", true)
|
2023-06-17 11:59:37 -07:00
|
|
|
.AddJsonFile("config/appsettings.json", true)
|
2023-02-19 21:39:03 -08:00
|
|
|
.AddEnvironmentVariables()
|
|
|
|
.Build();
|
|
|
|
}
|
|
|
|
|
2023-03-31 22:49:39 -07:00
|
|
|
static ILogger GetLogger(IConfiguration configuration)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-07-23 22:55:20 -07:00
|
|
|
var otel = configuration.GetValue<string>(ConfigConst.ConnectionStringOpenTelemetry) ?? "";
|
2023-03-31 22:49:39 -07:00
|
|
|
|
2023-04-03 09:32:42 -07:00
|
|
|
if (otel == "")
|
2023-03-31 22:49:39 -07:00
|
|
|
return Log.Logger = new LoggerConfiguration()
|
|
|
|
.WriteTo.Console()
|
2023-04-03 09:31:00 -07:00
|
|
|
.MinimumLevel.Information()
|
2023-03-31 22:49:39 -07:00
|
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
return Log.Logger = new LoggerConfiguration()
|
|
|
|
.WriteTo.Console()
|
2023-04-03 09:31:00 -07:00
|
|
|
.MinimumLevel.Information()
|
2023-03-31 22:49:39 -07:00
|
|
|
.WriteTo.OpenTelemetry(
|
|
|
|
otel,
|
|
|
|
resourceAttributes: new Dictionary<string, object>
|
|
|
|
{
|
2023-04-03 08:57:19 -07:00
|
|
|
{ "Application", "newsbot-collector-api"},
|
2023-03-31 22:49:39 -07:00
|
|
|
{ "service.name", "newsbot-collector-api" }
|
|
|
|
})
|
|
|
|
.CreateLogger();
|
2023-07-14 22:23:45 -07:00
|
|
|
}
|