| | 1 | | using Microsoft.Extensions.Configuration; |
| | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | 3 | | using Microsoft.Extensions.Hosting; |
| | 4 | |
|
| | 5 | | namespace NLightning.Application.NLTG.Extensions; |
| | 6 | |
|
| | 7 | | using Common.Options; |
| | 8 | | using Domain.Bitcoin.Services; |
| | 9 | | using Domain.Factories; |
| | 10 | | using Domain.Node.Options; |
| | 11 | | using Domain.Protocol.Factories; |
| | 12 | | using Domain.Protocol.Managers; |
| | 13 | | using Factories; |
| | 14 | | using Infrastructure.Node.Factories; |
| | 15 | | using Infrastructure.Node.Interfaces; |
| | 16 | | using Infrastructure.Node.Managers; |
| | 17 | | using Infrastructure.Protocol.Factories; |
| | 18 | | using Infrastructure.Transport.Factories; |
| | 19 | | using Interfaces; |
| | 20 | | using Managers; |
| | 21 | | using Services; |
| | 22 | |
|
| | 23 | | public static class NltgServiceExtensions |
| | 24 | | { |
| | 25 | | /// <summary> |
| | 26 | | /// Registers all NLTG application services for dependency injection |
| | 27 | | /// </summary> |
| | 28 | | public static IHostBuilder ConfigureNltgServices(this IHostBuilder hostBuilder, SecureKeyManager secureKeyManager) |
| | 29 | | { |
| 0 | 30 | | return hostBuilder.ConfigureServices((hostContext, services) => |
| 0 | 31 | | { |
| 0 | 32 | | // Get configuration |
| 0 | 33 | | var configuration = hostContext.Configuration; |
| 0 | 34 | |
|
| 0 | 35 | | // Register configuration as a service |
| 0 | 36 | | services.AddSingleton(configuration); |
| 0 | 37 | |
|
| 0 | 38 | | // Register the main daemon service |
| 0 | 39 | | services.AddHostedService<NltgDaemonService>(); |
| 0 | 40 | |
|
| 0 | 41 | | // Add HttpClient for FeeService with configuration |
| 0 | 42 | | services.AddHttpClient<IFeeService, FeeService>(client => |
| 0 | 43 | | { |
| 0 | 44 | | client.Timeout = TimeSpan.FromSeconds(30); |
| 0 | 45 | | client.DefaultRequestHeaders.Add("Accept", "application/json"); |
| 0 | 46 | | }); |
| 0 | 47 | |
|
| 0 | 48 | | // Register application services |
| 0 | 49 | | // Singleton services (one instance throughout the application) |
| 0 | 50 | | services.AddSingleton<IFeeService, FeeService>(); |
| 0 | 51 | | services.AddSingleton<IMessageFactory, MessageFactory>(); |
| 0 | 52 | | services.AddSingleton<IMessageServiceFactory, MessageServiceFactory>(); |
| 0 | 53 | | services.AddSingleton<IPeerFactory, PeerFactory>(); |
| 0 | 54 | | services.AddSingleton<IPeerManager, PeerManager>(); |
| 0 | 55 | | services.AddSingleton<IPingPongServiceFactory, PingPongServiceFactory>(); |
| 0 | 56 | | services.AddSingleton<ISecureKeyManager>(secureKeyManager); |
| 0 | 57 | | services.AddSingleton<ITcpListenerService, TcpListenerService>(); |
| 0 | 58 | | services.AddSingleton<ITransportServiceFactory, TransportServiceFactory>(); |
| 0 | 59 | |
|
| 0 | 60 | | // Scoped services (one instance per scope) |
| 0 | 61 | |
|
| 0 | 62 | | // Transient services (new instance each time) |
| 0 | 63 | |
|
| 0 | 64 | | // Register options with values from configuration |
| 0 | 65 | | services.AddOptions<FeeEstimationOptions>().BindConfiguration("FeeEstimation").ValidateOnStart(); |
| 0 | 66 | | services.AddOptions<NodeOptions>() |
| 0 | 67 | | .BindConfiguration("Node") |
| 0 | 68 | | .PostConfigure(options => |
| 0 | 69 | | { |
| 0 | 70 | | var configuredAddresses = configuration.GetSection("Node:ListenAddresses").Get<string[]?>(); |
| 0 | 71 | | if (configuredAddresses is { Length: > 0 }) |
| 0 | 72 | | { |
| 0 | 73 | | options.ListenAddresses = configuredAddresses.ToList(); |
| 0 | 74 | | } |
| 0 | 75 | | }) |
| 0 | 76 | | .ValidateOnStart(); |
| 0 | 77 | | }); |
| | 78 | | } |
| | 79 | | } |