|
1 | 1 | using Microsoft.Extensions.DependencyInjection; |
2 | 2 | using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 3 | +using Microsoft.Extensions.AI; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Microsoft.Extensions.Options; |
3 | 6 | using NLWebNet.Models; |
4 | 7 | using NLWebNet.Services; |
5 | 8 | using NLWebNet.MCP; |
@@ -94,4 +97,104 @@ public static IServiceCollection AddNLWebNet<TDataBackend>(this IServiceCollecti |
94 | 97 |
|
95 | 98 | return services; |
96 | 99 | } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Adds NLWebNet services with multi-backend support |
| 103 | + /// </summary> |
| 104 | + /// <param name="services">The service collection</param> |
| 105 | + /// <param name="configureOptions">Optional configuration callback</param> |
| 106 | + /// <param name="configureMultiBackend">Multi-backend configuration callback</param> |
| 107 | + /// <returns>The service collection for chaining</returns> |
| 108 | + public static IServiceCollection AddNLWebNetMultiBackend(this IServiceCollection services, |
| 109 | + Action<NLWebOptions>? configureOptions = null, |
| 110 | + Action<MultiBackendOptions>? configureMultiBackend = null) |
| 111 | + { |
| 112 | + // Configure options |
| 113 | + if (configureOptions != null) |
| 114 | + { |
| 115 | + services.Configure(configureOptions); |
| 116 | + } |
| 117 | + |
| 118 | + // Configure multi-backend options |
| 119 | + if (configureMultiBackend != null) |
| 120 | + { |
| 121 | + services.Configure<MultiBackendOptions>(configureMultiBackend); |
| 122 | + } |
| 123 | + |
| 124 | + // Add logging (required for the services) |
| 125 | + services.AddLogging(); |
| 126 | + |
| 127 | + // Register core NLWebNet services |
| 128 | + services.AddScoped<INLWebService>(provider => |
| 129 | + { |
| 130 | + var options = provider.GetRequiredService<IOptions<NLWebOptions>>(); |
| 131 | + if (options.Value.MultiBackend.Enabled) |
| 132 | + { |
| 133 | + // Use multi-backend constructor |
| 134 | + return new NLWebService( |
| 135 | + provider.GetRequiredService<IQueryProcessor>(), |
| 136 | + provider.GetRequiredService<IResultGenerator>(), |
| 137 | + provider.GetRequiredService<IBackendManager>(), |
| 138 | + provider.GetRequiredService<ILogger<NLWebService>>(), |
| 139 | + options); |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + // Use single backend constructor for backward compatibility |
| 144 | + return new NLWebService( |
| 145 | + provider.GetRequiredService<IQueryProcessor>(), |
| 146 | + provider.GetRequiredService<IResultGenerator>(), |
| 147 | + provider.GetRequiredService<IDataBackend>(), |
| 148 | + provider.GetRequiredService<ILogger<NLWebService>>(), |
| 149 | + options); |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + services.AddScoped<IQueryProcessor, QueryProcessor>(); |
| 154 | + services.AddScoped<IResultGenerator>(provider => |
| 155 | + { |
| 156 | + var options = provider.GetRequiredService<IOptions<NLWebOptions>>(); |
| 157 | + if (options.Value.MultiBackend.Enabled) |
| 158 | + { |
| 159 | + // Use multi-backend constructor |
| 160 | + return new ResultGenerator( |
| 161 | + provider.GetRequiredService<IBackendManager>(), |
| 162 | + provider.GetRequiredService<ILogger<ResultGenerator>>(), |
| 163 | + options, |
| 164 | + provider.GetService<IChatClient>()); |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + // Use single backend constructor for backward compatibility |
| 169 | + return new ResultGenerator( |
| 170 | + provider.GetRequiredService<IDataBackend>(), |
| 171 | + provider.GetRequiredService<ILogger<ResultGenerator>>(), |
| 172 | + options, |
| 173 | + provider.GetService<IChatClient>()); |
| 174 | + } |
| 175 | + }); |
| 176 | + |
| 177 | + // Register MCP services |
| 178 | + services.AddScoped<IMcpService, McpService>(); |
| 179 | + |
| 180 | + // Register multi-backend services |
| 181 | + services.AddScoped<IBackendManager, BackendManager>(); |
| 182 | + |
| 183 | + // Register default data backend (can be overridden) |
| 184 | + services.AddScoped<IDataBackend, MockDataBackend>(); |
| 185 | + |
| 186 | + // Add health checks |
| 187 | + services.AddHealthChecks() |
| 188 | + .AddCheck<NLWebHealthCheck>("nlweb") |
| 189 | + .AddCheck<DataBackendHealthCheck>("data-backend") |
| 190 | + .AddCheck<AIServiceHealthCheck>("ai-service"); |
| 191 | + |
| 192 | + // Add metrics |
| 193 | + services.AddMetrics(); |
| 194 | + |
| 195 | + // Add rate limiting |
| 196 | + services.AddSingleton<IRateLimitingService, InMemoryRateLimitingService>(); |
| 197 | + |
| 198 | + return services; |
| 199 | + } |
97 | 200 | } |
0 commit comments