< 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: 75
Uncovered lines: 10
Coverable lines: 85
Total lines: 132
Line coverage: 88.2%
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%4486.66%
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;
 18
 19namespace GistBackend;
 20
 1021public class StartUp(IConfiguration configuration)
 22{
 23    public const string RetryingHttpClientName = "WithRetry";
 24    public const string GistsControllerMariaDbHandlerOptionsName = $"GistsController{nameof(MariaDbHandlerOptions)}";
 25
 26    public void ConfigureServices(IServiceCollection services)
 27    {
 28        // CORS Configuration
 1029        var corsOrigin = Environment.GetEnvironmentVariable("CORS_ALLOWED_ORIGIN") ?? "https://dummy-origin.com";
 30
 1031        services.AddCors(options =>
 1032        {
 1033            options.AddPolicy("RestrictiveCorsPolicy", builder =>
 1034            {
 1035                builder.WithOrigins(corsOrigin)
 1036                       .WithMethods("GET")
 1037                       .WithHeaders("Content-Type", "Authorization", "Accept")
 1038                       .DisallowCredentials();
 2039            });
 2040        });
 41
 42        const string gistMariaDbHandlerOptionsName = $"Gist{nameof(MariaDbHandlerOptions)}";
 43        const string recapMariaDbHandlerOptionsName = $"Recap{nameof(MariaDbHandlerOptions)}";
 44        const string cleanupMariaDbHandlerOptionsName = $"Cleanup{nameof(MariaDbHandlerOptions)}";
 45        const string telegramMariaDbHandlerOptionsName = $"Telegram{nameof(MariaDbHandlerOptions)}";
 46        const string dummyUserAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0";
 47
 1048        services.Configure<EmbeddingClientHandlerOptions>(
 1049            configuration.GetSection(nameof(EmbeddingClientHandlerOptions)));
 1050        services.Configure<AIHandlerOptions>(
 1051            configuration.GetSection(nameof(AIHandlerOptions)));
 1052        services.Configure<WebCrawlHandlerOptions>(
 1053            configuration.GetSection(nameof(WebCrawlHandlerOptions)));
 1054        services.Configure<ChromaDbHandlerOptions>(
 1055            configuration.GetSection(nameof(ChromaDbHandlerOptions)));
 1056        services.Configure<TelegramBotClientHandlerOptions>(
 1057            configuration.GetSection(nameof(TelegramBotClientHandlerOptions)));
 1058        services.Configure<CleanupServiceOptions>(
 1059            configuration.GetSection(nameof(CleanupServiceOptions)));
 1060        services.Configure<TelegramServiceOptions>(
 1061            configuration.GetSection(nameof(TelegramServiceOptions)));
 1062        services.Configure<MariaDbHandlerOptions>(gistMariaDbHandlerOptionsName,
 1063            configuration.GetSection(gistMariaDbHandlerOptionsName));
 1064        services.Configure<MariaDbHandlerOptions>(recapMariaDbHandlerOptionsName,
 1065            configuration.GetSection(recapMariaDbHandlerOptionsName));
 1066        services.Configure<MariaDbHandlerOptions>(cleanupMariaDbHandlerOptionsName,
 1067            configuration.GetSection(cleanupMariaDbHandlerOptionsName));
 1068        services.Configure<MariaDbHandlerOptions>(telegramMariaDbHandlerOptionsName,
 1069            configuration.GetSection(telegramMariaDbHandlerOptionsName));
 1070        services.Configure<MariaDbHandlerOptions>(GistsControllerMariaDbHandlerOptionsName,
 1071            configuration.GetSection(GistsControllerMariaDbHandlerOptionsName));
 1072        services.Configure<ChromaDbHandlerOptions>(configuration.GetSection(nameof(ChromaDbHandlerOptions)));
 73
 1074        services.AddHttpClient(RetryingHttpClientName)
 075            .ConfigureHttpClient(client => client.DefaultRequestHeaders.UserAgent.ParseAdd(dummyUserAgent))
 1076            .AddStandardResilienceHandler(options => {
 1077                options.Retry.BackoffType = DelayBackoffType.Exponential;
 2078            });
 79
 1080        services.AddTransient<IRssFeedHandler, RssFeedHandler>();
 1081        services.AddSingleton<IWebCrawlHandler, WebCrawlHandler>();
 1082        services.AddTransient<IMariaDbHandler, MariaDbHandler>();
 1083        services.AddTransient<IEmbeddingClientHandler, EmbeddingClientHandler>();
 1084        services.AddTransient<IAIHandler, AIHandler>();
 1085        services.AddTransient<IChromaDbHandler, ChromaDbHandler>();
 1086        services.AddTransient<IGistDebouncer, GistDebouncer>();
 1087        services.AddTransient<ITelegramBotClientHandler, TelegramBotClientHandler>();
 1088        services.AddTransient<IDateTimeHandler, DateTimeHandler>();
 89
 1090        services.AddControllers();
 91
 1092        services.AddHostedService(provider =>
 1093            ActivatorUtilities.CreateInstance<GistService>(provider,
 1094                provider.GetKeyedMariaDbHandler(gistMariaDbHandlerOptionsName)));
 95
 1096        services.AddHostedService(provider =>
 1097            ActivatorUtilities.CreateInstance<RecapService>(provider,
 1098                provider.GetKeyedMariaDbHandler(recapMariaDbHandlerOptionsName)));
 99
 10100        services.AddHostedService(provider =>
 10101            ActivatorUtilities.CreateInstance<CleanupService>(provider,
 10102                provider.GetKeyedMariaDbHandler(cleanupMariaDbHandlerOptionsName)));
 103
 10104        services.AddHostedService(provider =>
 10105            ActivatorUtilities.CreateInstance<TelegramService>(provider,
 10106                provider.GetKeyedMariaDbHandler(telegramMariaDbHandlerOptionsName)));
 107
 10108        services.AddKeyedScoped<IMariaDbHandler>(GistsControllerMariaDbHandlerOptionsName, (provider, _) => {
 0109            var options = provider.GetRequiredService<IOptionsSnapshot<MariaDbHandlerOptions>>()
 0110                .Get(GistsControllerMariaDbHandlerOptionsName);
 0111            var dateTimeHandler = provider.GetRequiredService<IDateTimeHandler>();
 0112            var logger = provider.GetService<ILogger<MariaDbHandler>>();
 0113            return new MariaDbHandler(
 0114                Options.Create(options),
 0115                dateTimeHandler,
 0116                logger
 0117            );
 10118        });
 10119    }
 120
 121    public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
 122    {
 10123        app.UseMetricServer();
 10124        app.UseCors("RestrictiveCorsPolicy");
 10125        app.UseRouting();
 10126        app.UseEndpoints(endpoints =>
 10127        {
 10128            endpoints.MapControllers();
 10129            endpoints.MapMetrics();
 20130        });
 10131    }
 132}