Rewrote hosts again
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
|
||||
namespace Telegrator.Hosting.Web
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for Telegram bot hosts with Webhook update receiving.
|
||||
/// Combines wbe application capabilities with reactive Telegram bot functionality.
|
||||
/// </summary>
|
||||
public interface ITelegramBotWebHost : ITelegramBotHost, IEndpointRouteBuilder, IApplicationBuilder, IAsyncDisposable
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ namespace Telegrator.Hosting.Web
|
||||
/// <summary>
|
||||
/// Represents a web hosted telegram bot
|
||||
/// </summary>
|
||||
public class TelegramBotWebHost : ITelegramBotWebHost
|
||||
public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuilder, IAsyncDisposable
|
||||
{
|
||||
private readonly WebApplication _innerApp;
|
||||
private readonly IUpdateRouter _updateRouter;
|
||||
@@ -51,11 +51,6 @@ namespace Telegrator.Hosting.Web
|
||||
/// <param name="webApplicationBuilder">The proxied instance of host builder.</param>
|
||||
public TelegramBotWebHost(WebApplicationBuilder webApplicationBuilder)
|
||||
{
|
||||
// Registering this host in services for easy access
|
||||
webApplicationBuilder.Services.AddSingleton<ITelegramBotHost>(this);
|
||||
webApplicationBuilder.Services.AddSingleton<ITelegramBotWebHost>(this);
|
||||
webApplicationBuilder.Services.AddSingleton<ITelegratorBot>(this);
|
||||
|
||||
// Building proxy application
|
||||
_innerApp = webApplicationBuilder.Build();
|
||||
_innerApp.UseTelegratorWeb();
|
||||
@@ -69,11 +64,12 @@ namespace Telegrator.Hosting.Web
|
||||
/// Creates new <see cref="TelegramBotHostBuilder"/> with default services and webhook update receiving scheme
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static TelegramBotWebHostBuilder CreateBuilder(TelegramBotWebOptions settings)
|
||||
public static TelegramBotWebHostBuilder CreateBuilder(WebApplicationOptions settings)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings.ToWebApplicationOptions());
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings);
|
||||
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
|
||||
|
||||
builder.Services.AddTelegramBotHostDefaults();
|
||||
builder.Services.AddTelegramWebhook();
|
||||
return builder;
|
||||
@@ -83,11 +79,12 @@ namespace Telegrator.Hosting.Web
|
||||
/// Creates new SLIM <see cref="TelegramBotHostBuilder"/> with default services and webhook update receiving scheme
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static TelegramBotWebHostBuilder CreateSlimBuilder(TelegramBotWebOptions settings)
|
||||
public static TelegramBotWebHostBuilder CreateSlimBuilder(WebApplicationOptions settings)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings.ToWebApplicationOptions());
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings);
|
||||
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
|
||||
|
||||
builder.Services.AddTelegramBotHostDefaults();
|
||||
builder.Services.AddTelegramWebhook();
|
||||
return builder;
|
||||
@@ -97,10 +94,10 @@ namespace Telegrator.Hosting.Web
|
||||
/// Creates new EMPTY <see cref="TelegramBotHostBuilder"/> WITHOUT any services or update receiving schemes
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static TelegramBotWebHostBuilder CreateEmptyBuilder(TelegramBotWebOptions settings)
|
||||
public static TelegramBotWebHostBuilder CreateEmptyBuilder(WebApplicationOptions settings)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings.ToWebApplicationOptions());
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings);
|
||||
return new TelegramBotWebHostBuilder(innerApp, settings);
|
||||
}
|
||||
|
||||
@@ -154,10 +151,8 @@ namespace Telegrator.Hosting.Web
|
||||
if (_disposed)
|
||||
return;
|
||||
|
||||
// Sorry for this, i really dont know how to handle such cases
|
||||
ValueTask disposeTask = _innerApp.DisposeAsync();
|
||||
while (!disposeTask.IsCompleted)
|
||||
Thread.Sleep(100);
|
||||
disposeTask.AsTask().Wait();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
_disposed = true;
|
||||
|
||||
@@ -18,8 +18,8 @@ namespace Telegrator.Hosting.Web
|
||||
public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
|
||||
{
|
||||
private readonly WebApplicationBuilder _innerBuilder;
|
||||
private readonly TelegramBotWebOptions _settings;
|
||||
private readonly IHandlersCollection _handlers;
|
||||
private readonly WebApplicationOptions _settings;
|
||||
internal IHandlersCollection _handlers = null!;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IHandlersCollection Handlers => _handlers;
|
||||
@@ -41,11 +41,10 @@ namespace Telegrator.Hosting.Web
|
||||
/// </summary>
|
||||
/// <param name="webApplicationBuilder"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegramBotWebOptions settings)
|
||||
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, WebApplicationOptions settings)
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
_handlers = new HostHandlersCollection(Services, _settings);
|
||||
|
||||
_innerBuilder.AddTelegratorWeb(settings);
|
||||
}
|
||||
@@ -56,13 +55,12 @@ namespace Telegrator.Hosting.Web
|
||||
/// <param name="webApplicationBuilder"></param>
|
||||
/// <param name="handlers"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegramBotWebOptions settings, IHandlersCollection handlers)
|
||||
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers, WebApplicationOptions settings)
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
_handlers = handlers ?? throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
_innerBuilder.AddTelegratorWeb(settings, handlers);
|
||||
_innerBuilder.AddTelegratorWeb(settings, null, handlers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -71,7 +69,9 @@ namespace Telegrator.Hosting.Web
|
||||
/// <returns></returns>
|
||||
public TelegramBotWebHost Build()
|
||||
{
|
||||
return new TelegramBotWebHost(_innerBuilder);
|
||||
TelegramBotWebHost host = new TelegramBotWebHost(_innerBuilder);
|
||||
host.UseTelegrator();
|
||||
return host;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Telegrator.Hosting.Web
|
||||
{
|
||||
/// <summary>
|
||||
/// Options for configuring the behavior for TelegramBotWebHost.
|
||||
/// </summary>
|
||||
public class TelegramBotWebOptions : TelegratorOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Disables automatic configuration for all of required <see cref="IOptions{TOptions}"/> instances
|
||||
/// </summary>
|
||||
public bool DisableAutoConfigure { get; set; }
|
||||
|
||||
/// <inheritdoc cref="WebApplicationOptions.Args"/>
|
||||
public string[]? Args { get; init; }
|
||||
|
||||
/// <inheritdoc cref="WebApplicationOptions.EnvironmentName"/>
|
||||
public string? EnvironmentName { get; init; }
|
||||
|
||||
/// <inheritdoc cref="WebApplicationOptions.ApplicationName"/>
|
||||
public string? ApplicationName { get; init; }
|
||||
|
||||
/// <inheritdoc cref="WebApplicationOptions.ContentRootPath"/>
|
||||
public string? ContentRootPath { get; init; }
|
||||
|
||||
/// <inheritdoc cref="WebApplicationOptions.WebRootPath"/>
|
||||
public string? WebRootPath { get; init; }
|
||||
|
||||
internal WebApplicationOptions ToWebApplicationOptions() => new WebApplicationOptions()
|
||||
{
|
||||
ApplicationName = ApplicationName,
|
||||
Args = Args,
|
||||
ContentRootPath = ContentRootPath,
|
||||
EnvironmentName = EnvironmentName,
|
||||
WebRootPath = WebRootPath
|
||||
};
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -6,20 +6,20 @@ namespace Telegrator.Hosting.Web
|
||||
/// Configuration options for Telegram bot behavior and execution settings.
|
||||
/// Controls various aspects of bot operation including concurrency, routing, webhook receiving, and execution policies.
|
||||
/// </summary>
|
||||
public class TelegratorWebOptions
|
||||
public class WebhookerOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets HTTPS URL to send updates to. Use an empty string to remove webhook integration
|
||||
/// </summary>
|
||||
[StringSyntax(StringSyntaxAttribute.Uri)]
|
||||
public required string WebhookUri { get; set; }
|
||||
public string WebhookUri { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// A secret token to be sent in a header “X-Telegram-Bot-Api-Secret-Token” in every webhook request, 1-256 characters.
|
||||
/// Only characters A-Z, a-z, 0-9, _ and - are allowed.
|
||||
/// The header is useful to ensure that the request comes from a webhook set by you.
|
||||
/// </summary>
|
||||
public string? SecretToken { get; set; }
|
||||
public string? SecretToken { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// The maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40.
|
||||
@@ -30,6 +30,6 @@ namespace Telegrator.Hosting.Web
|
||||
/// <summary>
|
||||
/// Pass true to drop all pending updates
|
||||
/// </summary>
|
||||
public bool DropPendingUpdates { get; set; }
|
||||
public bool DropPendingUpdates { get; set; } = false;
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ namespace Telegrator.Mediation
|
||||
private readonly IEndpointRouteBuilder _botHost;
|
||||
private readonly ITelegramBotClient _botClient;
|
||||
private readonly IUpdateRouter _updateRouter;
|
||||
private readonly TelegratorWebOptions _options;
|
||||
private readonly WebhookerOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initiallizes new instance of <see cref="HostedUpdateWebhooker"/>
|
||||
@@ -32,7 +32,7 @@ namespace Telegrator.Mediation
|
||||
/// <param name="updateRouter"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public HostedUpdateWebhooker(IEndpointRouteBuilder botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<TelegratorWebOptions> options)
|
||||
public HostedUpdateWebhooker(IEndpointRouteBuilder botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<WebhookerOptions> options)
|
||||
{
|
||||
if (string.IsNullOrEmpty(options.Value.WebhookUri))
|
||||
throw new ArgumentNullException(nameof(options), "Option \"WebhookUrl\" must be set to subscribe for update recieving");
|
||||
|
||||
@@ -5,10 +5,9 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Diagnostics;
|
||||
using Telegram.Bot;
|
||||
using Telegrator;
|
||||
using Telegrator.Core;
|
||||
using Telegrator.Hosting;
|
||||
using Telegrator.Hosting.Web;
|
||||
using Telegrator.Mediation;
|
||||
using Telegrator.Providers;
|
||||
@@ -17,7 +16,7 @@ namespace Telegrator
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extensions for <see cref="IServiceCollection"/>
|
||||
/// Provides method to configure <see cref="ITelegramBotWebHost"/>
|
||||
/// Provides method to configure Telegram Bot WebHost
|
||||
/// </summary>
|
||||
public static class ServicesCollectionExtensions
|
||||
{
|
||||
@@ -31,69 +30,78 @@ namespace Telegrator
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IHandlersCollection"/> from the builder properties.
|
||||
/// </summary>
|
||||
public IHandlersCollection Handlers => (IHandlersCollection)builder.Properties[HandlersCollectionPropertyKey];
|
||||
public IHandlersCollection Handlers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (builder is TelegramBotHostBuilder botHostBuilder)
|
||||
return botHostBuilder.Handlers;
|
||||
|
||||
if (builder is TelegramBotWebHostBuilder webBotHostBuilder)
|
||||
return webBotHostBuilder.Handlers;
|
||||
|
||||
return (IHandlersCollection)builder.Properties[HandlersCollectionPropertyKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
|
||||
/// </summary>
|
||||
public static WebApplicationBuilder AddTelegratorWeb(this WebApplicationBuilder builder, TelegramBotWebOptions settings, IHandlersCollection? handlers = null)
|
||||
public static IHostApplicationBuilder AddTelegratorWeb(this IHostApplicationBuilder builder, WebApplicationOptions settings, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
|
||||
{
|
||||
if (settings is null)
|
||||
throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
IServiceCollection services = builder.Services;
|
||||
ConfigurationManager configuration = builder.Configuration;
|
||||
IConfigurationManager configuration = builder.Configuration;
|
||||
|
||||
handlers ??= new HostHandlersCollection(services, settings);
|
||||
builder.Host.Properties.Add(HandlersCollectionPropertyKey, handlers);
|
||||
|
||||
if (handlers is IHostHandlersCollection hostHandlers)
|
||||
if (options == null)
|
||||
{
|
||||
foreach (PreBuildingRoutine preBuildRoutine in hostHandlers.PreBuilderRoutines)
|
||||
options = configuration.GetSection(nameof(TelegratorOptions)).Get<TelegratorOptions>();
|
||||
if (options == null)
|
||||
throw new MissingMemberException("Auto configuration disabled, yet no options of type 'TelegratorOptions' wasn't registered. This configuration is runtime required!");
|
||||
}
|
||||
|
||||
services.AddSingleton(Options.Create(options));
|
||||
|
||||
if (handlers != null)
|
||||
{
|
||||
if (handlers is IHandlersManager manager)
|
||||
{
|
||||
try
|
||||
{
|
||||
// TODO: fix
|
||||
//preBuildRoutine.Invoke(builder);
|
||||
Debug.WriteLine("Pre-Building routine was not executed");
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_ = 0xBAD + 0xC0DE;
|
||||
}
|
||||
ServiceDescriptor descriptor = new ServiceDescriptor(typeof(IHandlersProvider), manager);
|
||||
services.Replace(descriptor);
|
||||
services.AddSingleton(manager);
|
||||
}
|
||||
}
|
||||
|
||||
if (!settings.DisableAutoConfigure)
|
||||
{
|
||||
services.Configure<TelegratorWebOptions>(configuration.GetSection(nameof(TelegratorWebOptions)));
|
||||
services.Configure<TelegratorWebOptions>(configuration.GetSection(nameof(TelegramBotClientOptions)));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!services.Any(srvc => srvc.ImplementationType == typeof(IOptions<TelegratorWebOptions>)))
|
||||
throw new MissingMemberException("Auto configuration disabled, yet no options of type 'TelegratorWebOptions' wasn't registered. This configuration is runtime required!");
|
||||
|
||||
if (!services.Any(srvc => srvc.ImplementationType == typeof(IOptions<TelegramBotClientOptions>)))
|
||||
throw new MissingMemberException("Auto configuration disabled, yet no options of type 'TelegramBotClientOptions' wasn't registered. This configuration is runtime required!");
|
||||
}
|
||||
|
||||
IOptions<TelegramBotWebOptions> options = Options.Create(settings);
|
||||
services.AddSingleton((IOptions<TelegratorOptions>)options);
|
||||
services.AddSingleton(options);
|
||||
handlers ??= new HostHandlersCollection(services, options);
|
||||
services.AddSingleton(handlers);
|
||||
|
||||
if (handlers is IHandlersManager manager)
|
||||
builder.Properties.Add(HandlersCollectionPropertyKey, handlers);
|
||||
if (builder is TelegramBotWebHostBuilder botHostBuilder)
|
||||
botHostBuilder._handlers = handlers;
|
||||
|
||||
if (!services.Any(srvc => srvc.ImplementationType == typeof(IOptions<WebhookerOptions>)))
|
||||
{
|
||||
ServiceDescriptor descriptor = new ServiceDescriptor(typeof(IHandlersProvider), manager);
|
||||
services.Replace(descriptor);
|
||||
services.AddSingleton(manager);
|
||||
WebhookerOptions? webhookerOptions = configuration.GetSection(nameof(WebhookerOptions)).Get<WebhookerOptions>();
|
||||
if (webhookerOptions == null)
|
||||
throw new MissingMemberException("Auto configuration disabled, yet no options of type 'WebhookerOptions' wasn't registered. This configuration is runtime required!");
|
||||
|
||||
services.AddSingleton(Options.Create(webhookerOptions));
|
||||
}
|
||||
|
||||
if (!services.Any(srvc => srvc.ImplementationType == typeof(IOptions<TelegramBotClientOptions>)))
|
||||
{
|
||||
services.AddSingleton(Options.Create(new TelegramBotClientOptions(options.Token, options.BaseUrl, options.UseTestEnvironment)
|
||||
{
|
||||
RetryCount = options.RetryCount,
|
||||
RetryThreshold = options.RetryThreshold
|
||||
}));
|
||||
}
|
||||
|
||||
services.AddTelegramBotHostDefaults();
|
||||
services.AddTelegramWebhook();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user