2020-11-27 22:52:52 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Serilog;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
using Serilog.Events;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using SharedLibraryCore.Database.MigrationContext;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class StartupExtensions
|
|
|
|
|
{
|
|
|
|
|
private static ILogger _defaultLogger = null;
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddBaseLogger(this IServiceCollection services,
|
|
|
|
|
ApplicationConfiguration appConfig)
|
|
|
|
|
{
|
|
|
|
|
if (_defaultLogger == null)
|
|
|
|
|
{
|
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile(Path.Join(Utilities.OperatingDirectory, "Configuration", "LoggingConfiguration.json"))
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var loggerConfig = new LoggerConfiguration()
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.ReadFrom.Configuration(configuration)
|
|
|
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Utilities.IsDevelopment)
|
|
|
|
|
{
|
|
|
|
|
loggerConfig = loggerConfig.WriteTo.Console(
|
2020-11-27 22:52:52 -05:00
|
|
|
|
outputTemplate:
|
|
|
|
|
"[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Server} {Level:u3}] {Message:lj}{NewLine}{Exception}")
|
|
|
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
2020-11-11 18:31:26 -05:00
|
|
|
|
.MinimumLevel.Debug();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_defaultLogger = loggerConfig.CreateLogger();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
services.AddLogging(builder => builder.AddSerilog(_defaultLogger, dispose: true));
|
|
|
|
|
return services;
|
|
|
|
|
}
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
public static IServiceCollection AddDatabaseContextOptions(this IServiceCollection services,
|
2020-11-27 22:52:52 -05:00
|
|
|
|
ApplicationConfiguration appConfig)
|
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var activeProvider = appConfig.DatabaseProvider?.ToLower();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(appConfig.ConnectionString) || activeProvider == "sqlite")
|
2020-11-27 22:52:52 -05:00
|
|
|
|
{
|
|
|
|
|
var currentPath = Utilities.OperatingDirectory;
|
|
|
|
|
currentPath = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
|
|
|
|
|
? $"{Path.DirectorySeparatorChar}{currentPath}"
|
|
|
|
|
: currentPath;
|
|
|
|
|
|
|
|
|
|
var connectionStringBuilder = new SqliteConnectionStringBuilder
|
|
|
|
|
{DataSource = Path.Join(currentPath, "Database", "Database.db")};
|
|
|
|
|
var connectionString = connectionStringBuilder.ToString();
|
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var builder = new DbContextOptionsBuilder<SqliteDatabaseContext>()
|
|
|
|
|
.UseSqlite(connectionString);
|
|
|
|
|
|
|
|
|
|
services.AddSingleton((DbContextOptions) builder.Options);
|
2020-11-27 22:52:52 -05:00
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
switch (activeProvider)
|
2020-11-27 22:52:52 -05:00
|
|
|
|
{
|
|
|
|
|
case "mysql":
|
|
|
|
|
var appendTimeout = !appConfig.ConnectionString.Contains("default command timeout",
|
|
|
|
|
StringComparison.InvariantCultureIgnoreCase);
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var mysqlBuilder = new DbContextOptionsBuilder<MySqlDatabaseContext>()
|
|
|
|
|
.UseMySql(appConfig.ConnectionString + (appendTimeout ? ";default command timeout=0" : ""),
|
|
|
|
|
mysqlOptions => mysqlOptions.EnableRetryOnFailure());
|
|
|
|
|
services.AddSingleton((DbContextOptions) mysqlBuilder.Options);
|
|
|
|
|
return services;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
case "postgresql":
|
|
|
|
|
appendTimeout = !appConfig.ConnectionString.Contains("Command Timeout",
|
|
|
|
|
StringComparison.InvariantCultureIgnoreCase);
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var postgresqlBuilder = new DbContextOptionsBuilder<PostgresqlDatabaseContext>()
|
|
|
|
|
.UseNpgsql(appConfig.ConnectionString + (appendTimeout ? ";Command Timeout=0" : ""),
|
|
|
|
|
postgresqlOptions => postgresqlOptions.EnableRetryOnFailure());
|
|
|
|
|
services.AddSingleton((DbContextOptions) postgresqlBuilder.Options);
|
|
|
|
|
return services;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentException($"No context available for {appConfig.DatabaseProvider}");
|
2020-11-27 22:52:52 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
|
|
|
|
}
|