-
-
Notifications
You must be signed in to change notification settings - Fork 637
Expand file tree
/
Copy pathBotSharpCoreExtensions.cs
More file actions
160 lines (132 loc) · 5.49 KB
/
BotSharpCoreExtensions.cs
File metadata and controls
160 lines (132 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using BotSharp.Abstraction.Functions;
using BotSharp.Core.Plugins;
using BotSharp.Abstraction.Settings;
using BotSharp.Abstraction.Options;
using BotSharp.Abstraction.Messaging.JsonConverters;
using BotSharp.Abstraction.Users.Settings;
using BotSharp.Abstraction.Infrastructures;
using BotSharp.Core.Processors;
using StackExchange.Redis;
using BotSharp.Core.Infrastructures.Events;
using BotSharp.Core.Roles.Services;
using BotSharp.Abstraction.Templating;
using BotSharp.Core.Templating;
using BotSharp.Abstraction.Infrastructures.Enums;
using BotSharp.Abstraction.Realtime;
using BotSharp.Abstraction.Repositories.Settings;
namespace BotSharp.Core;
public static class BotSharpCoreExtensions
{
public static IServiceCollection AddBotSharpCore(this IServiceCollection services, IConfiguration config, Action<BotSharpOptions>? configOptions = null)
{
services.AddSingleton<IDistributedLocker, DistributedLocker>();
// Register template render
services.AddSingleton<IRenderConfiguration, RenderConfiguration>();
services.AddScoped<ITemplateRender, TemplateRender>();
services.AddScoped<ISettingService, SettingService>();
services.AddScoped<IRoleService, RoleService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<ProcessorFactory>();
AddRedisEvents(services, config);
// Register cache service
AddCacheServices(services, config);
RegisterPlugins(services, config);
AddBotSharpOptions(services, configOptions);
return services;
}
private static void AddCacheServices(IServiceCollection services, IConfiguration config)
{
services.AddMemoryCache();
var cacheSettings = new SharpCacheSettings();
config.Bind("SharpCache", cacheSettings);
services.AddSingleton(x => cacheSettings);
var dbSettings = new BotSharpDatabaseSettings();
config.Bind("Database", dbSettings);
if (!string.IsNullOrEmpty(dbSettings.Redis))
{
services.AddSingleton<RedisCacheService>();
}
services.AddSingleton<ICacheService>(sp => cacheSettings.CacheType switch
{
CacheType.RedisCache => ActivatorUtilities.CreateInstance<RedisCacheService>(sp),
_ => ActivatorUtilities.CreateInstance<MemoryCacheService>(sp),
});
}
public static IServiceCollection UsingSqlServer(this IServiceCollection services, IConfiguration config)
{
services.AddScoped<IBotSharpRepository>(sp =>
{
var myDatabaseSettings = sp.GetRequiredService<BotSharpDatabaseSettings>();
return DataContextHelper.GetSqlServerDbContext<BotSharpDbContext>(myDatabaseSettings, sp);
});
return services;
}
public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
app.ApplicationServices.GetRequiredService<PluginLoader>().Configure(app);
// Set root services for SharpCacheAttribute
SharpCacheAttribute.Services = app.ApplicationServices;
return app;
}
private static void AddBotSharpOptions(IServiceCollection services, Action<BotSharpOptions>? configure)
{
var options = new BotSharpOptions();
if (configure != null)
{
configure(options);
}
AddDefaultJsonConverters(options);
services.AddSingleton(options);
}
private static void AddRedisEvents(IServiceCollection services, IConfiguration config)
{
// Add Redis connection as a singleton
var dbSettings = new BotSharpDatabaseSettings();
config.Bind("Database", dbSettings);
if (string.IsNullOrEmpty(dbSettings.Redis))
{
return;
}
services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(dbSettings.Redis));
services.AddSingleton<IEventPublisher, RedisPublisher>();
services.AddSingleton<IEventSubscriber, RedisSubscriber>();
}
private static void AddDefaultJsonConverters(BotSharpOptions options)
{
options.JsonSerializerOptions.Converters.Add(new RichContentJsonConverter());
options.JsonSerializerOptions.Converters.Add(new TemplateMessageJsonConverter());
}
public static void RegisterPlugins(IServiceCollection services, IConfiguration config)
{
var pluginSettings = new PluginSettings();
config.Bind("PluginLoader", pluginSettings);
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<PluginSettings>("PluginLoader");
});
var accountSettings = new AccountSetting();
config.Bind("Account", accountSettings);
services.AddScoped(x => accountSettings);
var loader = new PluginLoader(services, config, pluginSettings);
loader.Load(assembly =>
{
// Register function callback
var functions = assembly.GetTypes()
.Where(x => x.IsClass
&& x.GetInterface(nameof(IFunctionCallback)) != null)
.ToArray();
foreach (var function in functions)
{
services.AddScoped(typeof(IFunctionCallback), function);
}
});
services.AddSingleton(loader);
}
}