Skip to content

Commit

Permalink
chore: split main / hosting logic into new ModShark.Main project (res…
Browse files Browse the repository at this point in the history
…olves #67)
  • Loading branch information
warriordog committed Aug 7, 2024
1 parent 2a7a932 commit ee47e39
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 71 deletions.
33 changes: 33 additions & 0 deletions Main/Main.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>ModShark.Main</AssemblyName>
<RootNamespace>ModShark.Main</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ModShark\ModShark.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<Content Include="appsettings.Local.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</Content>
<Content Include="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
56 changes: 56 additions & 0 deletions Main/Program.cs
Original file line number Diff line number Diff line change
@@ -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<ModSharkConfig>()
?? 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<Worker>();

var host = builder.Build();
host.Run();
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"ModShark": {
"Main": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
Expand Down
10 changes: 4 additions & 6 deletions ModShark/Worker.cs → Main/Worker.cs
Original file line number Diff line number Diff line change
@@ -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<Worker> logger, WorkerConfig config, IServiceScopeFactory scopeFactory) : BackgroundService
{
Expand Down Expand Up @@ -37,9 +40,4 @@ await scope.ServiceProvider
.GetRequiredService<IReportService>()
.MakeReports(report, stoppingToken);
}
}

public class WorkerConfig
{
public int PollInterval { get; set; }
}
File renamed without changes.
2 changes: 1 addition & 1 deletion ModShark/appsettings.json → Main/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"FromAddress": "",
"FromName": "ModShark",
"ToAddresses": []
},
},

"Console": {
"Enabled": true
Expand Down
6 changes: 6 additions & 0 deletions ModShark.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
10 changes: 1 addition & 9 deletions ModShark/ModShark.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -13,17 +13,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<Content Update="appsettings.Local.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SharkeyDB\SharkeyDB.csproj" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions ModShark/ModSharkConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
26 changes: 6 additions & 20 deletions ModShark/ModSharkModule.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,32 +10,17 @@ namespace ModShark;

public static class ModSharkModule
{
/// <summary>
/// Registers ModShark into the provided service collection.
/// </summary>
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>();

Expand All @@ -55,7 +42,6 @@ public static T AddModShark<T>(this T services, IConfiguration configuration)
services.AddSingleton<ILinkService, LinkService>();
services.AddScoped<IMetaService, MetaService>();
services.AddScoped<IServiceAccountService, ServiceAccountService>();
services.AddHostedService<Worker>();

return services;
}
Expand Down
34 changes: 0 additions & 34 deletions ModShark/Program.cs

This file was deleted.

1 change: 1 addition & 0 deletions ModShark/Properties/project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.Extensions.Logging;

0 comments on commit ee47e39

Please sign in to comment.