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-03-31 22:49:39 -07:00
|
|
|
using Newsbot.Collector.Api;
|
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-03-05 22:33:41 -08:00
|
|
|
using Serilog;
|
2023-03-31 22:49:39 -07:00
|
|
|
|
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-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-03-13 22:25:45 -07:00
|
|
|
builder.Services.AddHealthChecks()
|
|
|
|
.AddNpgSql(config.GetValue<string>(ConfigConnectionStringConst.Database) ?? "");
|
|
|
|
|
2023-02-14 17:51:22 -08:00
|
|
|
builder.Services.AddControllers();
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
|
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));
|
|
|
|
//builder.Services.Configure<
|
|
|
|
|
2023-02-14 17:51:22 -08:00
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
2023-03-19 22:54:17 -07:00
|
|
|
if (config.GetValue<bool>("EnableSwagger"))
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
}
|
2023-02-14 17:51:22 -08:00
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
app.UseHangfireDashboard();
|
2023-03-31 22:49:39 -07:00
|
|
|
BackgroundJobs.SetupRecurringJobs(config);
|
2023-02-14 17:51:22 -08:00
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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)
|
|
|
|
.AddEnvironmentVariables()
|
|
|
|
.Build();
|
|
|
|
}
|
|
|
|
|
2023-03-31 22:49:39 -07:00
|
|
|
static ILogger GetLogger(IConfiguration configuration)
|
2023-02-19 21:39:03 -08:00
|
|
|
{
|
2023-03-31 22:49:39 -07:00
|
|
|
var otel = configuration.GetValue<string>(ConfigConnectionStringConst.OpenTelemetry) ?? "";
|
|
|
|
|
|
|
|
if (otel == "")
|
|
|
|
return Log.Logger = new LoggerConfiguration()
|
|
|
|
.WriteTo.Console()
|
|
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
return Log.Logger = new LoggerConfiguration()
|
|
|
|
.WriteTo.Console()
|
|
|
|
.WriteTo.OpenTelemetry(
|
|
|
|
otel,
|
|
|
|
resourceAttributes: new Dictionary<string, object>
|
|
|
|
{
|
|
|
|
{ "service.name", "newsbot-collector-api" }
|
|
|
|
})
|
|
|
|
.CreateLogger();
|
2023-03-19 22:54:17 -07:00
|
|
|
}
|