about summary refs log tree commit diff
path: root/MatrixContentFilter/Program.cs
blob: b849131bbc15f85cc5860ff71a5df6890e18fac5 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using ArcaneLibs.Extensions;
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.Diagnostics.Metrics;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

var builder = Host.CreateDefaultBuilder(args);

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);
        Console.WriteLine("Configuration: {0}", config.ToJson());
        services.AddSingleton<MatrixContentFilterConfiguration>(config);

        services.AddMetrics(m => m.AddDebugConsole());
        services.AddSingleton<MatrixContentFilterMetrics>();

        if (config.OpenTelemetry.Enabled) {
            services.AddOpenTelemetry()
                .ConfigureResource(resource => resource.AddService("MatrixContentFilter"))
                .WithTracing(tracing => tracing
                    .AddAspNetCoreInstrumentation()
                    .AddConsoleExporter())
                .WithMetrics(metrics => metrics
                    .AddAspNetCoreInstrumentation()
                    .AddHttpClientInstrumentation()
                    .AddRuntimeInstrumentation()
                    .AddConsoleExporter());
        }

        services.AddRoryLibMatrixServices(new() {
            AppName = "MatrixContentFilter"
        });

        services.AddMatrixBot().AddCommandHandler().DiscoverAllCommands()
            .WithInviteHandler<InviteHandler>()
            .WithCommandResultHandler(CommandResultHandler.HandleAsync);

        services.AddSingleton<InfoCacheService>();

        services.AddSingleton<IContentFilter, ImageFilter>();
        services.AddSingleton<IContentFilter, VideoFilter>();
        services.AddSingleton<IContentFilter, PendingInviteLimiter>();

        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>();
                if (config.SanityCheck.Enabled)
                    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}");
        }
    })
    // .ConfigureLogging((ctx, lb) => {
    //     lb.AddOpenTelemetry(options => {
    //         options
    //             .SetResourceBuilder(
    //                 ResourceBuilder.CreateDefault()
    //                     .AddService("MatrixContentFilter"))
    //             .AddConsoleExporter();
    //     });
    // })
    .UseConsoleLifetime().Build();

await host.RunAsync();