2024-07-13 16:29:27 +02:00
|
|
|
using DotBased.ASP.Auth;
|
2024-06-16 21:09:49 +02:00
|
|
|
using DotBased.Logging;
|
|
|
|
using DotBased.Logging.Serilog;
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
|
|
using Serilog;
|
|
|
|
using SharpRSS.Core.Configuration;
|
|
|
|
using SharpRSS.Data;
|
2024-07-13 16:29:27 +02:00
|
|
|
using AuthService = SharpRSS.Business.Services.AuthService;
|
2024-06-16 21:09:49 +02:00
|
|
|
|
2024-06-16 17:20:30 +02:00
|
|
|
namespace SharpRSS.Business;
|
|
|
|
|
|
|
|
public static class DependencyInjection
|
|
|
|
{
|
2024-07-27 16:08:06 +02:00
|
|
|
public static WebApplicationBuilder AddSRSS(this WebApplicationBuilder builder)
|
2024-06-16 21:09:49 +02:00
|
|
|
{
|
2024-07-03 20:17:44 +02:00
|
|
|
/*
|
|
|
|
* Logging (serilog)
|
|
|
|
*/
|
2024-06-16 21:09:49 +02:00
|
|
|
var serilogConfig = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).UseBasedExtension();
|
|
|
|
Log.Logger = serilogConfig.CreateLogger();
|
|
|
|
|
|
|
|
LogService.AddLogAdapter(new BasedSerilogAdapter(Log.Logger));
|
2024-07-06 00:02:02 +02:00
|
|
|
var _logger = LogService.RegisterLogger(typeof(DependencyInjection));
|
2024-06-16 21:09:49 +02:00
|
|
|
|
|
|
|
builder.Logging.ClearProviders();
|
|
|
|
builder.Logging.AddSerilog();
|
|
|
|
|
2024-07-03 20:17:44 +02:00
|
|
|
/*
|
|
|
|
* EF Core DbContextFactory
|
|
|
|
*/
|
2024-06-16 21:09:49 +02:00
|
|
|
builder.Services.AddDbContextFactory<SRSSContext>(options =>
|
|
|
|
{
|
|
|
|
var dbSettings = new DatabaseSettings();
|
|
|
|
builder.Configuration.GetSection(DatabaseSettings.Configuration).Bind(dbSettings);
|
|
|
|
options.ConfigureWarnings(c => c.Log((RelationalEventId.CommandExecuted, LogLevel.Debug)));
|
|
|
|
switch (dbSettings.Server.ToUpper())
|
|
|
|
{
|
|
|
|
case "SQLITE":
|
2024-07-06 00:02:02 +02:00
|
|
|
_logger.Information("Configuring SQLite context...");
|
2024-06-16 21:09:49 +02:00
|
|
|
options.UseSqlite(dbSettings.Connection);
|
|
|
|
break;
|
|
|
|
case "MARIADB":
|
2024-07-06 00:02:02 +02:00
|
|
|
_logger.Information("Configuring MariaDB context...");
|
2024-06-16 21:09:49 +02:00
|
|
|
var srvVersion = ServerVersion.AutoDetect(dbSettings.Connection);
|
2024-07-06 00:02:02 +02:00
|
|
|
_logger.Information("Server found, version: {SrvVersion}", srvVersion.Version.Build);
|
2024-06-16 21:09:49 +02:00
|
|
|
options.UseMySql(dbSettings.Connection, srvVersion);
|
|
|
|
break;
|
|
|
|
case "MSSQL":
|
2024-07-06 00:02:02 +02:00
|
|
|
_logger.Information("Configuring MSSQL context...");
|
2024-06-16 21:09:49 +02:00
|
|
|
options.UseSqlServer(dbSettings.Connection);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new ArgumentException("No database server type specified in settings!, supported types: 'SQLite' 'MariaDB' 'MSSQL'");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-07-03 20:17:44 +02:00
|
|
|
/*
|
|
|
|
* Services
|
|
|
|
*/
|
|
|
|
builder.Services.AddScoped<AuthService>();
|
|
|
|
|
|
|
|
//TODO: Auth, Settings
|
2024-06-16 21:09:49 +02:00
|
|
|
return builder;
|
|
|
|
}
|
2024-06-16 17:20:30 +02:00
|
|
|
}
|