btcpayserver/BTCPayServer/Controllers/GreenField/ServerInfoController.cs

80 lines
3.2 KiB
C#
Raw Normal View History

2020-05-16 23:57:49 +02:00
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
2020-05-16 23:57:49 +02:00
using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.HostedServices;
using BTCPayServer.Security;
using BTCPayServer.Services;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization;
2020-06-30 08:26:19 +02:00
using Microsoft.AspNetCore.Cors;
2020-05-16 23:57:49 +02:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
2020-05-19 20:08:37 +02:00
using NBXplorer.Models;
2020-05-16 23:57:49 +02:00
namespace BTCPayServer.Controllers.GreenField
{
[ApiController]
2020-06-30 08:26:19 +02:00
[EnableCors(CorsPolicies.All)]
2020-05-16 23:57:49 +02:00
public class GreenFieldServerInfoController : Controller
{
private readonly BTCPayServerEnvironment _env;
private readonly NBXplorerDashboard _dashBoard;
private readonly StoreRepository _storeRepository;
private readonly UserManager<ApplicationUser> _userManager;
private readonly BTCPayNetworkProvider _networkProvider;
private readonly PaymentMethodHandlerDictionary _paymentMethodHandlerDictionary;
public GreenFieldServerInfoController(
2020-06-28 10:55:27 +02:00
BTCPayServerEnvironment env,
NBXplorerDashboard dashBoard,
StoreRepository storeRepository,
2020-05-16 23:57:49 +02:00
UserManager<ApplicationUser> userManager,
BTCPayNetworkProvider networkProvider,
PaymentMethodHandlerDictionary paymentMethodHandlerDictionary)
{
_env = env;
_dashBoard = dashBoard;
_storeRepository = storeRepository;
_userManager = userManager;
_networkProvider = networkProvider;
_paymentMethodHandlerDictionary = paymentMethodHandlerDictionary;
}
2020-06-28 10:55:27 +02:00
2020-05-16 23:57:49 +02:00
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/server/info")]
public async Task<ActionResult> ServerInfo()
{
var stores = await _storeRepository.GetStoresByUserId(_userManager.GetUserId(User));
var supportedPaymentMethods = _paymentMethodHandlerDictionary
.SelectMany(handler => handler.GetSupportedPaymentMethods().Select(id => id.ToString()))
.Distinct();
var syncStatus = _dashBoard.GetAll()
2020-05-18 16:51:21 +02:00
.Where(summary => summary.Network.ShowSyncSummary)
2020-05-16 23:57:49 +02:00
.Select(summary => new ServerInfoSyncStatusData
{
CryptoCode = summary.Network.CryptoCode,
2020-05-19 20:08:37 +02:00
NodeInformation = summary.Status.BitcoinStatus is BitcoinStatus s ? new ServerInfoNodeData()
{
Headers = s.Headers,
Blocks = s.Blocks,
VerificationProgress = s.VerificationProgress
2020-06-28 10:55:27 +02:00
} : null,
2020-05-19 20:08:37 +02:00
ChainHeight = summary.Status.ChainHeight,
SyncHeight = summary.Status.SyncHeight
2020-05-16 23:57:49 +02:00
});
ServerInfoData model = new ServerInfoData
{
2020-05-19 20:08:37 +02:00
FullySynched = _dashBoard.IsFullySynched(),
SyncStatus = syncStatus,
2020-05-16 23:57:49 +02:00
Onion = _env.OnionUrl,
Version = _env.Version,
SupportedPaymentMethods = supportedPaymentMethods
};
return Ok(model);
}
}
}