Migrate DI container to Autofac
This commit is contained in:
12
PluralKit.API/Modules.cs
Normal file
12
PluralKit.API/Modules.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Autofac;
|
||||
|
||||
namespace PluralKit.API
|
||||
{
|
||||
public class APIModule: Module
|
||||
{
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
builder.RegisterType<TokenAuthService>().AsSelf();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,8 @@
|
||||
using Microsoft.AspNetCore;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace PluralKit.API
|
||||
{
|
||||
@@ -8,13 +11,16 @@ namespace PluralKit.API
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
InitUtils.Init();
|
||||
CreateWebHostBuilder(args).Build().Run();
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseConfiguration(InitUtils.BuildConfiguration(args).Build())
|
||||
.ConfigureKestrel(opts => { opts.ListenAnyIP(5000);})
|
||||
.UseStartup<Startup>();
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
|
||||
.ConfigureWebHostDefaults(whb => whb
|
||||
|
||||
.UseConfiguration(InitUtils.BuildConfiguration(args).Build())
|
||||
.ConfigureKestrel(opts => { opts.ListenAnyIP(5000); })
|
||||
.UseStartup<Startup>());
|
||||
}
|
||||
}
|
@@ -1,10 +1,14 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Autofac;
|
||||
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using PluralKit.Core;
|
||||
|
||||
namespace PluralKit.API
|
||||
{
|
||||
public class Startup
|
||||
@@ -23,20 +27,16 @@ namespace PluralKit.API
|
||||
services.AddControllers()
|
||||
.SetCompatibilityVersion(CompatibilityVersion.Latest)
|
||||
.AddNewtonsoftJson(); // sorry MS, this just does *more*
|
||||
}
|
||||
|
||||
services
|
||||
.AddTransient<IDataStore, PostgresDataStore>()
|
||||
.AddSingleton<SchemaService>()
|
||||
|
||||
.AddSingleton(svc => InitUtils.InitMetrics(svc.GetRequiredService<CoreConfig>(), "API"))
|
||||
|
||||
.AddScoped<TokenAuthService>()
|
||||
|
||||
.AddTransient(_ => Configuration.GetSection("PluralKit").Get<CoreConfig>() ?? new CoreConfig())
|
||||
.AddSingleton(svc => InitUtils.InitLogger(svc.GetRequiredService<CoreConfig>(), "api"))
|
||||
|
||||
.AddTransient<DbConnectionCountHolder>()
|
||||
.AddTransient<DbConnectionFactory>();
|
||||
public void ConfigureContainer(ContainerBuilder builder)
|
||||
{
|
||||
builder.RegisterInstance(Configuration);
|
||||
builder.RegisterModule(new ConfigModule<object>());
|
||||
builder.RegisterModule(new LoggingModule("api"));
|
||||
builder.RegisterModule(new MetricsModule("API"));
|
||||
builder.RegisterModule<DataStoreModule>();
|
||||
builder.RegisterModule<APIModule>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
Reference in New Issue
Block a user