public void Start() { if (!Directory.Exists(_Directory)) { Directory.CreateDirectory(_Directory); } string chain = NBXplorerDefaultSettings.GetFolderName(NetworkType.Regtest); string chainDirectory = Path.Combine(_Directory, chain); if (!Directory.Exists(chainDirectory)) { Directory.CreateDirectory(chainDirectory); } StringBuilder config = new StringBuilder(); config.AppendLine($"{chain.ToLowerInvariant()}=1"); if (InContainer) { config.AppendLine($"bind=0.0.0.0"); } config.AppendLine($"port={Port}"); config.AppendLine($"chains=btc,ltc"); config.AppendLine($"btc.explorer.url={NBXplorerUri.AbsoluteUri}"); config.AppendLine($"btc.explorer.cookiefile=0"); config.AppendLine("allow-admin-registration=1"); config.AppendLine($"ltc.explorer.url={LTCNBXplorerUri.AbsoluteUri}"); config.AppendLine($"ltc.explorer.cookiefile=0"); config.AppendLine($"btc.lightning={IntegratedLightning.AbsoluteUri}"); if (!string.IsNullOrEmpty(SSHPassword) && string.IsNullOrEmpty(SSHKeyFile)) { config.AppendLine($"sshpassword={SSHPassword}"); } if (!string.IsNullOrEmpty(SSHKeyFile)) { config.AppendLine($"sshkeyfile={SSHKeyFile}"); } if (!string.IsNullOrEmpty(SSHConnection)) { config.AppendLine($"sshconnection={SSHConnection}"); } if (TestDatabase == TestDatabases.MySQL && !String.IsNullOrEmpty(MySQL)) { config.AppendLine($"mysql=" + MySQL); } else if (!String.IsNullOrEmpty(Postgres)) { config.AppendLine($"postgres=" + Postgres); } var confPath = Path.Combine(chainDirectory, "settings.config"); File.WriteAllText(confPath, config.ToString()); ServerUri = new Uri("http://" + HostName + ":" + Port + "/"); HttpClient = new HttpClient(); HttpClient.BaseAddress = ServerUri; Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"); var conf = new DefaultConfiguration() { Logger = Logs.LogProvider.CreateLogger("Console") }.CreateConfiguration(new[] { "--datadir", _Directory, "--conf", confPath, "--disable-registration", "false" }); _Host = new WebHostBuilder() .UseConfiguration(conf) .UseContentRoot(FindBTCPayServerDirectory()) .ConfigureServices(s => { s.AddLogging(l => { l.AddFilter("System.Net.Http.HttpClient", LogLevel.Critical); l.SetMinimumLevel(LogLevel.Information) .AddFilter("Microsoft", LogLevel.Error) .AddFilter("Hangfire", LogLevel.Error) .AddProvider(Logs.LogProvider); }); }) .UseKestrel() .UseStartup <Startup>() .Build(); _Host.StartWithTasksAsync().GetAwaiter().GetResult(); var urls = _Host.ServerFeatures.Get <IServerAddressesFeature>().Addresses; foreach (var url in urls) { Logs.Tester.LogInformation("Listening on " + url); } Logs.Tester.LogInformation("Server URI " + ServerUri); InvoiceRepository = (InvoiceRepository)_Host.Services.GetService(typeof(InvoiceRepository)); StoreRepository = (StoreRepository)_Host.Services.GetService(typeof(StoreRepository)); Networks = (BTCPayNetworkProvider)_Host.Services.GetService(typeof(BTCPayNetworkProvider)); if (MockRates) { var rateProvider = (RateProviderFactory)_Host.Services.GetService(typeof(RateProviderFactory)); rateProvider.Providers.Clear(); var coinAverageMock = new MockRateProvider(); coinAverageMock.ExchangeRates.Add(new Rating.ExchangeRate() { Exchange = "coinaverage", CurrencyPair = CurrencyPair.Parse("BTC_USD"), BidAsk = new BidAsk(5000m) }); coinAverageMock.ExchangeRates.Add(new Rating.ExchangeRate() { Exchange = "coinaverage", CurrencyPair = CurrencyPair.Parse("BTC_CAD"), BidAsk = new BidAsk(4500m) }); coinAverageMock.ExchangeRates.Add(new Rating.ExchangeRate() { Exchange = "coinaverage", CurrencyPair = CurrencyPair.Parse("LTC_BTC"), BidAsk = new BidAsk(0.001m) }); coinAverageMock.ExchangeRates.Add(new Rating.ExchangeRate() { Exchange = "coinaverage", CurrencyPair = CurrencyPair.Parse("LTC_USD"), BidAsk = new BidAsk(500m) }); rateProvider.Providers.Add("coinaverage", coinAverageMock); var bitflyerMock = new MockRateProvider(); bitflyerMock.ExchangeRates.Add(new Rating.ExchangeRate() { Exchange = "bitflyer", CurrencyPair = CurrencyPair.Parse("BTC_JPY"), BidAsk = new BidAsk(700000m) }); rateProvider.Providers.Add("bitflyer", bitflyerMock); var quadrigacx = new MockRateProvider(); quadrigacx.ExchangeRates.Add(new Rating.ExchangeRate() { Exchange = "quadrigacx", CurrencyPair = CurrencyPair.Parse("BTC_CAD"), BidAsk = new BidAsk(6000m) }); rateProvider.Providers.Add("quadrigacx", quadrigacx); var bittrex = new MockRateProvider(); bittrex.ExchangeRates.Add(new Rating.ExchangeRate() { Exchange = "bittrex", CurrencyPair = CurrencyPair.Parse("DOGE_BTC"), BidAsk = new BidAsk(0.004m) }); rateProvider.Providers.Add("bittrex", bittrex); } WaitSiteIsOperational().GetAwaiter().GetResult(); }
public async Task StartAsync() { if (!Directory.Exists(_Directory)) { Directory.CreateDirectory(_Directory); } string chain = NBXplorerDefaultSettings.GetFolderName(NetworkType.Regtest); string chainDirectory = Path.Combine(_Directory, chain); if (!Directory.Exists(chainDirectory)) { Directory.CreateDirectory(chainDirectory); } StringBuilder config = new StringBuilder(); config.AppendLine($"{chain.ToLowerInvariant()}=1"); if (InContainer) { config.AppendLine($"bind=0.0.0.0"); } config.AppendLine($"port={Port}"); config.AppendLine($"chains={string.Join(',', Chains)}"); if (Chains.Contains("BTC", StringComparer.OrdinalIgnoreCase)) { config.AppendLine($"btc.explorer.url={NBXplorerUri.AbsoluteUri}"); config.AppendLine($"btc.explorer.cookiefile=0"); } if (UseLightning) { config.AppendLine($"btc.lightning={IntegratedLightning.AbsoluteUri}"); var localLndBackupFile = Path.Combine(_Directory, "walletunlock.json"); File.Copy(TestUtils.GetTestDataFullPath("LndSeedBackup/walletunlock.json"), localLndBackupFile, true); config.AppendLine($"btc.external.lndseedbackup={localLndBackupFile}"); } if (Chains.Contains("LTC", StringComparer.OrdinalIgnoreCase)) { config.AppendLine($"ltc.explorer.url={LTCNBXplorerUri.AbsoluteUri}"); config.AppendLine($"ltc.explorer.cookiefile=0"); } if (Chains.Contains("LBTC", StringComparer.OrdinalIgnoreCase)) { config.AppendLine($"lbtc.explorer.url={LBTCNBXplorerUri.AbsoluteUri}"); config.AppendLine($"lbtc.explorer.cookiefile=0"); } if (AllowAdminRegistration) { config.AppendLine("allow-admin-registration=1"); } config.AppendLine($"torrcfile={TestUtils.GetTestDataFullPath("Tor/torrc")}"); config.AppendLine($"socksendpoint={SocksEndpoint}"); config.AppendLine($"debuglog=debug.log"); if (!string.IsNullOrEmpty(SSHPassword) && string.IsNullOrEmpty(SSHKeyFile)) { config.AppendLine($"sshpassword={SSHPassword}"); } if (!string.IsNullOrEmpty(SSHKeyFile)) { config.AppendLine($"sshkeyfile={SSHKeyFile}"); } if (!string.IsNullOrEmpty(SSHConnection)) { config.AppendLine($"sshconnection={SSHConnection}"); } if (TestDatabase == TestDatabases.MySQL && !String.IsNullOrEmpty(MySQL)) { config.AppendLine($"mysql=" + MySQL); } else if (!String.IsNullOrEmpty(Postgres)) { config.AppendLine($"postgres=" + Postgres); } var confPath = Path.Combine(chainDirectory, "settings.config"); await File.WriteAllTextAsync(confPath, config.ToString()); ServerUri = new Uri("http://" + HostName + ":" + Port + "/"); HttpClient = new HttpClient(); HttpClient.BaseAddress = ServerUri; Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"); var conf = new DefaultConfiguration() { Logger = Logs.LogProvider.CreateLogger("Console") }.CreateConfiguration(new[] { "--datadir", _Directory, "--conf", confPath, "--disable-registration", DisableRegistration ? "true" : "false" }); _Host = new WebHostBuilder() .UseConfiguration(conf) .UseContentRoot(FindBTCPayServerDirectory()) .UseWebRoot(Path.Combine(FindBTCPayServerDirectory(), "wwwroot")) .ConfigureServices(s => { s.AddLogging(l => { l.AddFilter("System.Net.Http.HttpClient", LogLevel.Critical); l.SetMinimumLevel(LogLevel.Information) .AddFilter("Microsoft", LogLevel.Error) .AddFilter("Hangfire", LogLevel.Error) .AddProvider(Logs.LogProvider); }); }) .ConfigureServices(services => { services.TryAddSingleton <IFeeProviderFactory>(new BTCPayServer.Services.Fees.FixedFeeProvider(new FeeRate(100L, 1))); }) .UseKestrel() .UseStartup <Startup>() .Build(); await _Host.StartWithTasksAsync(); var urls = _Host.ServerFeatures.Get <IServerAddressesFeature>().Addresses; foreach (var url in urls) { Logs.Tester.LogInformation("Listening on " + url); } Logs.Tester.LogInformation("Server URI " + ServerUri); InvoiceRepository = (InvoiceRepository)_Host.Services.GetService(typeof(InvoiceRepository)); StoreRepository = (StoreRepository)_Host.Services.GetService(typeof(StoreRepository)); Networks = (BTCPayNetworkProvider)_Host.Services.GetService(typeof(BTCPayNetworkProvider)); if (MockRates) { var rateProvider = (RateProviderFactory)_Host.Services.GetService(typeof(RateProviderFactory)); rateProvider.Providers.Clear(); coinAverageMock = new MockRateProvider(); coinAverageMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_USD"), new BidAsk(5000m))); coinAverageMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_CAD"), new BidAsk(4500m))); coinAverageMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_LTC"), new BidAsk(162m))); coinAverageMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("LTC_USD"), new BidAsk(500m))); rateProvider.Providers.Add("coingecko", coinAverageMock); var bitflyerMock = new MockRateProvider(); bitflyerMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_JPY"), new BidAsk(700000m))); rateProvider.Providers.Add("bitflyer", bitflyerMock); var quadrigacx = new MockRateProvider(); quadrigacx.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_CAD"), new BidAsk(6000m))); rateProvider.Providers.Add("quadrigacx", quadrigacx); var bittrex = new MockRateProvider(); bittrex.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("DOGE_BTC"), new BidAsk(0.004m))); rateProvider.Providers.Add("bittrex", bittrex); var bitfinex = new MockRateProvider(); bitfinex.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("UST_BTC"), new BidAsk(0.000136m))); rateProvider.Providers.Add("bitfinex", bitfinex); var bitpay = new MockRateProvider(); bitpay.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("ETB_BTC"), new BidAsk(0.1m))); rateProvider.Providers.Add("bitpay", bitpay); } Logs.Tester.LogInformation("Waiting site is operational..."); await WaitSiteIsOperational(); Logs.Tester.LogInformation("Site is now operational"); }
static void Main(string[] args) { if (args.Length > 0 && args[0] == "run") { args = args.Skip(1).ToArray(); // Hack to make dotnet watch work } ServicePointManager.DefaultConnectionLimit = 100; IWebHost host = null; var processor = new ConsoleLoggerProcessor(); CustomConsoleLogProvider loggerProvider = new CustomConsoleLogProvider(processor); using var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(loggerProvider); var logger = loggerFactory.CreateLogger("Configuration"); Logs logs = new Logs(); logs.Configure(loggerFactory); IConfiguration conf = null; try { conf = new DefaultConfiguration() { Logger = logger }.CreateConfiguration(args); if (conf == null) { return; } var builder = new WebHostBuilder() .UseKestrel() .UseConfiguration(conf) .ConfigureLogging(l => { l.AddFilter("Microsoft", LogLevel.Error); if (!conf.GetOrDefault <bool>("verbose", false)) { l.AddFilter("Events", LogLevel.Warning); } l.AddFilter("System.Net.Http.HttpClient", LogLevel.Critical); l.AddFilter("Microsoft.AspNetCore.Antiforgery.Internal", LogLevel.Critical); l.AddFilter("Fido2NetLib.DistributedCacheMetadataService", LogLevel.Error); l.AddProvider(new CustomConsoleLogProvider(processor)); }) .UseStartup <Startup>(); // When we run the app with dotnet run (typically in dev env), the wwwroot isn't in the same directory // than this assembly. // But when we use dotnet publish, the wwwroot is published alongside the assembly! // This fix https://github.com/btcpayserver/btcpayserver/issues/1894 var defaultContentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var defaultWebRoot = Path.Combine(defaultContentPath, "wwwroot"); var defaultWebRootExists = Directory.Exists(defaultWebRoot); if (!defaultWebRootExists) { // When we use dotnet run... builder.UseContentRoot(Directory.GetCurrentDirectory()); } host = builder.Build(); host.StartWithTasksAsync().GetAwaiter().GetResult(); var urls = host.ServerFeatures.Get <IServerAddressesFeature>().Addresses; foreach (var url in urls) { // Some tools such as dotnet watch parse this exact log to open the browser logger.LogInformation("Now listening on: " + url); } host.WaitForShutdown(); } catch (ConfigException ex) { if (!string.IsNullOrEmpty(ex.Message)) { logs.Configuration.LogError(ex.Message); } } catch (Exception e) when(PluginManager.IsExceptionByPlugin(e)) { var pluginDir = new DataDirectories().Configure(conf).PluginDir; PluginManager.DisablePlugin(pluginDir, e.Source); } finally { processor.Dispose(); if (host == null) { logs.Configuration.LogError("Configuration error"); } if (host != null) { host.Dispose(); } Serilog.Log.CloseAndFlush(); loggerProvider.Dispose(); } }