about summary refs log tree commit diff
path: root/MatrixContentFilter/Program.cs
blob: 7eec93026db0e2dd489e135c8d2c077fdfa4bf1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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();