Fix tests, clean code of Options

This commit is contained in:
nicolas.dorier
2018-01-12 16:00:31 +09:00
parent ef165e15bf
commit 58194cb060
4 changed files with 87 additions and 54 deletions

View File

@@ -67,9 +67,14 @@ namespace BTCPayServer.Tests
{ {
if (!Directory.Exists(_Directory)) if (!Directory.Exists(_Directory))
Directory.CreateDirectory(_Directory); Directory.CreateDirectory(_Directory);
string chain = ChainType.Regtest.ToNetwork().Name;
string chainDirectory = Path.Combine(_Directory, chain);
if (!Directory.Exists(chainDirectory))
Directory.CreateDirectory(chainDirectory);
StringBuilder config = new StringBuilder(); StringBuilder config = new StringBuilder();
config.AppendLine($"regtest=1"); config.AppendLine($"{chain.ToLowerInvariant()}=1");
config.AppendLine($"port={Port}"); config.AppendLine($"port={Port}");
config.AppendLine($"chains=btc,ltc"); config.AppendLine($"chains=btc,ltc");
@@ -81,11 +86,12 @@ namespace BTCPayServer.Tests
if (Postgres != null) if (Postgres != null)
config.AppendLine($"postgres=" + Postgres); config.AppendLine($"postgres=" + Postgres);
File.WriteAllText(Path.Combine(_Directory, "settings.config"), config.ToString()); var confPath = Path.Combine(chainDirectory, "settings.config");
File.WriteAllText(confPath, config.ToString());
ServerUri = new Uri("http://" + HostName + ":" + Port + "/"); ServerUri = new Uri("http://" + HostName + ":" + Port + "/");
var conf = new DefaultConfiguration() { Logger = Logs.LogProvider.CreateLogger("Console") }.CreateConfiguration(new[] { "--datadir", _Directory }); var conf = new DefaultConfiguration() { Logger = Logs.LogProvider.CreateLogger("Console") }.CreateConfiguration(new[] { "--datadir", _Directory, "--conf", confPath });
_Host = new WebHostBuilder() _Host = new WebHostBuilder()
.UseConfiguration(conf) .UseConfiguration(conf)

View File

@@ -13,6 +13,13 @@ using NBXplorer;
namespace BTCPayServer.Configuration namespace BTCPayServer.Configuration
{ {
public class NBXplorerConnectionSetting
{
public string CryptoCode { get; internal set; }
public Uri ExplorerUri { get; internal set; }
public string CookieFile { get; internal set; }
}
public class BTCPayServerOptions public class BTCPayServerOptions
{ {
public ChainType ChainType public ChainType ChainType
@@ -35,6 +42,12 @@ namespace BTCPayServer.Configuration
set; set;
} }
public List<NBXplorerConnectionSetting> NBXplorerConnectionSettings
{
get;
set;
} = new List<NBXplorerConnectionSetting>();
public void LoadArgs(IConfiguration conf) public void LoadArgs(IConfiguration conf)
{ {
ChainType = DefaultConfiguration.GetChainType(conf); ChainType = DefaultConfiguration.GetChainType(conf);
@@ -51,14 +64,11 @@ namespace BTCPayServer.Configuration
if (supportedChains.Contains(net.CryptoCode)) if (supportedChains.Contains(net.CryptoCode))
{ {
validChains.Add(net.CryptoCode); validChains.Add(net.CryptoCode);
var explorer = conf.GetOrDefault<Uri>($"{net.CryptoCode}.explorer.url", net.NBXplorerNetwork.DefaultSettings.DefaultUrl); NBXplorerConnectionSetting setting = new NBXplorerConnectionSetting();
var cookieFile = conf.GetOrDefault<string>($"{net.CryptoCode}.explorer.cookiefile", net.NBXplorerNetwork.DefaultSettings.DefaultCookieFile); setting.CryptoCode = net.CryptoCode;
if (cookieFile.Trim() == "0" || string.IsNullOrEmpty(cookieFile.Trim())) setting.ExplorerUri = conf.GetOrDefault<Uri>($"{net.CryptoCode}.explorer.url", net.NBXplorerNetwork.DefaultSettings.DefaultUrl);
cookieFile = null; setting.CookieFile = conf.GetOrDefault<string>($"{net.CryptoCode}.explorer.cookiefile", net.NBXplorerNetwork.DefaultSettings.DefaultCookieFile);
if (explorer != null) NBXplorerConnectionSettings.Add(setting);
{
ExplorerFactories.Add(net.CryptoCode, (n) => CreateExplorerClient(n, explorer, cookieFile));
}
} }
} }
var invalidChains = String.Join(',', supportedChains.Where(s => !validChains.Contains(s)).ToArray()); var invalidChains = String.Join(',', supportedChains.Where(s => !validChains.Contains(s)).ToArray());
@@ -70,15 +80,6 @@ namespace BTCPayServer.Configuration
ExternalUrl = conf.GetOrDefault<Uri>("externalurl", null); ExternalUrl = conf.GetOrDefault<Uri>("externalurl", null);
} }
private static ExplorerClient CreateExplorerClient(BTCPayNetwork n, Uri uri, string cookieFile)
{
var explorer = new ExplorerClient(n.NBXplorerNetwork, uri);
if (cookieFile == null || !explorer.SetCookieAuth(cookieFile))
explorer.SetNoAuth();
return explorer;
}
public Dictionary<string, Func<BTCPayNetwork, ExplorerClient>> ExplorerFactories = new Dictionary<string, Func<BTCPayNetwork, ExplorerClient>>();
public string PostgresConnectionString public string PostgresConnectionString
{ {
get; get;

View File

@@ -1,8 +1,10 @@
using System; using System;
using Microsoft.Extensions.Logging;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Configuration; using BTCPayServer.Configuration;
using BTCPayServer.Logging;
using NBXplorer; using NBXplorer;
namespace BTCPayServer namespace BTCPayServer
@@ -18,18 +20,41 @@ namespace BTCPayServer
{ {
_NetworkProviders = networkProviders; _NetworkProviders = networkProviders;
_Options = options; _Options = options;
foreach (var setting in options.NBXplorerConnectionSettings)
{
var cookieFile = setting.CookieFile;
if (cookieFile.Trim() == "0" || string.IsNullOrEmpty(cookieFile.Trim()))
cookieFile = null;
Logs.Configuration.LogInformation($"{setting.CryptoCode}: Explorer url is {(setting.ExplorerUri.AbsoluteUri ?? "not set")}");
Logs.Configuration.LogInformation($"{setting.CryptoCode}: Cookie file is {(setting.CookieFile ?? "not set")}");
if (setting.ExplorerUri != null)
{
_Clients.TryAdd(setting.CryptoCode, CreateExplorerClient(_NetworkProviders.GetNetwork(setting.CryptoCode), setting.ExplorerUri, setting.CookieFile));
}
}
} }
private static ExplorerClient CreateExplorerClient(BTCPayNetwork n, Uri uri, string cookieFile)
{
var explorer = new ExplorerClient(n.NBXplorerNetwork, uri);
if (cookieFile == null || !explorer.SetCookieAuth(cookieFile))
{
Logs.Configuration.LogWarning($"{n.CryptoCode}: Not using cookie authentication");
explorer.SetNoAuth();
}
return explorer;
}
Dictionary<string, ExplorerClient> _Clients = new Dictionary<string, ExplorerClient>();
public ExplorerClient GetExplorerClient(string cryptoCode) public ExplorerClient GetExplorerClient(string cryptoCode)
{ {
var network = _NetworkProviders.GetNetwork(cryptoCode); var network = _NetworkProviders.GetNetwork(cryptoCode);
if (network == null) if (network == null)
return null; return null;
if (_Options.ExplorerFactories.TryGetValue(network.CryptoCode, out Func<BTCPayNetwork, ExplorerClient> factory)) _Clients.TryGetValue(network.CryptoCode, out ExplorerClient client);
{ return client;
return factory(network);
}
return null;
} }
public ExplorerClient GetExplorerClient(BTCPayNetwork network) public ExplorerClient GetExplorerClient(BTCPayNetwork network)
@@ -44,18 +69,18 @@ namespace BTCPayServer
var network = _NetworkProviders.GetNetwork(cryptoCode); var network = _NetworkProviders.GetNetwork(cryptoCode);
if (network == null) if (network == null)
return null; return null;
if (_Options.ExplorerFactories.ContainsKey(network.CryptoCode)) if (_Clients.ContainsKey(network.CryptoCode))
return network; return network;
return null; return null;
} }
public IEnumerable<(BTCPayNetwork, ExplorerClient)> GetAll() public IEnumerable<(BTCPayNetwork, ExplorerClient)> GetAll()
{ {
foreach(var net in _NetworkProviders.GetAll()) foreach (var net in _NetworkProviders.GetAll())
{ {
if(_Options.ExplorerFactories.TryGetValue(net.CryptoCode, out Func<BTCPayNetwork, ExplorerClient> factory)) if (_Clients.TryGetValue(net.CryptoCode, out ExplorerClient explorer))
{ {
yield return (net, factory(net)); yield return (net, explorer);
} }
} }
} }

View File

@@ -1,27 +1,28 @@
{ {
"iisSettings": { "iisSettings": {
"windowsAuthentication": false, "windowsAuthentication": false,
"anonymousAuthentication": true, "anonymousAuthentication": true,
"iisExpress": { "iisExpress": {
"applicationUrl": "http://localhost:14139/", "applicationUrl": "http://localhost:14139/",
"sslPort": 0 "sslPort": 0
}
},
"profiles": {
"Default": {
"commandName": "Project"
},
"Docker-Regtest": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"BTCPAY_BTCEXPLORERURL": "http://127.0.0.1:32838/",
"BTCPAY_LTCEXPLORERURL": "http://127.0.0.1:32839/",
"BTCPAY_NETWORK": "regtest",
"ASPNETCORE_ENVIRONMENT": "Development",
"BTCPAY_POSTGRES": "User ID=postgres;Host=127.0.0.1;Port=39372;Database=btcpayserver"
},
"applicationUrl": "http://localhost:14142/"
}
} }
} },
"profiles": {
"Default": {
"commandName": "Project",
"commandLineArgs": "--network regtest --port 4392 -btcexplorerurl \"http://127.0.0.1:3838/\""
},
"Docker-Regtest": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"BTCPAY_NETWORK": "regtest",
"BTCPAY_LTCEXPLORERURL": "http://127.0.0.1:32839/",
"BTCPAY_BTCEXPLORERURL": "http://127.0.0.1:32838/",
"ASPNETCORE_ENVIRONMENT": "Development",
"BTCPAY_POSTGRES": "User ID=postgres;Host=127.0.0.1;Port=39372;Database=btcpayserver"
},
"applicationUrl": "http://localhost:14142/"
}
}
}