diff --git a/PluralKit.Core/Modules/ConfigModule.cs b/PluralKit.Core/Modules/ConfigModule.cs new file mode 100644 index 00000000..1ba7fa77 --- /dev/null +++ b/PluralKit.Core/Modules/ConfigModule.cs @@ -0,0 +1,28 @@ +using Autofac; + +using Microsoft.Extensions.Configuration; + +namespace PluralKit.Core +{ + public class ConfigModule: Module where T: new() + { + private readonly 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().GetSection("PluralKit").Get() ?? new CoreConfig()).SingleInstance(); + + // Register the submodule config (BotConfig, etc) if specified + if (_submodule != null) + builder.Register(c => c.Resolve().GetSection("PluralKit").GetSection(_submodule).Get() ?? new T()).SingleInstance(); + } + } +} \ No newline at end of file diff --git a/PluralKit.Core/Modules/DataStoreModule.cs b/PluralKit.Core/Modules/DataStoreModule.cs new file mode 100644 index 00000000..6060cbed --- /dev/null +++ b/PluralKit.Core/Modules/DataStoreModule.cs @@ -0,0 +1,19 @@ +using Autofac; +using Autofac.Extensions.DependencyInjection; + +using Microsoft.Extensions.DependencyInjection; + +namespace PluralKit.Core +{ + public class DataStoreModule: Module + { + protected override void Load(ContainerBuilder builder) + { + builder.RegisterType().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + + builder.Populate(new ServiceCollection().AddMemoryCache()); + } + } +} \ No newline at end of file diff --git a/PluralKit.Core/Modules.cs b/PluralKit.Core/Modules/LoggingModule.cs similarity index 70% rename from PluralKit.Core/Modules.cs rename to PluralKit.Core/Modules/LoggingModule.cs index 08dffd8c..fd52293f 100644 --- a/PluralKit.Core/Modules.cs +++ b/PluralKit.Core/Modules/LoggingModule.cs @@ -1,13 +1,7 @@ -using System; +using System; using System.Globalization; -using App.Metrics; - using Autofac; -using Autofac.Extensions.DependencyInjection; - -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; using NodaTime; @@ -19,66 +13,6 @@ using Serilog.Sinks.SystemConsole.Themes; namespace PluralKit.Core { - public class DataStoreModule: Module - { - protected override void Load(ContainerBuilder builder) - { - builder.RegisterType().SingleInstance(); - builder.RegisterType().As().SingleInstance(); - builder.RegisterType().AsSelf().SingleInstance(); - - builder.Populate(new ServiceCollection().AddMemoryCache()); - } - } - - public class ConfigModule: Module where T: new() - { - private readonly 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().GetSection("PluralKit").Get() ?? new CoreConfig()).SingleInstance(); - - // Register the submodule config (BotConfig, etc) if specified - if (_submodule != null) - builder.Register(c => c.Resolve().GetSection("PluralKit").GetSection(_submodule).Get() ?? 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())) - .AsSelf().As().SingleInstance(); - } - - 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; @@ -176,7 +110,7 @@ namespace PluralKit.Core return Log.Logger = logCfg.CreateLogger(); } } - + // Serilog why is this necessary for such a simple thing >.> public class UTCTimestampFormatProvider: IFormatProvider { diff --git a/PluralKit.Core/Modules/MetricsModule.cs b/PluralKit.Core/Modules/MetricsModule.cs new file mode 100644 index 00000000..cb650b3f --- /dev/null +++ b/PluralKit.Core/Modules/MetricsModule.cs @@ -0,0 +1,32 @@ +using App.Metrics; + +using Autofac; + +namespace PluralKit.Core +{ + 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())) + .AsSelf().As().SingleInstance(); + } + + 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(); + } + } +} \ No newline at end of file