| | 1 | | using Microsoft.Extensions.Configuration; |
| | 2 | | using Microsoft.Extensions.Hosting; |
| | 3 | | using Serilog; |
| | 4 | |
|
| | 5 | | namespace NLightning.Application.NLTG.Extensions; |
| | 6 | |
|
| | 7 | | using Helpers; |
| | 8 | |
|
| | 9 | | public static class NltgConfigurationExtensions |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Configures the host builder with NLTG configuration and Serilog |
| | 13 | | /// </summary> |
| | 14 | | public static IHostBuilder ConfigureNltg(this IHostBuilder hostBuilder, IConfiguration configuration) |
| | 15 | | { |
| | 16 | | // Configure the host builder |
| 0 | 17 | | return hostBuilder |
| 0 | 18 | | .ConfigureAppConfiguration(builder => |
| 0 | 19 | | { |
| 0 | 20 | | builder.AddConfiguration(configuration); |
| 0 | 21 | | }) |
| 0 | 22 | | .UseSerilog((_, _, loggerConfig) => |
| 0 | 23 | | { |
| 0 | 24 | | // Read from current configuration |
| 0 | 25 | | loggerConfig |
| 0 | 26 | | .ReadFrom.Configuration(configuration) |
| 0 | 27 | | .Enrich.With<ClassNameEnricher>(); |
| 0 | 28 | | }); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Configures the host builder with NLTG configuration and Serilog |
| | 33 | | /// </summary> |
| | 34 | | public static IHostBuilder ConfigureNltg(this IHostBuilder hostBuilder, string[] args) |
| | 35 | | { |
| 0 | 36 | | var config = ReadInitialConfiguration(args); |
| | 37 | |
|
| | 38 | | // Configure the host builder |
| 0 | 39 | | return hostBuilder |
| 0 | 40 | | .ConfigureAppConfiguration(builder => |
| 0 | 41 | | { |
| 0 | 42 | | builder.AddConfiguration(config); |
| 0 | 43 | | }) |
| 0 | 44 | | .UseSerilog((_, _, loggerConfig) => |
| 0 | 45 | | { |
| 0 | 46 | | // Read from current configuration |
| 0 | 47 | | loggerConfig |
| 0 | 48 | | .ReadFrom.Configuration(config) |
| 0 | 49 | | .Enrich.With<ClassNameEnricher>(); |
| 0 | 50 | | }); |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public static IConfiguration ReadInitialConfiguration(string[] args) |
| | 54 | | { |
| | 55 | | // Get network from the command line or environment variable first |
| 0 | 56 | | var initialConfig = new ConfigurationBuilder() |
| 0 | 57 | | .AddCommandLine(args) |
| 0 | 58 | | .AddEnvironmentVariables("NLTG_") |
| 0 | 59 | | .Build(); |
| 0 | 60 | | var network = initialConfig["network"] ?? initialConfig["n"] ?? "mainnet"; |
| | 61 | |
|
| | 62 | | // Check for custom config path first |
| 0 | 63 | | var configPath = initialConfig["config"] ?? initialConfig["c"]; |
| 0 | 64 | | var usingCustomConfig = !string.IsNullOrEmpty(configPath); |
| | 65 | |
|
| 0 | 66 | | if (usingCustomConfig) |
| | 67 | | { |
| 0 | 68 | | configPath = Path.GetFullPath(configPath!); |
| 0 | 69 | | if (!File.Exists(configPath)) |
| | 70 | | { |
| 0 | 71 | | Log.Warning("Custom configuration file not found at {ConfigPath}", configPath); |
| 0 | 72 | | usingCustomConfig = false; |
| | 73 | | } |
| | 74 | | } |
| | 75 | |
|
| | 76 | | // If no custom path, use default ~/.nltg/{network}/appsettings.json |
| 0 | 77 | | if (!usingCustomConfig) |
| | 78 | | { |
| 0 | 79 | | var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); |
| 0 | 80 | | var configDir = Path.Combine(homeDir, ".nltg", network); |
| 0 | 81 | | configPath = Path.Combine(configDir, "appsettings.json"); |
| | 82 | |
|
| | 83 | | // Ensure directory exists |
| 0 | 84 | | Directory.CreateDirectory(configDir); |
| | 85 | |
|
| | 86 | | // Create default config if none exists |
| 0 | 87 | | if (!File.Exists(configPath)) |
| | 88 | | { |
| 0 | 89 | | File.WriteAllText(configPath, CreateDefaultConfigJson()); |
| | 90 | | } |
| | 91 | | } |
| | 92 | |
|
| | 93 | | // Log startup info using bootstrap logger |
| 0 | 94 | | Log.Information("Starting NLTG with configuration from {ConfigPath} (Network: {Network})", configPath, network); |
| | 95 | |
|
| | 96 | | // Build configuration with proper precedence |
| 0 | 97 | | var config = new ConfigurationBuilder(); |
| 0 | 98 | | config.Sources.Clear(); |
| | 99 | |
|
| 0 | 100 | | return config |
| 0 | 101 | | .AddJsonFile(configPath!, optional: false, reloadOnChange: false) |
| 0 | 102 | | .AddEnvironmentVariables("NLTG_") |
| 0 | 103 | | .AddCommandLine(args) |
| 0 | 104 | | .Build(); |
| | 105 | | } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Creates default configuration JSON |
| | 109 | | /// </summary> |
| | 110 | | private static string CreateDefaultConfigJson() |
| | 111 | | { |
| 0 | 112 | | return """ |
| 0 | 113 | | { |
| 0 | 114 | | "Serilog": { |
| 0 | 115 | | "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], |
| 0 | 116 | | "MinimumLevel": { |
| 0 | 117 | | "Default": "Error", |
| 0 | 118 | | "Override": { |
| 0 | 119 | | "NLightning": "Information", |
| 0 | 120 | | "System": "Warning" |
| 0 | 121 | | } |
| 0 | 122 | | }, |
| 0 | 123 | | "WriteTo": [ |
| 0 | 124 | | { |
| 0 | 125 | | "Name": "Console", |
| 0 | 126 | | "Args": { |
| 0 | 127 | | "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] [{ClassName}] {Message:lj}{NewL |
| 0 | 128 | | } |
| 0 | 129 | | }, |
| 0 | 130 | | { |
| 0 | 131 | | "Name": "File", |
| 0 | 132 | | "Args": { |
| 0 | 133 | | "path": "logs/log-.txt", |
| 0 | 134 | | "rollingInterval": "Month", |
| 0 | 135 | | "retainedFileCountLimit": 12, |
| 0 | 136 | | "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] [{ClassName}] {Message:lj}{NewL |
| 0 | 137 | | } |
| 0 | 138 | | } |
| 0 | 139 | | ], |
| 0 | 140 | | "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "ClassName" ] |
| 0 | 141 | | }, |
| 0 | 142 | | "Node": { |
| 0 | 143 | | "Network": "regtest", |
| 0 | 144 | | "Daemon": false, |
| 0 | 145 | | "DnsSeedServers": [ |
| 0 | 146 | | "nlseed.nlightn.ing", |
| 0 | 147 | | "nodes.lightning.directory", |
| 0 | 148 | | "lseed.bitcoinstats.com" |
| 0 | 149 | | ], |
| 0 | 150 | | "ListenAddresses": [ |
| 0 | 151 | | "0.0.0.0:9735" |
| 0 | 152 | | ], |
| 0 | 153 | | "Features": { |
| 0 | 154 | | "StaticRemoteKey": "Compulsory" |
| 0 | 155 | | } |
| 0 | 156 | | }, |
| 0 | 157 | | "FeeEstimation": { |
| 0 | 158 | | "Url": "https://mempool.space/api/v1/fees/recommended", |
| 0 | 159 | | "Method": "GET", |
| 0 | 160 | | "ContentType": "application/json", |
| 0 | 161 | | "PreferredFeeRate": "fastestFee", |
| 0 | 162 | | "CacheExpiration": "5m", |
| 0 | 163 | | "RateMultiplier": 1000, |
| 0 | 164 | | "CacheFile": "fee_estimation_cache.bin" |
| 0 | 165 | | } |
| 0 | 166 | | } |
| 0 | 167 | | """; |
| | 168 | | } |
| | 169 | | } |