mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-20 10:40:29 +01:00
111 lines
3.3 KiB
C#
111 lines
3.3 KiB
C#
using BTCPayServer.Authentication;
|
|
using Microsoft.Extensions.Logging;
|
|
using BTCPayServer.Logging;
|
|
using DBreeze;
|
|
using NBitcoin;
|
|
using NBXplorer;
|
|
using NBXplorer.DerivationStrategy;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using BTCPayServer.Data;
|
|
using BTCPayServer.Services.Invoices;
|
|
using BTCPayServer.Services.Wallets;
|
|
|
|
namespace BTCPayServer.Configuration
|
|
{
|
|
public class BTCPayServerRuntime : IDisposable
|
|
{
|
|
public ExplorerClient Explorer
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
public void Configure(BTCPayServerOptions opts)
|
|
{
|
|
ConfigureAsync(opts).GetAwaiter().GetResult();
|
|
}
|
|
public async Task ConfigureAsync(BTCPayServerOptions opts)
|
|
{
|
|
Network = opts.Network;
|
|
Explorer = new ExplorerClient(opts.Network, opts.Explorer);
|
|
|
|
if (!Explorer.SetCookieAuth(opts.CookieFile))
|
|
Explorer.SetNoAuth();
|
|
|
|
CancellationTokenSource cts = new CancellationTokenSource(30000);
|
|
try
|
|
{
|
|
Logs.Configuration.LogInformation("Trying to connect to explorer " + Explorer.Address.AbsoluteUri);
|
|
await Explorer.WaitServerStartedAsync(cts.Token).ConfigureAwait(false);
|
|
Logs.Configuration.LogInformation("Connection successfull");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new ConfigException($"Could not connect to NBXplorer, {ex.Message}");
|
|
}
|
|
|
|
ApplicationDbContextFactory dbContext = null;
|
|
if (opts.PostgresConnectionString == null)
|
|
{
|
|
var connStr = "Data Source=" + Path.Combine(opts.DataDir, "sqllite.db");
|
|
Logs.Configuration.LogInformation($"SQLite DB used ({connStr})");
|
|
dbContext = new ApplicationDbContextFactory(DatabaseType.Sqlite, connStr);
|
|
}
|
|
else
|
|
{
|
|
Logs.Configuration.LogInformation($"Postgres DB used ({opts.PostgresConnectionString})");
|
|
dbContext = new ApplicationDbContextFactory(DatabaseType.Postgres, opts.PostgresConnectionString);
|
|
}
|
|
DBFactory = dbContext;
|
|
|
|
InvoiceRepository = new InvoiceRepository(dbContext, CreateDBPath(opts, "InvoiceDB"), Network);
|
|
_Resources.Add(InvoiceRepository);
|
|
}
|
|
|
|
private static string CreateDBPath(BTCPayServerOptions opts, string name)
|
|
{
|
|
var dbpath = Path.Combine(opts.DataDir, name);
|
|
if (!Directory.Exists(dbpath))
|
|
Directory.CreateDirectory(dbpath);
|
|
return dbpath;
|
|
}
|
|
|
|
List<IDisposable> _Resources = new List<IDisposable>();
|
|
|
|
public void Dispose()
|
|
{
|
|
lock (_Resources)
|
|
{
|
|
foreach (var r in _Resources)
|
|
{
|
|
r.Dispose();
|
|
}
|
|
_Resources.Clear();
|
|
}
|
|
}
|
|
|
|
public Network Network
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
public InvoiceRepository InvoiceRepository
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
public ApplicationDbContextFactory DBFactory
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
}
|
|
|
|
}
|