about summary refs log tree commit diff
path: root/MatrixContentFilter/Program.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-10-04 19:51:44 +0200
committerRory& <root@rory.gay>2024-10-04 19:51:44 +0200
commitc8f7ef7c1d2bd705a5442c0dc591b8e5a50673a5 (patch)
tree9b951c6e2c120ec370ce8318238aadbdda880a89 /MatrixContentFilter/Program.cs
downloadMatrixContentFilter-master.tar.xz
Initial commit HEAD master
Diffstat (limited to 'MatrixContentFilter/Program.cs')
-rw-r--r--MatrixContentFilter/Program.cs70
1 files changed, 70 insertions, 0 deletions
diff --git a/MatrixContentFilter/Program.cs b/MatrixContentFilter/Program.cs
new file mode 100644
index 0000000..7eec930
--- /dev/null
+++ b/MatrixContentFilter/Program.cs
@@ -0,0 +1,70 @@
+using MatrixContentFilter;
+using MatrixContentFilter.Handlers;
+using LibMatrix.Services;
+using LibMatrix.Utilities.Bot;
+using MatrixContentFilter.Abstractions;
+using MatrixContentFilter.Handlers.Filters;
+using MatrixContentFilter.Services;
+using MatrixContentFilter.Services.AsyncActionQueues;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+
+var builder = Host.CreateDefaultBuilder(args);
+
+builder.ConfigureHostOptions(host => {
+    host.ServicesStartConcurrently = true;
+    host.ServicesStopConcurrently = true;
+    host.ShutdownTimeout = TimeSpan.FromSeconds(5);
+});
+
+if (Environment.GetEnvironmentVariable("MATRIXCONTENTFILTER_APPSETTINGS_PATH") is string path)
+    builder.ConfigureAppConfiguration(x => x.AddJsonFile(path));
+
+var host = builder.ConfigureServices((ctx, services) => {
+    var config = new MatrixContentFilterConfiguration(ctx.Configuration);
+    services.AddSingleton<MatrixContentFilterConfiguration>(config);
+
+    services.AddRoryLibMatrixServices(new() {
+        AppName = "MatrixContentFilter"
+    });
+
+    services.AddMatrixBot().AddCommandHandler().DiscoverAllCommands()
+        .WithInviteHandler(InviteHandler.HandleAsync)
+        .WithCommandResultHandler(CommandResultHandler.HandleAsync);
+
+    services.AddSingleton<InfoCacheService>();
+
+    services.AddSingleton<IContentFilter, ImageFilter>();
+
+    services.AddSingleton<ConfigurationService>();
+    services.AddSingleton<IHostedService, ConfigurationService>(s => s.GetRequiredService<ConfigurationService>());
+    services.AddSingleton<AsyncMessageQueue>();
+    services.AddSingleton<IHostedService, AsyncMessageQueue>(s => s.GetRequiredService<AsyncMessageQueue>());
+
+    switch (config.AppMode) {
+        case "bot":
+            services.AddHostedService<MatrixContentFilterBot>();
+            // services.AddHostedService<BotModeSanityCheckService>();
+            break;
+        default:
+            throw new NotSupportedException($"Unknown app mode: {config.AppMode}");
+    }
+    
+    switch (config.AsyncQueueImplementation) {
+        case "lifo":
+            services.AddSingleton<LiFoAsyncActionQueue>();
+            services.AddSingleton<AbstractAsyncActionQueue, LiFoAsyncActionQueue>(s => s.GetRequiredService<LiFoAsyncActionQueue>());
+            services.AddSingleton<IHostedService, LiFoAsyncActionQueue>(s => s.GetRequiredService<LiFoAsyncActionQueue>());
+            break;
+        case "fifo":
+            services.AddSingleton<FiFoAsyncActionQueue>();
+            services.AddSingleton<AbstractAsyncActionQueue, FiFoAsyncActionQueue>(s => s.GetRequiredService<FiFoAsyncActionQueue>());
+            services.AddSingleton<IHostedService, FiFoAsyncActionQueue>(s => s.GetRequiredService<FiFoAsyncActionQueue>());
+            break;
+        default:
+            throw new NotSupportedException($"Unknown async queue implementation: {config.AsyncQueueImplementation}");
+    }
+}).UseConsoleLifetime().Build();
+
+await host.RunAsync();
\ No newline at end of file