telegram-client/src/Nocr.TelegramClient.AppServices/ServiceCollectionExtensions.cs

63 lines
3.0 KiB
C#

using Insight.TelegramBot.Handling.Handlers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
using Nocr.TelegramClient.AppServices.Handlers;
using Nocr.TelegramClient.AppServices.Options;
using Nocr.TelegramClient.AppServices.TextSubscriptions;
using Nocr.TelegramClient.AppServices.TextSubscriptions.Handlers;
using Nocr.TelegramClient.AppServices.Users;
using Nocr.TextMatcher.Api.Contracts.TextMatches;
using Nocr.Users.Api.Contracts.Users;
using Rebus.Config;
using RestEase;
namespace Nocr.TelegramClient.AppServices;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddAppServices(this IServiceCollection services, IConfiguration configuration)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
// Add registrations here
services.AddRebusHandler<TextSubscriptionMatchedHandler>();
services.AddHttpClient();
services.Configure<AdministrationOptions>(configuration.GetSection(nameof(AdministrationOptions)));
services.AddScoped<IUpdateHandler, SetLocalizerCultureHandler>();
services.AddScoped<IUpdateHandler, UpdateTelegramLoggingHandler>();
services.Configure<UsersRestEaseOptions>(configuration.GetSection(nameof(UsersRestEaseOptions)));
services.AddScoped<IUsersController>(ctx =>
{
var options = ctx.GetRequiredService<IOptions<UsersRestEaseOptions>>().Value;
var httpClientFactory = ctx.GetRequiredService<IHttpClientFactory>();
var client = httpClientFactory.CreateClient(nameof(IUsersController));
client.BaseAddress = new Uri(options.BasePath);
return RestClient.For<IUsersController>(client);
});
services.Configure<TextMatcherRestEaseOptions>(configuration.GetSection(nameof(TextMatcherRestEaseOptions)));
services.AddScoped<ITextSubscriptionsController>(ctx =>
{
var options = ctx.GetRequiredService<IOptions<TextMatcherRestEaseOptions>>().Value;
var httpClientFactory = ctx.GetRequiredService<IHttpClientFactory>();
var client = httpClientFactory.CreateClient(nameof(ITextSubscriptionsController));
client.BaseAddress = new Uri(options.BasePath);
return RestClient.For<ITextSubscriptionsController>(client);
});
services.AddScoped<IUsersService, UsersService>();
services.AddScoped<ITextMatcherService, TextMatcherService>();
services.Configure<MessageDispatcherOptions>(configuration.GetSection(nameof(MessageDispatcherOptions)));
services.AddSingleton<IMessageDispatcherQueue, MessageDispatcherQueue>();
services.AddScoped<IMessageDispatcherHandler, MessageDispatcherHandler>();
services.AddHostedService<MessageDispatcherBackgroundService>();
return services;
}
}