SharpRSS/SharpRSS.Business/DependencyInjection.cs

59 lines
2.4 KiB
C#

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;
namespace SharpRSS.Business;
public static class DependencyInjection
{
public static WebApplicationBuilder UseSRSS(this WebApplicationBuilder builder)
{
// Logging (serilog)
var serilogConfig = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).UseBasedExtension();
Log.Logger = serilogConfig.CreateLogger();
LogService.AddLogAdapter(new BasedSerilogAdapter(Log.Logger));
var logger = LogService.RegisterLogger(typeof(DependencyInjection));
builder.Logging.ClearProviders();
builder.Logging.AddSerilog();
// EF Core DbContextFactory
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":
logger.Information("Configuring SQLite context...");
options.UseSqlite(dbSettings.Connection);
break;
case "MARIADB":
logger.Information("Configuring MariaDB context...");
var srvVersion = ServerVersion.AutoDetect(dbSettings.Connection);
logger.Information("Server found, version: {SrvVersion}", srvVersion.Version.Build);
options.UseMySql(dbSettings.Connection, srvVersion);
break;
case "MSSQL":
logger.Information("Configuring MSSQL context...");
options.UseSqlServer(dbSettings.Connection);
break;
default:
throw new ArgumentException("No database server type specified in settings!, supported types: 'SQLite' 'MariaDB' 'MSSQL'");
}
});
//TODO: Services, Auth, Settings
return builder;
}
}