BotSharp/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs

121 lines
4 KiB
C#
Raw Normal View History

2023-08-19 00:15:50 +00:00
using BotSharp.Abstraction.Functions;
2023-05-27 01:58:31 +00:00
using Microsoft.AspNetCore.Builder;
2023-06-03 02:07:30 +00:00
using Microsoft.Extensions.Configuration;
2023-10-11 04:11:06 +00:00
using BotSharp.Core.Plugins;
using BotSharp.Abstraction.Settings;
2024-03-18 19:36:41 +00:00
using BotSharp.Abstraction.Options;
2024-03-18 19:52:38 +00:00
using BotSharp.Abstraction.Messaging.JsonConverters;
2024-05-26 01:34:29 +00:00
using BotSharp.Abstraction.Users.Settings;
2023-07-21 22:17:16 +00:00
2023-05-27 01:58:31 +00:00
namespace BotSharp.Core;
2023-11-29 23:23:14 +00:00
public static class BotSharpCoreExtensions
2023-05-27 01:58:31 +00:00
{
2024-03-18 19:36:41 +00:00
public static IServiceCollection AddBotSharpCore(this IServiceCollection services, IConfiguration config, Action<BotSharpOptions>? configOptions = null)
2023-05-27 01:58:31 +00:00
{
services.AddScoped<ISettingService, SettingService>();
2023-06-11 23:46:02 +00:00
services.AddScoped<IUserService, UserService>();
2024-04-30 12:50:01 +00:00
services.AddSingleton<DistributedLocker>();
2023-10-23 23:20:18 +00:00
2023-08-14 18:16:35 +00:00
RegisterPlugins(services, config);
2024-03-18 19:36:41 +00:00
ConfigureBotSharpOptions(services, configOptions);
2024-04-30 12:50:01 +00:00
2023-08-14 18:16:35 +00:00
return services;
}
2023-07-21 21:56:14 +00:00
2023-08-14 18:16:35 +00:00
public static IServiceCollection UsingSqlServer(this IServiceCollection services, IConfiguration config)
{
services.AddScoped<IBotSharpRepository>(sp =>
{
2023-09-01 22:33:36 +00:00
var myDatabaseSettings = sp.GetRequiredService<BotSharpDatabaseSettings>();
return DataContextHelper.GetSqlServerDbContext<BotSharpDbContext>(myDatabaseSettings, sp);
2023-08-14 18:16:35 +00:00
});
return services;
}
2023-09-03 04:54:22 +00:00
//public static IServiceCollection UsingFileRepository(this IServiceCollection services, IConfiguration config)
//{
// services.AddScoped<IBotSharpRepository>(sp =>
// {
// var myDatabaseSettings = sp.GetRequiredService<BotSharpDatabaseSettings>();
// return new FileRepository(myDatabaseSettings, sp);
// });
2023-07-21 21:56:14 +00:00
2023-09-03 04:54:22 +00:00
// return services;
//}
2023-05-27 01:58:31 +00:00
public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
2023-06-26 22:16:21 +00:00
app.ApplicationServices.GetRequiredService<PluginLoader>().Configure(app);
2023-05-27 01:58:31 +00:00
return app;
}
2023-06-03 02:07:30 +00:00
2024-03-18 19:36:41 +00:00
private static void ConfigureBotSharpOptions(IServiceCollection services, Action<BotSharpOptions>? configure)
{
var options = new BotSharpOptions();
if (configure != null)
{
configure(options);
}
2024-03-18 19:52:38 +00:00
AddDefaultJsonConverters(options);
2024-03-18 19:36:41 +00:00
services.AddSingleton(options);
}
2024-03-18 19:52:38 +00:00
private static void AddDefaultJsonConverters(BotSharpOptions options)
2024-03-18 19:36:41 +00:00
{
2024-03-18 19:52:38 +00:00
options.JsonSerializerOptions.Converters.Add(new RichContentJsonConverter());
options.JsonSerializerOptions.Converters.Add(new TemplateMessageJsonConverter());
2024-03-18 19:36:41 +00:00
}
2023-06-10 02:56:04 +00:00
public static void RegisterPlugins(IServiceCollection services, IConfiguration config)
{
2023-10-11 04:11:06 +00:00
var pluginSettings = new PluginSettings();
2023-06-17 02:42:35 +00:00
config.Bind("PluginLoader", pluginSettings);
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<PluginSettings>("PluginLoader");
});
2023-06-11 23:46:02 +00:00
2024-05-26 01:34:29 +00:00
var accountSettings = new AccountSetting();
config.Bind("Account", accountSettings);
services.AddScoped(x => accountSettings);
2023-06-17 02:42:35 +00:00
var loader = new PluginLoader(services, config, pluginSettings);
2023-09-24 16:20:02 +00:00
loader.Load(assembly =>
{
// Register routing handlers
var handlers = assembly.GetTypes()
.Where(x => x.IsClass)
.Where(x => x.GetInterface(nameof(IRoutingHandler)) != null)
.ToArray();
foreach (var handler in handlers)
{
services.AddScoped(typeof(IRoutingHandler), handler);
}
// Register function callback
var functions = assembly.GetTypes()
.Where(x => x.IsClass)
.Where(x => x.GetInterface(nameof(IFunctionCallback)) != null)
.ToArray();
foreach (var function in functions)
{
services.AddScoped(typeof(IFunctionCallback), function);
}
});
2023-06-26 22:16:21 +00:00
services.AddSingleton(loader);
2023-06-10 02:56:04 +00:00
}
2023-05-27 01:58:31 +00:00
}