Files
telegram-bot/Core/Startup.cs
T

77 lines
2.7 KiB
C#
Raw Normal View History

2020-03-01 22:09:14 +04:00
using BotFramework;
using Kruzya.TelegramBot.Core.AutoDelete;
2022-01-03 23:25:20 +03:00
using Kruzya.TelegramBot.Core.Cache;
2020-03-08 01:55:50 +04:00
using Kruzya.TelegramBot.Core.Data;
2020-03-01 22:46:07 +04:00
using Kruzya.TelegramBot.Core.Extensions;
2022-01-04 17:45:06 +02:00
using Kruzya.TelegramBot.Core.Service;
2020-03-01 22:09:14 +04:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
2020-03-08 01:55:50 +04:00
using Microsoft.EntityFrameworkCore;
2020-03-01 22:09:14 +04:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
2023-07-20 20:24:16 +03:00
using Kruzya.TelegramBot.Core.Options;
2022-01-03 23:25:20 +03:00
using Telegram.Bot.Types;
2024-01-28 18:51:18 +03:00
using BotFramework.Abstractions.UpdateProvider;
2020-03-01 22:09:14 +04:00
2023-07-29 15:14:52 +03:00
namespace Kruzya.TelegramBot.Core;
public class Startup
2020-03-01 22:09:14 +04:00
{
2023-07-29 15:14:52 +03:00
private Core _core;
2020-03-01 22:09:14 +04:00
2023-07-29 15:14:52 +03:00
public Startup(IConfiguration configuration)
{
// TODO: add Core to DI.
_core = new Core(configuration);
}
2020-03-01 22:09:14 +04:00
2023-07-29 15:14:52 +03:00
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddTelegramBot();
services.AddLogging(builder => builder.AddConsole());
2020-03-02 08:49:57 +04:00
2023-07-29 15:14:52 +03:00
// Add all modules to DI with Core instance.
services.AddSingleton<Core>(_core);
foreach (var module in _core.Modules) services.AddSingleton(module.GetType(), module);
2020-03-01 22:46:07 +04:00
2023-07-29 15:14:52 +03:00
// Add database context to DI.
var connectionString = _core.Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<CoreContext>(ctx =>
ctx.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
2021-12-26 17:06:47 +04:00
2023-07-29 15:14:52 +03:00
// Cache for users.
services.AddSingleton<ICacheStorage<long, User>, MemoryCache<long, User>>();
2022-01-03 23:25:20 +03:00
2023-07-29 15:14:52 +03:00
services.AddSingleton<UserService>();
services.AddSingleton<IAntiFlood, MemoryAntiFloodService>();
2022-01-04 17:45:06 +02:00
2023-07-29 15:14:52 +03:00
services.AddSingleton<ThreadSafeRandom>();
2024-01-28 18:51:18 +03:00
services.AddSingleton<IWebhookProvider, WebhookAspNetProvider>();
services.AddControllersWithViews();
2023-07-29 15:14:52 +03:00
services.AddSingleton<ConcurrentBag<DeleteRequest>>();
services.AddHostedService<DeleteService>();
2023-07-29 15:14:52 +03:00
services.AddScoped<IOptionProvider, DbOptionProvider>();
services.AddHttpClient();
2023-07-20 20:24:16 +03:00
2023-07-29 15:14:52 +03:00
// Trigger module handlers.
_core.Modules.ConfigureServices(services);
}
2020-03-01 22:09:14 +04:00
2023-07-29 15:14:52 +03:00
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
2024-01-28 18:51:18 +03:00
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
2023-07-29 15:14:52 +03:00
_core.Modules.Configure(app, env);
2020-03-01 22:09:14 +04:00
}
2023-07-29 15:14:52 +03:00
}