Migrate DI container to Autofac
This commit is contained in:
109
PluralKit.Core/Modules.cs
Normal file
109
PluralKit.Core/Modules.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
|
||||
using App.Metrics;
|
||||
|
||||
using Autofac;
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
using NodaTime;
|
||||
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
using Serilog.Formatting.Compact;
|
||||
using Serilog.Sinks.SystemConsole.Themes;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public class DataStoreModule: Module
|
||||
{
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
builder.RegisterType<DbConnectionCountHolder>().SingleInstance();
|
||||
builder.RegisterType<DbConnectionFactory>().AsSelf().SingleInstance();
|
||||
builder.RegisterType<PostgresDataStore>().AsSelf().As<IDataStore>();
|
||||
builder.RegisterType<SchemaService>().AsSelf();
|
||||
}
|
||||
}
|
||||
|
||||
public class ConfigModule<T>: Module where T: new()
|
||||
{
|
||||
private string _submodule;
|
||||
|
||||
public ConfigModule(string submodule = null)
|
||||
{
|
||||
_submodule = submodule;
|
||||
}
|
||||
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
// We're assuming IConfiguration is already available somehow - it comes from various places (auto-injected in ASP, etc)
|
||||
|
||||
// Register the CoreConfig and where to find it
|
||||
builder.Register(c => c.Resolve<IConfiguration>().GetSection("PluralKit").Get<CoreConfig>() ?? new CoreConfig()).SingleInstance();
|
||||
|
||||
// Register the submodule config (BotConfig, etc) if specified
|
||||
if (_submodule != null)
|
||||
builder.Register(c => c.Resolve<IConfiguration>().GetSection("PluralKit").GetSection(_submodule).Get<T>() ?? new T()).SingleInstance();
|
||||
}
|
||||
}
|
||||
|
||||
public class MetricsModule: Module
|
||||
{
|
||||
private readonly string _onlyContext;
|
||||
|
||||
public MetricsModule(string onlyContext = null)
|
||||
{
|
||||
_onlyContext = onlyContext;
|
||||
}
|
||||
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
builder.Register(c => InitMetrics(c.Resolve<CoreConfig>()))
|
||||
.AsSelf().As<IMetrics>();
|
||||
}
|
||||
|
||||
private IMetricsRoot InitMetrics(CoreConfig config)
|
||||
{
|
||||
var builder = AppMetrics.CreateDefaultBuilder();
|
||||
if (config.InfluxUrl != null && config.InfluxDb != null)
|
||||
builder.Report.ToInfluxDb(config.InfluxUrl, config.InfluxDb);
|
||||
if (_onlyContext != null)
|
||||
builder.Filter.ByIncludingOnlyContext(_onlyContext);
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
|
||||
public class LoggingModule: Module
|
||||
{
|
||||
private readonly string _component;
|
||||
|
||||
public LoggingModule(string component)
|
||||
{
|
||||
_component = component;
|
||||
}
|
||||
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
builder.Register(c => InitLogger(c.Resolve<CoreConfig>())).AsSelf().SingleInstance();
|
||||
}
|
||||
|
||||
private ILogger InitLogger(CoreConfig config)
|
||||
{
|
||||
return new LoggerConfiguration()
|
||||
.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb)
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.Async(a =>
|
||||
a.File(
|
||||
new RenderedCompactJsonFormatter(),
|
||||
(config.LogDir ?? "logs") + $"/pluralkit.{_component}.log",
|
||||
rollingInterval: RollingInterval.Day,
|
||||
flushToDiskInterval: TimeSpan.FromSeconds(10),
|
||||
restrictedToMinimumLevel: LogEventLevel.Information,
|
||||
buffered: true))
|
||||
.WriteTo.Async(a =>
|
||||
a.Console(theme: AnsiConsoleTheme.Code, outputTemplate:"[{Timestamp:HH:mm:ss}] {Level:u3} {Message:lj}{NewLine}{Exception}"))
|
||||
.CreateLogger();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="App.Metrics" Version="3.2.0" />
|
||||
<PackageReference Include="App.Metrics.Reporting.InfluxDB" Version="3.2.0" />
|
||||
<PackageReference Include="Autofac" Version="4.9.4" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
|
||||
<PackageReference Include="Dapper" Version="1.60.6" />
|
||||
<PackageReference Include="Dapper.Contrib" Version="1.60.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.0" />
|
||||
|
||||
@@ -324,35 +324,7 @@ namespace PluralKit
|
||||
// Add global type mapper for ProxyTag compound type in Postgres
|
||||
NpgsqlConnection.GlobalTypeMapper.MapComposite<ProxyTag>("proxy_tag");
|
||||
}
|
||||
|
||||
public static ILogger InitLogger(CoreConfig config, string component)
|
||||
{
|
||||
return new LoggerConfiguration()
|
||||
.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb)
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.Async(a =>
|
||||
a.File(
|
||||
new RenderedCompactJsonFormatter(),
|
||||
(config.LogDir ?? "logs") + $"/pluralkit.{component}.log",
|
||||
rollingInterval: RollingInterval.Day,
|
||||
flushToDiskInterval: TimeSpan.FromSeconds(10),
|
||||
restrictedToMinimumLevel: LogEventLevel.Information,
|
||||
buffered: true))
|
||||
.WriteTo.Async(a =>
|
||||
a.Console(theme: AnsiConsoleTheme.Code, outputTemplate:"[{Timestamp:HH:mm:ss}] [{EventId}] {Level:u3} {Message:lj}{NewLine}{Exception}"))
|
||||
.CreateLogger();
|
||||
}
|
||||
|
||||
public static IMetrics InitMetrics(CoreConfig config, string onlyContext = null)
|
||||
{
|
||||
var builder = AppMetrics.CreateDefaultBuilder();
|
||||
if (config.InfluxUrl != null && config.InfluxDb != null)
|
||||
builder.Report.ToInfluxDb(config.InfluxUrl, config.InfluxDb);
|
||||
if (onlyContext != null)
|
||||
builder.Filter.ByIncludingOnlyContext(onlyContext);
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
|
||||
public static JsonSerializerSettings BuildSerializerSettings() => new JsonSerializerSettings().BuildSerializerSettings();
|
||||
|
||||
public static JsonSerializerSettings BuildSerializerSettings(this JsonSerializerSettings settings)
|
||||
@@ -362,18 +334,6 @@ namespace PluralKit
|
||||
}
|
||||
}
|
||||
|
||||
public class LoggerProvider
|
||||
{
|
||||
private CoreConfig _config;
|
||||
public ILogger RootLogger { get; }
|
||||
|
||||
public LoggerProvider(CoreConfig config, string component)
|
||||
{
|
||||
_config = config;
|
||||
RootLogger = InitUtils.InitLogger(_config, component);
|
||||
}
|
||||
}
|
||||
|
||||
public class UlongEncodeAsLongHandler : SqlMapper.TypeHandler<ulong>
|
||||
{
|
||||
public override ulong Parse(object value)
|
||||
@@ -660,16 +620,6 @@ namespace PluralKit
|
||||
}
|
||||
}
|
||||
|
||||
public class EventIdProvider
|
||||
{
|
||||
public Guid EventId { get; }
|
||||
|
||||
public EventIdProvider()
|
||||
{
|
||||
EventId = Guid.NewGuid();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConnectionUtils
|
||||
{
|
||||
public static async IAsyncEnumerable<T> QueryStreamAsync<T>(this DbConnectionFactory connFactory, string sql, object param)
|
||||
|
||||
Reference in New Issue
Block a user