< Summary

Information
Class: GistBackend.StartUp
Assembly: GistBackend
File(s): /home/runner/work/the-gist-of-it-sec/the-gist-of-it-sec/backend/GistBackend/StartUp.cs
Line coverage
88%
Covered lines: 79
Uncovered lines: 10
Coverable lines: 89
Total lines: 138
Line coverage: 88.7%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ConfigureServices(...)100%4487.01%
Configure(...)100%11100%

File(s)

/home/runner/work/the-gist-of-it-sec/the-gist-of-it-sec/backend/GistBackend/StartUp.cs

#LineLine coverage
 1using GistBackend.Handlers;
 2using GistBackend.Handlers.AIHandler;
 3using GistBackend.Handlers.ChromaDbHandler;
 4using GistBackend.Handlers.MariaDbHandler;
 5using GistBackend.Handlers.RssFeedHandler;
 6using GistBackend.Handlers.TelegramBotClientHandler;
 7using GistBackend.Handlers.WebCrawlHandler;
 8using GistBackend.Services;
 9using GistBackend.Utils;
 10using Microsoft.AspNetCore.Builder;
 11using Microsoft.AspNetCore.Hosting;
 12using Microsoft.Extensions.Configuration;
 13using Microsoft.Extensions.DependencyInjection;
 14using Microsoft.Extensions.Logging;
 15using Microsoft.Extensions.Options;
 16using Polly;
 17using Prometheus;
 18using Scalar.AspNetCore;
 19
 20namespace GistBackend;
 21
 1022public class StartUp(IConfiguration configuration)
 23{
 24    public const string RetryingHttpClientName = "WithRetry";
 25    public const string GistsControllerMariaDbHandlerOptionsName = $"GistsController{nameof(MariaDbHandlerOptions)}";
 26
 27    public void ConfigureServices(IServiceCollection services)
 28    {
 29        // CORS Configuration
 1030        var corsOrigin = Environment.GetEnvironmentVariable("CORS_ALLOWED_ORIGIN") ?? "https://dummy-origin.com";
 31
 1032        services.AddCors(options =>
 1033        {
 1034            options.AddPolicy("RestrictiveCorsPolicy", builder =>
 1035            {
 1036                builder.WithOrigins(corsOrigin)
 1037                       .WithMethods("GET")
 1038                       .WithHeaders("Content-Type", "Authorization", "Accept")
 1039                       .DisallowCredentials();
 2040            });
 2041        });
 42
 43        const string gistMariaDbHandlerOptionsName = $"Gist{nameof(MariaDbHandlerOptions)}";
 44        const string recapMariaDbHandlerOptionsName = $"Recap{nameof(MariaDbHandlerOptions)}";
 45        const string cleanupMariaDbHandlerOptionsName = $"Cleanup{nameof(MariaDbHandlerOptions)}";
 46        const string telegramMariaDbHandlerOptionsName = $"Telegram{nameof(MariaDbHandlerOptions)}";
 47        const string dummyUserAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0";
 48
 1049        services.Configure<EmbeddingClientHandlerOptions>(
 1050            configuration.GetSection(nameof(EmbeddingClientHandlerOptions)));
 1051        services.Configure<AIHandlerOptions>(
 1052            configuration.GetSection(nameof(AIHandlerOptions)));
 1053        services.Configure<WebCrawlHandlerOptions>(
 1054            configuration.GetSection(nameof(WebCrawlHandlerOptions)));
 1055        services.Configure<ChromaDbHandlerOptions>(
 1056            configuration.GetSection(nameof(ChromaDbHandlerOptions)));
 1057        services.Configure<TelegramBotClientHandlerOptions>(
 1058            configuration.GetSection(nameof(TelegramBotClientHandlerOptions)));
 1059        services.Configure<CleanupServiceOptions>(
 1060            configuration.GetSection(nameof(CleanupServiceOptions)));
 1061        services.Configure<TelegramServiceOptions>(
 1062            configuration.GetSection(nameof(TelegramServiceOptions)));
 1063        services.Configure<MariaDbHandlerOptions>(gistMariaDbHandlerOptionsName,
 1064            configuration.GetSection(gistMariaDbHandlerOptionsName));
 1065        services.Configure<MariaDbHandlerOptions>(recapMariaDbHandlerOptionsName,
 1066            configuration.GetSection(recapMariaDbHandlerOptionsName));
 1067        services.Configure<MariaDbHandlerOptions>(cleanupMariaDbHandlerOptionsName,
 1068            configuration.GetSection(cleanupMariaDbHandlerOptionsName));
 1069        services.Configure<MariaDbHandlerOptions>(telegramMariaDbHandlerOptionsName,
 1070            configuration.GetSection(telegramMariaDbHandlerOptionsName));
 1071        services.Configure<MariaDbHandlerOptions>(GistsControllerMariaDbHandlerOptionsName,
 1072            configuration.GetSection(GistsControllerMariaDbHandlerOptionsName));
 1073        services.Configure<ChromaDbHandlerOptions>(configuration.GetSection(nameof(ChromaDbHandlerOptions)));
 74
 1075        services.AddHttpClient(RetryingHttpClientName)
 076            .ConfigureHttpClient(client => client.DefaultRequestHeaders.UserAgent.ParseAdd(dummyUserAgent))
 1077            .AddStandardResilienceHandler(options => {
 1078                options.Retry.BackoffType = DelayBackoffType.Exponential;
 2079            });
 80
 1081        services.AddMemoryCache();
 82
 1083        services.AddTransient<IRssFeedHandler, RssFeedHandler>();
 1084        services.AddSingleton<IWebCrawlHandler, WebCrawlHandler>();
 1085        services.AddTransient<IMariaDbHandler, MariaDbHandler>();
 1086        services.AddTransient<IEmbeddingClientHandler, EmbeddingClientHandler>();
 1087        services.AddTransient<IAIHandler, AIHandler>();
 1088        services.AddTransient<IChromaDbHandler, ChromaDbHandler>();
 1089        services.AddTransient<IGistDebouncer, GistDebouncer>();
 1090        services.AddTransient<ITelegramBotClientHandler, TelegramBotClientHandler>();
 1091        services.AddTransient<IDateTimeHandler, DateTimeHandler>();
 92
 1093        services.AddControllers();
 1094        services.AddOpenApi();
 95
 1096        services.AddHostedService(provider =>
 1097            ActivatorUtilities.CreateInstance<GistService>(provider,
 1098                provider.GetKeyedMariaDbHandler(gistMariaDbHandlerOptionsName)));
 99
 10100        services.AddHostedService(provider =>
 10101            ActivatorUtilities.CreateInstance<RecapService>(provider,
 10102                provider.GetKeyedMariaDbHandler(recapMariaDbHandlerOptionsName)));
 103
 10104        services.AddHostedService(provider =>
 10105            ActivatorUtilities.CreateInstance<CleanupService>(provider,
 10106                provider.GetKeyedMariaDbHandler(cleanupMariaDbHandlerOptionsName)));
 107
 10108        services.AddHostedService(provider =>
 10109            ActivatorUtilities.CreateInstance<TelegramService>(provider,
 10110                provider.GetKeyedMariaDbHandler(telegramMariaDbHandlerOptionsName)));
 111
 10112        services.AddKeyedScoped<IMariaDbHandler>(GistsControllerMariaDbHandlerOptionsName, (provider, _) => {
 0113            var options = provider.GetRequiredService<IOptionsSnapshot<MariaDbHandlerOptions>>()
 0114                .Get(GistsControllerMariaDbHandlerOptionsName);
 0115            var dateTimeHandler = provider.GetRequiredService<IDateTimeHandler>();
 0116            var logger = provider.GetService<ILogger<MariaDbHandler>>();
 0117            return new MariaDbHandler(
 0118                Options.Create(options),
 0119                dateTimeHandler,
 0120                logger
 0121            );
 10122        });
 10123    }
 124
 125    public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
 126    {
 10127        app.UseMetricServer();
 10128        app.UseCors("RestrictiveCorsPolicy");
 10129        app.UseRouting();
 10130        app.UseEndpoints(endpoints =>
 10131        {
 10132            endpoints.MapControllers();
 10133            endpoints.MapMetrics();
 10134            endpoints.MapOpenApi();
 10135            endpoints.MapScalarApiReference();
 20136        });
 10137    }
 138}