SharpRSS/SharpRSS.Business/DependencyInjection.cs

70 lines
2.6 KiB
C#

using DotBased.ASP.Auth;
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;
using AuthService = SharpRSS.Business.Services.AuthService;
namespace SharpRSS.Business;
public static class DependencyInjection
{
public static WebApplicationBuilder AddSRSS(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'");
}
});
/*
* Services
*/
builder.Services.AddScoped<AuthService>();
//TODO: Auth, Settings
return builder;
}
}