SharpRSS/SharpRSS.Business/DependencyInjection.cs

82 lines
3.1 KiB
C#
Raw Normal View History

2024-07-13 16:29:27 +02:00
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;
2024-07-13 16:29:27 +02:00
using AuthService = SharpRSS.Business.Services.AuthService;
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));
2024-07-06 00:02:02 +02:00
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":
2024-07-06 00:02:02 +02:00
_logger.Information("Configuring SQLite context...");
options.UseSqlite(dbSettings.Connection);
break;
case "MARIADB":
2024-07-06 00:02:02 +02:00
_logger.Information("Configuring MariaDB context...");
var srvVersion = ServerVersion.AutoDetect(dbSettings.Connection);
2024-07-06 00:02:02 +02:00
_logger.Information("Server found, version: {SrvVersion}", srvVersion.Version.Build);
options.UseMySql(dbSettings.Connection, srvVersion);
break;
case "MSSQL":
2024-07-06 00:02:02 +02:00
_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>();
2024-07-06 00:02:02 +02:00
/*
* Authentication
*/
2024-07-13 16:29:27 +02:00
builder.Services.UseBasedAuth(options =>
{
options.AllowRegistration = false;
options.AuthenticationStateMaxAgeBeforeExpire = TimeSpan.FromDays(7);
options.LoginPath = "/auth/login";
options.LogoutPath = "/auth/logout";
});
builder.Services.AddCascadingAuthenticationState();
2024-07-06 00:02:02 +02:00
//TODO: Auth, Settings
return builder;
}
}