| | | 1 | | using GistBackend.Handlers; |
| | | 2 | | using GistBackend.Handlers.AIHandler; |
| | | 3 | | using GistBackend.Handlers.ChromaDbHandler; |
| | | 4 | | using GistBackend.Handlers.MariaDbHandler; |
| | | 5 | | using GistBackend.Handlers.RssFeedHandler; |
| | | 6 | | using GistBackend.Handlers.TelegramBotClientHandler; |
| | | 7 | | using GistBackend.Handlers.WebCrawlHandler; |
| | | 8 | | using GistBackend.Services; |
| | | 9 | | using GistBackend.Utils; |
| | | 10 | | using Microsoft.AspNetCore.Builder; |
| | | 11 | | using Microsoft.AspNetCore.Hosting; |
| | | 12 | | using Microsoft.Extensions.Configuration; |
| | | 13 | | using Microsoft.Extensions.DependencyInjection; |
| | | 14 | | using Microsoft.Extensions.Logging; |
| | | 15 | | using Microsoft.Extensions.Options; |
| | | 16 | | using Polly; |
| | | 17 | | using Prometheus; |
| | | 18 | | |
| | | 19 | | namespace GistBackend; |
| | | 20 | | |
| | 10 | 21 | | public 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 |
| | 10 | 29 | | var corsOrigin = Environment.GetEnvironmentVariable("CORS_ALLOWED_ORIGIN") ?? "https://dummy-origin.com"; |
| | | 30 | | |
| | 10 | 31 | | services.AddCors(options => |
| | 10 | 32 | | { |
| | 10 | 33 | | options.AddPolicy("RestrictiveCorsPolicy", builder => |
| | 10 | 34 | | { |
| | 10 | 35 | | builder.WithOrigins(corsOrigin) |
| | 10 | 36 | | .WithMethods("GET") |
| | 10 | 37 | | .WithHeaders("Content-Type", "Authorization", "Accept") |
| | 10 | 38 | | .DisallowCredentials(); |
| | 20 | 39 | | }); |
| | 20 | 40 | | }); |
| | | 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 | | |
| | 10 | 48 | | services.Configure<EmbeddingClientHandlerOptions>( |
| | 10 | 49 | | configuration.GetSection(nameof(EmbeddingClientHandlerOptions))); |
| | 10 | 50 | | services.Configure<AIHandlerOptions>( |
| | 10 | 51 | | configuration.GetSection(nameof(AIHandlerOptions))); |
| | 10 | 52 | | services.Configure<WebCrawlHandlerOptions>( |
| | 10 | 53 | | configuration.GetSection(nameof(WebCrawlHandlerOptions))); |
| | 10 | 54 | | services.Configure<ChromaDbHandlerOptions>( |
| | 10 | 55 | | configuration.GetSection(nameof(ChromaDbHandlerOptions))); |
| | 10 | 56 | | services.Configure<TelegramBotClientHandlerOptions>( |
| | 10 | 57 | | configuration.GetSection(nameof(TelegramBotClientHandlerOptions))); |
| | 10 | 58 | | services.Configure<CleanupServiceOptions>( |
| | 10 | 59 | | configuration.GetSection(nameof(CleanupServiceOptions))); |
| | 10 | 60 | | services.Configure<TelegramServiceOptions>( |
| | 10 | 61 | | configuration.GetSection(nameof(TelegramServiceOptions))); |
| | 10 | 62 | | services.Configure<MariaDbHandlerOptions>(gistMariaDbHandlerOptionsName, |
| | 10 | 63 | | configuration.GetSection(gistMariaDbHandlerOptionsName)); |
| | 10 | 64 | | services.Configure<MariaDbHandlerOptions>(recapMariaDbHandlerOptionsName, |
| | 10 | 65 | | configuration.GetSection(recapMariaDbHandlerOptionsName)); |
| | 10 | 66 | | services.Configure<MariaDbHandlerOptions>(cleanupMariaDbHandlerOptionsName, |
| | 10 | 67 | | configuration.GetSection(cleanupMariaDbHandlerOptionsName)); |
| | 10 | 68 | | services.Configure<MariaDbHandlerOptions>(telegramMariaDbHandlerOptionsName, |
| | 10 | 69 | | configuration.GetSection(telegramMariaDbHandlerOptionsName)); |
| | 10 | 70 | | services.Configure<MariaDbHandlerOptions>(GistsControllerMariaDbHandlerOptionsName, |
| | 10 | 71 | | configuration.GetSection(GistsControllerMariaDbHandlerOptionsName)); |
| | 10 | 72 | | services.Configure<ChromaDbHandlerOptions>(configuration.GetSection(nameof(ChromaDbHandlerOptions))); |
| | | 73 | | |
| | 10 | 74 | | services.AddHttpClient(RetryingHttpClientName) |
| | 0 | 75 | | .ConfigureHttpClient(client => client.DefaultRequestHeaders.UserAgent.ParseAdd(dummyUserAgent)) |
| | 10 | 76 | | .AddStandardResilienceHandler(options => { |
| | 10 | 77 | | options.Retry.BackoffType = DelayBackoffType.Exponential; |
| | 20 | 78 | | }); |
| | | 79 | | |
| | 10 | 80 | | services.AddTransient<IRssFeedHandler, RssFeedHandler>(); |
| | 10 | 81 | | services.AddSingleton<IWebCrawlHandler, WebCrawlHandler>(); |
| | 10 | 82 | | services.AddTransient<IMariaDbHandler, MariaDbHandler>(); |
| | 10 | 83 | | services.AddTransient<IEmbeddingClientHandler, EmbeddingClientHandler>(); |
| | 10 | 84 | | services.AddTransient<IAIHandler, AIHandler>(); |
| | 10 | 85 | | services.AddTransient<IChromaDbHandler, ChromaDbHandler>(); |
| | 10 | 86 | | services.AddTransient<IGistDebouncer, GistDebouncer>(); |
| | 10 | 87 | | services.AddTransient<ITelegramBotClientHandler, TelegramBotClientHandler>(); |
| | 10 | 88 | | services.AddTransient<IDateTimeHandler, DateTimeHandler>(); |
| | | 89 | | |
| | 10 | 90 | | services.AddControllers(); |
| | | 91 | | |
| | 10 | 92 | | services.AddHostedService(provider => |
| | 10 | 93 | | ActivatorUtilities.CreateInstance<GistService>(provider, |
| | 10 | 94 | | provider.GetKeyedMariaDbHandler(gistMariaDbHandlerOptionsName))); |
| | | 95 | | |
| | 10 | 96 | | services.AddHostedService(provider => |
| | 10 | 97 | | ActivatorUtilities.CreateInstance<RecapService>(provider, |
| | 10 | 98 | | provider.GetKeyedMariaDbHandler(recapMariaDbHandlerOptionsName))); |
| | | 99 | | |
| | 10 | 100 | | services.AddHostedService(provider => |
| | 10 | 101 | | ActivatorUtilities.CreateInstance<CleanupService>(provider, |
| | 10 | 102 | | provider.GetKeyedMariaDbHandler(cleanupMariaDbHandlerOptionsName))); |
| | | 103 | | |
| | 10 | 104 | | services.AddHostedService(provider => |
| | 10 | 105 | | ActivatorUtilities.CreateInstance<TelegramService>(provider, |
| | 10 | 106 | | provider.GetKeyedMariaDbHandler(telegramMariaDbHandlerOptionsName))); |
| | | 107 | | |
| | 10 | 108 | | services.AddKeyedScoped<IMariaDbHandler>(GistsControllerMariaDbHandlerOptionsName, (provider, _) => { |
| | 0 | 109 | | var options = provider.GetRequiredService<IOptionsSnapshot<MariaDbHandlerOptions>>() |
| | 0 | 110 | | .Get(GistsControllerMariaDbHandlerOptionsName); |
| | 0 | 111 | | var dateTimeHandler = provider.GetRequiredService<IDateTimeHandler>(); |
| | 0 | 112 | | var logger = provider.GetService<ILogger<MariaDbHandler>>(); |
| | 0 | 113 | | return new MariaDbHandler( |
| | 0 | 114 | | Options.Create(options), |
| | 0 | 115 | | dateTimeHandler, |
| | 0 | 116 | | logger |
| | 0 | 117 | | ); |
| | 10 | 118 | | }); |
| | 10 | 119 | | } |
| | | 120 | | |
| | | 121 | | public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
| | | 122 | | { |
| | 10 | 123 | | app.UseMetricServer(); |
| | 10 | 124 | | app.UseCors("RestrictiveCorsPolicy"); |
| | 10 | 125 | | app.UseRouting(); |
| | 10 | 126 | | app.UseEndpoints(endpoints => |
| | 10 | 127 | | { |
| | 10 | 128 | | endpoints.MapControllers(); |
| | 10 | 129 | | endpoints.MapMetrics(); |
| | 20 | 130 | | }); |
| | 10 | 131 | | } |
| | | 132 | | } |