* Changed public API overview generator behaviour, now working only in DEBUG builds
* Fixed wrong LeveldDebug method calls after moving logic from providers to router * Added independent "IndentFlags" property to inner debugger class * Fixed debug logging in few places * Removed "ICollectingOptions" and merged it with new options abstract "ITelegratorOptions" * Added WebHook version of hosting class
This commit is contained in:
@@ -1,10 +1,196 @@
|
||||
using Telegrator.Hosting.Components;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Hosting.Components;
|
||||
using Telegrator.Hosting.Providers;
|
||||
using Telegrator.Hosting.Web.Components;
|
||||
using Telegrator.MadiatorCore;
|
||||
using Telegrator.MadiatorCore.Descriptors;
|
||||
|
||||
namespace Telegrator.Hosting.Web
|
||||
{
|
||||
public class TelegramBotWebHost //: ITelegramBotWebHost
|
||||
/// <summary>
|
||||
/// Represents a web hosted telegram bot
|
||||
/// </summary>
|
||||
public class TelegramBotWebHost : ITelegramBotWebHost
|
||||
{
|
||||
private readonly WebApplication _innerApp;
|
||||
private readonly IUpdateRouter _updateRouter;
|
||||
private readonly ILogger<TelegramBotHost> _logger;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IServiceProvider Services => _innerApp.Services;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IUpdateRouter UpdateRouter => _updateRouter;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ICollection<EndpointDataSource> DataSources => ((IEndpointRouteBuilder)_innerApp).DataSources;
|
||||
|
||||
/// <summary>
|
||||
/// Allows consumers to be notified of application lifetime events.
|
||||
/// </summary>
|
||||
public IHostApplicationLifetime Lifetime => _innerApp.Lifetime;
|
||||
|
||||
/// <summary>
|
||||
/// This application's logger
|
||||
/// </summary>
|
||||
public ILogger<TelegramBotHost> Logger => _logger;
|
||||
|
||||
// Private interface fields
|
||||
IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services;
|
||||
IServiceProvider IApplicationBuilder.ApplicationServices { get => Services; set => throw new NotImplementedException(); }
|
||||
IFeatureCollection IApplicationBuilder.ServerFeatures => ((IApplicationBuilder)_innerApp).ServerFeatures;
|
||||
IDictionary<string, object?> IApplicationBuilder.Properties => ((IApplicationBuilder)_innerApp).Properties;
|
||||
|
||||
internal TelegramBotWebHost(WebApplicationBuilder webApplicationBuilder, HostHandlersCollection handlers)
|
||||
{
|
||||
RegisterHostServices(webApplicationBuilder, handlers);
|
||||
_innerApp = webApplicationBuilder.Build();
|
||||
|
||||
_updateRouter = Services.GetRequiredService<IUpdateRouter>();
|
||||
_logger = Services.GetRequiredService<ILogger<TelegramBotHost>>();
|
||||
|
||||
LogHandlers(handlers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates new <see cref="TelegramBotHostBuilder"/> with default services and webhook update receiving scheme
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static TelegramBotWebHostBuilder CreateBuilder(TelegramBotWebOptions settings)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings.ToWebApplicationOptions());
|
||||
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
|
||||
builder.Services.AddTelegramBotHostDefaults();
|
||||
builder.Services.AddTelegramWebhook();
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates new SLIM <see cref="TelegramBotHostBuilder"/> with default services and webhook update receiving scheme
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static TelegramBotWebHostBuilder CreateSlimBuilder(TelegramBotWebOptions settings)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings.ToWebApplicationOptions());
|
||||
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
|
||||
builder.Services.AddTelegramBotHostDefaults();
|
||||
builder.Services.AddTelegramWebhook();
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates new EMPTY <see cref="TelegramBotHostBuilder"/> WITHOUT any services or update receiving schemes
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static TelegramBotWebHostBuilder CreateEmptyBuilder(TelegramBotWebOptions settings)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings, nameof(settings));
|
||||
WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings.ToWebApplicationOptions());
|
||||
return new TelegramBotWebHostBuilder(innerApp, settings);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task StartAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _innerApp.StartAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task StopAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _innerApp.StopAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IApplicationBuilder CreateApplicationBuilder()
|
||||
=> ((IEndpointRouteBuilder)_innerApp).CreateApplicationBuilder();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware)
|
||||
=> _innerApp.Use(middleware);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IApplicationBuilder New()
|
||||
=> ((IApplicationBuilder)_innerApp).New();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public RequestDelegate Build()
|
||||
=> ((IApplicationBuilder)_innerApp).Build();
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the host.
|
||||
/// </summary>
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
|
||||
await _innerApp.DisposeAsync();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the host.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
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);
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
private void LogHandlers(HostHandlersCollection handlers)
|
||||
{
|
||||
StringBuilder logBuilder = new StringBuilder("Registered handlers : ");
|
||||
if (!handlers.Keys.Any())
|
||||
throw new Exception();
|
||||
|
||||
foreach (UpdateType updateType in handlers.Keys)
|
||||
{
|
||||
HandlerDescriptorList descriptors = handlers[updateType];
|
||||
logBuilder.Append("\n\tUpdateType." + updateType + " :");
|
||||
|
||||
foreach (HandlerDescriptor descriptor in descriptors.Reverse())
|
||||
{
|
||||
logBuilder.AppendFormat("\n\t* {0} - {1}",
|
||||
descriptor.Indexer.ToString(),
|
||||
descriptor.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
Logger.LogInformation(logBuilder.ToString());
|
||||
}
|
||||
|
||||
private void RegisterHostServices(WebApplicationBuilder hostApplicationBuilder, HostHandlersCollection handlers)
|
||||
{
|
||||
//hostApplicationBuilder.Services.RemoveAll<IHost>();
|
||||
//hostApplicationBuilder.Services.AddSingleton<IHost>(this);
|
||||
|
||||
hostApplicationBuilder.Services.AddSingleton<ITelegramBotHost>(this);
|
||||
hostApplicationBuilder.Services.AddSingleton<ITelegramBotWebHost>(this);
|
||||
hostApplicationBuilder.Services.AddSingleton<ITelegratorBot>(this);
|
||||
hostApplicationBuilder.Services.AddSingleton<IHandlersCollection>(handlers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user