-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathBootstrapper.cs
More file actions
74 lines (66 loc) · 3.18 KB
/
Bootstrapper.cs
File metadata and controls
74 lines (66 loc) · 3.18 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
using MaIN.Domain.Configuration;
using MaIN.Infrastructure.Configuration;
using MaIN.Infrastructure.Repositories;
using MaIN.Domain.Repositories;
using MaIN.Infrastructure.Repositories.FileSystem;
using MaIN.Infrastructure.Repositories.Mongo;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
namespace MaIN.Infrastructure;
public static class Bootstrapper
{
public static IServiceCollection ConfigureInfrastructure(this IServiceCollection services,
IConfiguration configuration)
{
var settings = new MaINSettings();
configuration.GetSection("MaIN").Bind(settings);
if (settings.MongoDbSettings != null)
{
services.AddSingleton<IMongoClient, MongoClient>(sp =>
new MongoClient(settings.MongoDbSettings?.ConnectionString));
services.AddSingleton<IChatRepository, MongoChatRepository>(sp =>
{
var mongoClient = sp.GetRequiredService<IMongoClient>();
var database = mongoClient.GetDatabase(settings.MongoDbSettings?.DatabaseName!);
return new MongoChatRepository(database, settings.MongoDbSettings?.ChatsCollection!);
});
services.AddSingleton<IAgentRepository, MongoAgentRepository>(sp =>
{
var mongoClient = sp.GetRequiredService<IMongoClient>();
var database = mongoClient.GetDatabase(settings.MongoDbSettings?.DatabaseName!);
return new MongoAgentRepository(database, settings.MongoDbSettings?.AgentsCollection!);
});
services.AddSingleton<IAgentFlowRepository, MongoAgentFlowRepository>(sp =>
{
var mongoClient = sp.GetRequiredService<IMongoClient>();
var database = mongoClient.GetDatabase(settings.MongoDbSettings?.DatabaseName!);
return new MongoAgentFlowRepository(database, settings.MongoDbSettings?.FlowsCollection!);
});
}
else if (settings.FileSystemSettings != null)
{
services.AddSingleton<IChatRepository, FileSystemChatRepository>((_) =>
new FileSystemChatRepository(settings.FileSystemSettings.Path!));
services.AddSingleton<IAgentRepository, FileSystemAgentRepository>((_) =>
new FileSystemAgentRepository(settings.FileSystemSettings.Path!));
services.AddSingleton<IAgentFlowRepository, FileSystemAgentFlowRepository>((_) =>
new FileSystemAgentFlowRepository(settings.FileSystemSettings.Path!));
}
else if (settings.SqliteSettings != null)
{
services.AddSqliteRepositories(settings.SqliteSettings.ConnectionString!);
}
else if (settings.SqlSettings != null)
{
services.AddSqlRepositories(settings.SqlSettings.ConnectionString!);
}
else
{
services.AddSingleton<IAgentFlowRepository, DefaultAgentFlowRepository>();
services.AddSingleton<IAgentRepository, DefaultAgentRepository>();
services.AddSingleton<IChatRepository, DefaultChatRepository>();
}
return services;
}
}