mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-20 02:28:31 +01:00
cf436e11ae
* Part 1: OpenIddict - Minor Changes & Config prep * add missing nuget * pr changes * pr fixes * remove config for openid -- no need for it for now * remove unused extension * Add tests * use pay tester http client * check redirecturl in tests
75 lines
2.9 KiB
C#
75 lines
2.9 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Primitives;
|
|
using NBitcoin;
|
|
|
|
namespace BTCPayServer.Configuration
|
|
{
|
|
public static class ConfigurationExtensions
|
|
{
|
|
public static T GetOrDefault<T>(this IConfiguration configuration, string key, T defaultValue)
|
|
{
|
|
var str = configuration[key] ?? configuration[key.Replace(".", string.Empty, StringComparison.InvariantCulture)];
|
|
if (str == null)
|
|
return defaultValue;
|
|
if (typeof(T) == typeof(bool))
|
|
{
|
|
var trueValues = new[] { "1", "true" };
|
|
var falseValues = new[] { "0", "false" };
|
|
if (trueValues.Contains(str, StringComparer.OrdinalIgnoreCase))
|
|
return (T)(object)true;
|
|
if (falseValues.Contains(str, StringComparer.OrdinalIgnoreCase))
|
|
return (T)(object)false;
|
|
throw new FormatException();
|
|
}
|
|
else if (typeof(T) == typeof(Uri))
|
|
if (string.IsNullOrEmpty(str))
|
|
{
|
|
return defaultValue;
|
|
}
|
|
else
|
|
{
|
|
return (T)(object)new Uri(str, UriKind.Absolute);
|
|
}
|
|
else if (typeof(T) == typeof(string))
|
|
return (T)(object)str;
|
|
else if (typeof(T) == typeof(IPAddress))
|
|
return (T)(object)IPAddress.Parse(str);
|
|
else if (typeof(T) == typeof(IPEndPoint))
|
|
{
|
|
var separator = str.LastIndexOf(":", StringComparison.InvariantCulture);
|
|
if (separator == -1)
|
|
throw new FormatException();
|
|
var ip = str.Substring(0, separator);
|
|
var port = str.Substring(separator + 1);
|
|
return (T)(object)new IPEndPoint(IPAddress.Parse(ip), int.Parse(port, CultureInfo.InvariantCulture));
|
|
}
|
|
else if (typeof(T) == typeof(int))
|
|
{
|
|
return (T)(object)int.Parse(str, CultureInfo.InvariantCulture);
|
|
}
|
|
else
|
|
{
|
|
throw new NotSupportedException("Configuration value does not support time " + typeof(T).Name);
|
|
}
|
|
}
|
|
|
|
public static string GetDataDir(this IConfiguration configuration)
|
|
{
|
|
var networkType = DefaultConfiguration.GetNetworkType(configuration);
|
|
return GetDataDir(configuration, networkType);
|
|
}
|
|
|
|
public static string GetDataDir(this IConfiguration configuration, NetworkType networkType)
|
|
{
|
|
var defaultSettings = BTCPayDefaultSettings.GetDefaultSettings(networkType);
|
|
return configuration.GetOrDefault("datadir", defaultSettings.DefaultDataDirectory);
|
|
}
|
|
}
|
|
}
|