Skip to content

Commit

Permalink
chore: separate DI registration from startup (resolves #66)
Browse files Browse the repository at this point in the history
  • Loading branch information
warriordog committed Jul 29, 2024
1 parent aeed851 commit 2a7a932
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 47 deletions.
62 changes: 62 additions & 0 deletions ModShark/ModSharkModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using ModShark.Reports;
using ModShark.Reports.Reporter;
using ModShark.Rules;
using ModShark.Services;
using SharkeyDB;

namespace ModShark;

public static class ModSharkModule
{
public static T AddModShark<T>(this T services, IConfiguration configuration)
where T : IServiceCollection
{
// Register dependencies first.
// Order doesn't really matter, but it makes more sense conceptually.
services.AddSharkeyDB();

// Read and register config.
// This will be reworked later.
var config = configuration
.GetSection("ModShark")
.Get<ModSharkConfig>()
?? throw new ApplicationException("Configuration file is invalid: could not map to the config object.");
services.AddSingleton(config.Postgres);
services.AddSingleton(config.Sharkey);
services.AddSingleton(config.Worker);
services.AddSingleton(config.Reporters.SendGrid);
services.AddSingleton(config.Reporters.Console);
services.AddSingleton(config.Reporters.Native);
services.AddSingleton(config.Reporters.Post);
services.AddSingleton(config.Rules.FlaggedUser);
services.AddSingleton(config.Rules.FlaggedInstance);
services.AddSingleton(config.Rules.FlaggedNote);

// Register all services.

services.AddHttpClient<IHttpService, HttpService>();
services.AddScoped<ISharkeyHttpService, SharkeyHttpService>();

services.AddSingleton<IRandomService, RandomService>();
services.AddSingleton<ITimeService, TimeService>();
services.AddSingleton<ISharkeyIdService, SharkeyIdService>();

services.AddSingleton<IConsoleReporter, ConsoleReporter>();
services.AddScoped<ISendGridReporter, SendGridReporter>();
services.AddScoped<INativeReporter, NativeReporter>();
services.AddScoped<IPostReporter, PostReporter>();
services.AddScoped<IReportService, ReportService>();

services.AddScoped<IFlaggedUserRule, FlaggedUserRule>();
services.AddScoped<IFlaggedInstanceRule, FlaggedInstanceRule>();
services.AddScoped<IFlaggedNoteRule, FlaggedNoteRule>();
services.AddScoped<IRuleService, RuleService>();

services.AddSingleton<ILinkService, LinkService>();
services.AddScoped<IMetaService, MetaService>();
services.AddScoped<IServiceAccountService, ServiceAccountService>();
services.AddHostedService<Worker>();

return services;
}
}
47 changes: 1 addition & 46 deletions ModShark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using ModShark;
using ModShark.Reports;
using ModShark.Reports.Reporter;
using ModShark.Rules;
using ModShark.Services;
using SharkeyDB;

var builder = Host.CreateApplicationBuilder(args);

Expand Down Expand Up @@ -33,47 +28,7 @@
if (builder.Environment.IsDevelopment())
builder.Configuration.AddJsonFile("appsettings.Local.json", optional: true);

// Read config
var config = builder.Configuration
.GetSection("ModShark")
.Get<ModSharkConfig>()
?? throw new ApplicationException("Configuration file is invalid: could not map to the config object.");

builder.Services.UseSharkeyDB();

builder.Services.AddSingleton(config.Postgres);
builder.Services.AddSingleton(config.Sharkey);
builder.Services.AddSingleton(config.Worker);
builder.Services.AddSingleton(config.Reporters.SendGrid);
builder.Services.AddSingleton(config.Reporters.Console);
builder.Services.AddSingleton(config.Reporters.Native);
builder.Services.AddSingleton(config.Reporters.Post);
builder.Services.AddSingleton(config.Rules.FlaggedUser);
builder.Services.AddSingleton(config.Rules.FlaggedInstance);
builder.Services.AddSingleton(config.Rules.FlaggedNote);

builder.Services.AddHttpClient<IHttpService, HttpService>();
builder.Services.AddScoped<ISharkeyHttpService, SharkeyHttpService>();

builder.Services.AddSingleton<IRandomService, RandomService>();
builder.Services.AddSingleton<ITimeService, TimeService>();
builder.Services.AddSingleton<ISharkeyIdService, SharkeyIdService>();

builder.Services.AddSingleton<IConsoleReporter, ConsoleReporter>();
builder.Services.AddScoped<ISendGridReporter, SendGridReporter>();
builder.Services.AddScoped<INativeReporter, NativeReporter>();
builder.Services.AddScoped<IPostReporter, PostReporter>();
builder.Services.AddScoped<IReportService, ReportService>();

builder.Services.AddScoped<IFlaggedUserRule, FlaggedUserRule>();
builder.Services.AddScoped<IFlaggedInstanceRule, FlaggedInstanceRule>();
builder.Services.AddScoped<IFlaggedNoteRule, FlaggedNoteRule>();
builder.Services.AddScoped<IRuleService, RuleService>();

builder.Services.AddSingleton<ILinkService, LinkService>();
builder.Services.AddScoped<IMetaService, MetaService>();
builder.Services.AddScoped<IServiceAccountService, ServiceAccountService>();
builder.Services.AddHostedService<Worker>();
builder.Services.AddModShark(builder.Configuration);

var host = builder.Build();
host.Run();
2 changes: 1 addition & 1 deletion SharkeyDB/SharkeyDBModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace SharkeyDB;

public static class SharkeyDBModule
{
public static T UseSharkeyDB<T>(this T services)
public static T AddSharkeyDB<T>(this T services)
where T : IServiceCollection
{
services.AddDbContext<SharkeyContext>();
Expand Down

0 comments on commit 2a7a932

Please sign in to comment.