Files

71 lines
2.6 KiB
C#
Raw Permalink 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;
2020-03-01 22:09:14 +04:00
namespace Kruzya.TelegramBot.Core
{
public class Startup
{
private Core _core;
public Startup(IConfiguration configuration)
{
// TODO: add Core to DI.
_core = new Core(configuration);
}
// 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
// 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
2020-03-08 01:55:50 +04:00
// Add database context to DI.
2021-12-26 17:06:47 +04:00
var connectionString = _core.Configuration.GetConnectionString("DefaultConnection");
2020-03-08 01:55:50 +04:00
services.AddDbContext<CoreContext>(ctx =>
2021-12-26 17:06:47 +04:00
ctx.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
2022-01-03 23:25:20 +03:00
// Cache for users.
services.AddSingleton<ICacheStorage<long, User>, MemoryCache<long, User>>();
2022-01-04 17:45:06 +02:00
services.AddSingleton<UserService>();
2022-02-16 19:44:53 +03:00
services.AddSingleton<IAntiFlood, MemoryAntiFloodService>();
2022-01-04 17:45:06 +02:00
2023-01-17 22:42:48 +02:00
services.AddSingleton<ThreadSafeRandom>();
services.AddSingleton<ConcurrentBag<DeleteRequest>>();
services.AddHostedService<DeleteService>();
2023-07-20 20:24:16 +03:00
services.AddScoped<IOptionProvider, DbOptionProvider>();
2023-07-28 23:14:36 +03:00
services.AddHttpClient();
2023-07-20 20:24:16 +03:00
2020-03-02 08:49:57 +04:00
// Trigger module handlers.
2020-03-01 22:09:14 +04:00
_core.Modules.ConfigureServices(services);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
_core.Modules.Configure(app, env);
}
}
}