forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModulesStartup.cs
More file actions
105 lines (96 loc) · 3.6 KB
/
ModulesStartup.cs
File metadata and controls
105 lines (96 loc) · 3.6 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
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using Console = Colorful.Console;
namespace BotSharp.Core.Modules
{
/// <summary>
/// Startup class for configurable modules
/// </summary>
public class ModulesStartup
{
private readonly IEnumerable<IModule> _modules;
private readonly IConfiguration _configuration;
/// <summary>
/// Create an instance of <see cref="ModulesStartup"/>
/// </summary>
/// <param name="configuration">
/// Application configuration containing modules configuration
/// </param>
public ModulesStartup(IConfiguration configuration)
{
this._configuration = configuration ??
throw new ArgumentNullException(nameof(configuration));
ModulesOptions options = configuration.Get<ModulesOptions>();
if (options.Modules.Count == 0)
{
options.Modules.Add(new ModuleOptions
{
Name = "DialogflowAi",
Type = "BotSharp.Platform.Dialogflow"
});
Console.WriteLine($"Platform emulator not found.", Color.Red);
Console.WriteLine($"Using default emulator: DialogflowAi, BotSharp.Platform.Dialogflow.");
}
Console.WriteLine($"Note: The red part information shown below can be ignored because the corresponding item is not referenced.", Color.Yellow);
this._modules = options.Modules
.Select(s =>
{
Type type = Type.GetType($"{s.Type}.ModuleInjector, {s.Type}");
if (type == null)
{
/* hard to debug in docker */
/* throw new TypeLoadException(
$"Cannot load type \"{s.Type}\"");*/
Console.WriteLine($"Cannot load type \"{s.Type}\"", Color.Red);
return null;
}
else
{
IModule module = (IModule)Activator.CreateInstance(type);
Console.WriteLine($"Loaded {s.Type.Split('.')[1]} \"{s.Type}\"", Color.Green);
return module;
}
}
);
}
/// <summary>
/// Configurates the services.
/// </summary>
/// <param name="services">
/// Instance of <see cref="IServiceCollection"/>.
/// </param>
/// <returns>
/// Instance of <see cref="IServiceProvider"/>.
/// </returns>
public void ConfigureServices(IServiceCollection services)
{
foreach (IModule module in this._modules)
{
if (module == null) continue;
module.ConfigureServices(services, this._configuration);
}
}
/// <summary>
/// Configures module services.
/// </summary>
/// <param name="app">
/// Instance of <see cref="IApplicationBuilder"/>.
/// </param>
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
foreach (IModule module in this._modules)
{
if (module == null) continue;
module.Configure(app, env);
}
}
}
}