From ee47e39b40347809fc82a4e51c060a1f17b0895a Mon Sep 17 00:00:00 2001 From: Hazel K Date: Tue, 6 Aug 2024 20:29:55 -0400 Subject: [PATCH] chore: split main / hosting logic into new ModShark.Main project (resolves #67) --- Main/Main.csproj | 33 +++++++++++ Main/Program.cs | 56 +++++++++++++++++++ .../Properties/launchSettings.json | 2 +- {ModShark => Main}/Worker.cs | 10 ++-- .../appsettings.Development.json | 0 {ModShark => Main}/appsettings.json | 2 +- ModShark.sln | 6 ++ ModShark/ModShark.csproj | 10 +--- ModShark/ModSharkConfig.cs | 6 ++ ModShark/ModSharkModule.cs | 26 ++------- ModShark/Program.cs | 34 ----------- ModShark/Properties/project.cs | 1 + 12 files changed, 115 insertions(+), 71 deletions(-) create mode 100644 Main/Main.csproj create mode 100644 Main/Program.cs rename {ModShark => Main}/Properties/launchSettings.json (93%) rename {ModShark => Main}/Worker.cs (90%) rename {ModShark => Main}/appsettings.Development.json (100%) rename {ModShark => Main}/appsettings.json (99%) delete mode 100644 ModShark/Program.cs create mode 100644 ModShark/Properties/project.cs diff --git a/Main/Main.csproj b/Main/Main.csproj new file mode 100644 index 0000000..f1be888 --- /dev/null +++ b/Main/Main.csproj @@ -0,0 +1,33 @@ + + + + Exe + net8.0 + enable + enable + ModShark.Main + ModShark.Main + + + + + + + + + + + + + Always + Never + + + Always + Always + + + Always + + + diff --git a/Main/Program.cs b/Main/Program.cs new file mode 100644 index 0000000..1204b4a --- /dev/null +++ b/Main/Program.cs @@ -0,0 +1,56 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using ModShark; +using ModShark.Main; + +var builder = Host.CreateApplicationBuilder(args); + +// Simple logging for dev, Systemd logging for production +builder.Logging.ClearProviders(); +if (builder.Environment.IsDevelopment()) +{ + builder.Logging.AddSimpleConsole(options => + { + options.IncludeScopes = true; + options.SingleLine = true; + options.TimestampFormat = "yyyy-MM-dd HH:mm:ss "; + }); +} +else +{ + builder.Logging.AddSystemdConsole(options => + { + options.IncludeScopes = true; + options.UseUtcTimestamp = true; + options.TimestampFormat = " [yyyy-MM-dd HH:mm:ss] "; + }); +} + +// Add local environment +if (builder.Environment.IsDevelopment()) + builder.Configuration.AddJsonFile("appsettings.Local.json", optional: true); + +// Read and register config. +// This will be reworked later. +var config = builder.Configuration + .GetSection("ModShark") + .Get() + ?? throw new ApplicationException("Configuration file is invalid: could not map to the config object."); +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.AddModShark(builder.Configuration); +builder.Services.AddHostedService(); + +var host = builder.Build(); +host.Run(); \ No newline at end of file diff --git a/ModShark/Properties/launchSettings.json b/Main/Properties/launchSettings.json similarity index 93% rename from ModShark/Properties/launchSettings.json rename to Main/Properties/launchSettings.json index 08e8008..d0f60f6 100644 --- a/ModShark/Properties/launchSettings.json +++ b/Main/Properties/launchSettings.json @@ -1,7 +1,7 @@ { "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { - "ModShark": { + "Main": { "commandName": "Project", "dotnetRunMessages": true, "environmentVariables": { diff --git a/ModShark/Worker.cs b/Main/Worker.cs similarity index 90% rename from ModShark/Worker.cs rename to Main/Worker.cs index da010aa..02c3ba5 100644 --- a/ModShark/Worker.cs +++ b/Main/Worker.cs @@ -1,7 +1,10 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using ModShark.Reports; using ModShark.Services; -namespace ModShark; +namespace ModShark.Main; public class Worker(ILogger logger, WorkerConfig config, IServiceScopeFactory scopeFactory) : BackgroundService { @@ -37,9 +40,4 @@ await scope.ServiceProvider .GetRequiredService() .MakeReports(report, stoppingToken); } -} - -public class WorkerConfig -{ - public int PollInterval { get; set; } } \ No newline at end of file diff --git a/ModShark/appsettings.Development.json b/Main/appsettings.Development.json similarity index 100% rename from ModShark/appsettings.Development.json rename to Main/appsettings.Development.json diff --git a/ModShark/appsettings.json b/Main/appsettings.json similarity index 99% rename from ModShark/appsettings.json rename to Main/appsettings.json index 5c58617..db2ec9d 100644 --- a/ModShark/appsettings.json +++ b/Main/appsettings.json @@ -30,7 +30,7 @@ "FromAddress": "", "FromName": "ModShark", "ToAddresses": [] - }, + }, "Console": { "Enabled": true diff --git a/ModShark.sln b/ModShark.sln index aa5b498..59a4d09 100644 --- a/ModShark.sln +++ b/ModShark.sln @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Resources", "Resources", "{ Resources\sample-misskey-ids.mjs = Resources\sample-misskey-ids.mjs EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Main", "Main\Main.csproj", "{BEA17D18-66E1-4075-AEAA-1626BF2FF8B7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -31,5 +33,9 @@ Global {D0EE90C8-1DA3-42F9-A461-A469F9C31D1E}.Debug|Any CPU.Build.0 = Debug|Any CPU {D0EE90C8-1DA3-42F9-A461-A469F9C31D1E}.Release|Any CPU.ActiveCfg = Release|Any CPU {D0EE90C8-1DA3-42F9-A461-A469F9C31D1E}.Release|Any CPU.Build.0 = Release|Any CPU + {BEA17D18-66E1-4075-AEAA-1626BF2FF8B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BEA17D18-66E1-4075-AEAA-1626BF2FF8B7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BEA17D18-66E1-4075-AEAA-1626BF2FF8B7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BEA17D18-66E1-4075-AEAA-1626BF2FF8B7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/ModShark/ModShark.csproj b/ModShark/ModShark.csproj index 2843201..3044442 100644 --- a/ModShark/ModShark.csproj +++ b/ModShark/ModShark.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -13,17 +13,9 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - Never - Never - - - diff --git a/ModShark/ModSharkConfig.cs b/ModShark/ModSharkConfig.cs index c306b75..ec30a8c 100644 --- a/ModShark/ModSharkConfig.cs +++ b/ModShark/ModSharkConfig.cs @@ -17,6 +17,12 @@ public class ModSharkConfig public required RulesConfig Rules { get; set; } } +[PublicAPI] +public class WorkerConfig +{ + public int PollInterval { get; set; } +} + [PublicAPI] public class SharkeyConfig { diff --git a/ModShark/ModSharkModule.cs b/ModShark/ModSharkModule.cs index 1ca57bc..b01d3e6 100644 --- a/ModShark/ModSharkModule.cs +++ b/ModShark/ModSharkModule.cs @@ -1,4 +1,6 @@ -using ModShark.Reports; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using ModShark.Reports; using ModShark.Reports.Reporter; using ModShark.Rules; using ModShark.Services; @@ -8,32 +10,17 @@ namespace ModShark; public static class ModSharkModule { + /// + /// Registers ModShark into the provided service collection. + /// public static T AddModShark(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() - ?? 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(); services.AddScoped(); @@ -55,7 +42,6 @@ public static T AddModShark(this T services, IConfiguration configuration) services.AddSingleton(); services.AddScoped(); services.AddScoped(); - services.AddHostedService(); return services; } diff --git a/ModShark/Program.cs b/ModShark/Program.cs deleted file mode 100644 index 214ec30..0000000 --- a/ModShark/Program.cs +++ /dev/null @@ -1,34 +0,0 @@ -using ModShark; - -var builder = Host.CreateApplicationBuilder(args); - -// Simple logging for dev, Systemd logging for production -builder.Logging.ClearProviders(); -if (builder.Environment.IsDevelopment()) -{ - builder.Logging.AddSimpleConsole(options => - { - options.IncludeScopes = true; - options.SingleLine = true; - options.TimestampFormat = "yyyy-MM-dd HH:mm:ss "; - }); -} -else -{ - builder.Logging.AddSystemdConsole(options => - { - options.IncludeScopes = true; - options.UseUtcTimestamp = true; - options.TimestampFormat = " [yyyy-MM-dd HH:mm:ss] "; - }); -} - - -// Add local environment -if (builder.Environment.IsDevelopment()) - builder.Configuration.AddJsonFile("appsettings.Local.json", optional: true); - -builder.Services.AddModShark(builder.Configuration); - -var host = builder.Build(); -host.Run(); \ No newline at end of file diff --git a/ModShark/Properties/project.cs b/ModShark/Properties/project.cs new file mode 100644 index 0000000..6139a57 --- /dev/null +++ b/ModShark/Properties/project.cs @@ -0,0 +1 @@ +global using Microsoft.Extensions.Logging; \ No newline at end of file