mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-20 10:40:29 +01:00
190 lines
7.8 KiB
C#
190 lines
7.8 KiB
C#
using BTCPayServer.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using Microsoft.AspNetCore.Http;
|
|
using NBitpayClient;
|
|
using NBitcoin;
|
|
using BTCPayServer.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.IO;
|
|
using Microsoft.Data.Sqlite;
|
|
using NBXplorer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.Hosting;
|
|
using BTCPayServer.Services;
|
|
using BTCPayServer.Services.Invoices;
|
|
using BTCPayServer.Services.Rates;
|
|
using BTCPayServer.Services.Stores;
|
|
using BTCPayServer.Services.Fees;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Rewrite;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using BTCPayServer.Controllers;
|
|
using BTCPayServer.Services.Mails;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using BTCPayServer.Models;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
using BTCPayServer.Services.Wallets;
|
|
using BTCPayServer.Authentication;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using BTCPayServer.Logging;
|
|
using BTCPayServer.HostedServices;
|
|
using Meziantou.AspNetCore.BundleTagHelpers;
|
|
using System.Security.Claims;
|
|
using BTCPayServer.Security;
|
|
|
|
namespace BTCPayServer.Hosting
|
|
{
|
|
public static class BTCPayServerServices
|
|
{
|
|
public static IServiceCollection AddBTCPayServer(this IServiceCollection services)
|
|
{
|
|
services.AddDbContext<ApplicationDbContext>((provider, o) =>
|
|
{
|
|
var factory = provider.GetRequiredService<ApplicationDbContextFactory>();
|
|
factory.ConfigureBuilder(o);
|
|
});
|
|
services.TryAddSingleton<SettingsRepository>();
|
|
services.TryAddSingleton<InvoicePaymentNotification>();
|
|
services.TryAddSingleton<BTCPayServerOptions>(o => o.GetRequiredService<IOptions<BTCPayServerOptions>>().Value);
|
|
services.TryAddSingleton<InvoiceRepository>(o =>
|
|
{
|
|
var opts = o.GetRequiredService<BTCPayServerOptions>();
|
|
var dbContext = o.GetRequiredService<ApplicationDbContextFactory>();
|
|
var dbpath = Path.Combine(opts.DataDir, "InvoiceDB");
|
|
if (!Directory.Exists(dbpath))
|
|
Directory.CreateDirectory(dbpath);
|
|
return new InvoiceRepository(dbContext, dbpath);
|
|
});
|
|
services.AddSingleton<BTCPayServerEnvironment>();
|
|
services.TryAddSingleton<TokenRepository>();
|
|
services.TryAddSingleton<EventAggregator>();
|
|
services.TryAddSingleton<CoinAverageSettings>();
|
|
services.TryAddSingleton<ApplicationDbContextFactory>(o =>
|
|
{
|
|
var opts = o.GetRequiredService<BTCPayServerOptions>();
|
|
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);
|
|
}
|
|
return dbContext;
|
|
});
|
|
services.TryAddSingleton<Payments.Lightning.LightningClientFactory>();
|
|
|
|
services.TryAddSingleton<BTCPayNetworkProvider>(o =>
|
|
{
|
|
var opts = o.GetRequiredService<BTCPayServerOptions>();
|
|
return opts.NetworkProvider;
|
|
});
|
|
|
|
services.TryAddSingleton<LanguageService>();
|
|
services.TryAddSingleton<NBXplorerDashboard>();
|
|
services.TryAddSingleton<StoreRepository>();
|
|
services.TryAddSingleton<BTCPayWalletProvider>();
|
|
services.TryAddSingleton<CurrencyNameTable>();
|
|
services.TryAddSingleton<IFeeProviderFactory>(o => new NBXplorerFeeProviderFactory(o.GetRequiredService<ExplorerClientProvider>())
|
|
{
|
|
Fallback = new FeeRate(100, 1),
|
|
BlockTarget = 20
|
|
});
|
|
|
|
services.AddSingleton<CssThemeManager>();
|
|
services.AddSingleton<IHostedService, CssThemeManagerHostedService>();
|
|
|
|
services.AddSingleton<Payments.IPaymentMethodHandler<DerivationStrategy>, Payments.Bitcoin.BitcoinLikePaymentHandler>();
|
|
services.AddSingleton<IHostedService, Payments.Bitcoin.NBXplorerListener>();
|
|
|
|
services.AddSingleton<Payments.IPaymentMethodHandler<Payments.Lightning.LightningSupportedPaymentMethod>, Payments.Lightning.LightningLikePaymentHandler>();
|
|
services.AddSingleton<IHostedService, Payments.Lightning.LightningListener>();
|
|
|
|
services.AddSingleton<IHostedService, NBXplorerWaiters>();
|
|
services.AddSingleton<IHostedService, InvoiceNotificationManager>();
|
|
services.AddSingleton<IHostedService, InvoiceWatcher>();
|
|
services.AddSingleton<IHostedService, RatesHostedService>();
|
|
services.AddTransient<IConfigureOptions<MvcOptions>, BTCPayClaimsFilter>();
|
|
services.AddTransient<IConfigureOptions<MvcOptions>, BitpayClaimsFilter>();
|
|
|
|
services.TryAddSingleton<ExplorerClientProvider>();
|
|
services.TryAddSingleton<Bitpay>(o =>
|
|
{
|
|
if (o.GetRequiredService<BTCPayServerOptions>().NetworkType == NetworkType.Mainnet)
|
|
return new Bitpay(new Key(), new Uri("https://bitpay.com/"));
|
|
else
|
|
return new Bitpay(new Key(), new Uri("https://test.bitpay.com/"));
|
|
});
|
|
services.TryAddSingleton<BTCPayRateProviderFactory>();
|
|
|
|
services.TryAddScoped<IHttpContextAccessor, HttpContextAccessor>();
|
|
services.AddTransient<AccessTokenController>();
|
|
services.AddTransient<InvoiceController>();
|
|
// Add application services.
|
|
services.AddTransient<IEmailSender, EmailSender>();
|
|
// bundling
|
|
|
|
services.AddAuthorization(o => Policies.AddBTCPayPolicies(o));
|
|
|
|
services.AddBundles();
|
|
services.AddTransient<BundleOptions>(provider =>
|
|
{
|
|
var opts = provider.GetRequiredService<BTCPayServerOptions>();
|
|
var bundle = new BundleOptions();
|
|
bundle.UseMinifiedFiles = opts.BundleJsCss;
|
|
bundle.AppendVersion = true;
|
|
return bundle;
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IApplicationBuilder UsePayServer(this IApplicationBuilder app)
|
|
{
|
|
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
|
|
{
|
|
//Wait the DB is ready
|
|
Retry(() =>
|
|
{
|
|
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
|
|
});
|
|
}
|
|
|
|
app.UseMiddleware<BTCPayMiddleware>();
|
|
return app;
|
|
}
|
|
|
|
static void Retry(Action act)
|
|
{
|
|
CancellationTokenSource cts = new CancellationTokenSource(10000);
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
act();
|
|
return;
|
|
}
|
|
catch when(!cts.IsCancellationRequested)
|
|
{
|
|
Thread.Sleep(100);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|