2021-04-01 05:27:22 +02:00
|
|
|
using System;
|
2022-01-01 14:05:24 +01:00
|
|
|
using System.Linq;
|
2017-09-13 08:47:34 +02:00
|
|
|
using System.IO;
|
|
|
|
using System.Net;
|
2020-06-28 10:55:27 +02:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
using BTCPayServer.Hosting;
|
|
|
|
using BTCPayServer.Logging;
|
2021-04-01 05:27:22 +02:00
|
|
|
using BTCPayServer.Plugins;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
2021-04-01 05:27:22 +02:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2022-01-12 12:12:10 +01:00
|
|
|
using System.Reflection;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
2020-06-28 10:55:27 +02:00
|
|
|
[assembly: InternalsVisibleTo("BTCPayServer.Tests")]
|
2017-09-13 08:47:34 +02:00
|
|
|
namespace BTCPayServer
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
class Program
|
|
|
|
{
|
|
|
|
static void Main(string[] args)
|
|
|
|
{
|
2022-01-01 14:05:24 +01:00
|
|
|
if (args.Length > 0 && args[0] == "run")
|
|
|
|
args = args.Skip(1).ToArray(); // Hack to make dotnet watch work
|
2022-01-12 12:12:10 +01:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
ServicePointManager.DefaultConnectionLimit = 100;
|
|
|
|
IWebHost host = null;
|
2018-01-15 06:42:51 +01:00
|
|
|
var processor = new ConsoleLoggerProcessor();
|
|
|
|
CustomConsoleLogProvider loggerProvider = new CustomConsoleLogProvider(processor);
|
2020-01-12 07:32:26 +01:00
|
|
|
using var loggerFactory = new LoggerFactory();
|
2017-10-27 10:53:04 +02:00
|
|
|
loggerFactory.AddProvider(loggerProvider);
|
|
|
|
var logger = loggerFactory.CreateLogger("Configuration");
|
2021-11-22 09:16:08 +01:00
|
|
|
Logs logs = new Logs();
|
2022-02-06 06:37:19 +01:00
|
|
|
logs.Configure(loggerFactory);
|
2021-04-01 05:27:22 +02:00
|
|
|
IConfiguration conf = null;
|
2017-10-27 10:53:04 +02:00
|
|
|
try
|
|
|
|
{
|
2021-04-01 05:27:22 +02:00
|
|
|
conf = new DefaultConfiguration() { Logger = logger }.CreateConfiguration(args);
|
2017-10-27 10:53:04 +02:00
|
|
|
if (conf == null)
|
|
|
|
return;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
2022-01-12 12:12:10 +01:00
|
|
|
var builder = new WebHostBuilder()
|
2017-10-27 10:53:04 +02:00
|
|
|
.UseKestrel()
|
|
|
|
.UseConfiguration(conf)
|
|
|
|
.ConfigureLogging(l =>
|
|
|
|
{
|
|
|
|
l.AddFilter("Microsoft", LogLevel.Error);
|
2021-11-04 16:13:40 +01:00
|
|
|
if (!conf.GetOrDefault<bool>("verbose", false))
|
|
|
|
l.AddFilter("Events", LogLevel.Warning);
|
2019-03-07 11:29:32 +01:00
|
|
|
l.AddFilter("System.Net.Http.HttpClient", LogLevel.Critical);
|
2018-01-18 10:12:01 +01:00
|
|
|
l.AddFilter("Microsoft.AspNetCore.Antiforgery.Internal", LogLevel.Critical);
|
2021-04-20 07:06:32 +02:00
|
|
|
l.AddFilter("Fido2NetLib.DistributedCacheMetadataService", LogLevel.Error);
|
2018-01-15 06:42:51 +01:00
|
|
|
l.AddProvider(new CustomConsoleLogProvider(processor));
|
2017-10-27 10:53:04 +02:00
|
|
|
})
|
2022-01-12 12:12:10 +01:00
|
|
|
.UseStartup<Startup>();
|
|
|
|
|
|
|
|
// When we run the app with dotnet run (typically in dev env), the wwwroot isn't in the same directory
|
|
|
|
// than this assembly.
|
|
|
|
// But when we use dotnet publish, the wwwroot is published alongside the assembly!
|
|
|
|
// This fix https://github.com/btcpayserver/btcpayserver/issues/1894
|
|
|
|
var defaultContentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
|
|
var defaultWebRoot = Path.Combine(defaultContentPath, "wwwroot");
|
|
|
|
var defaultWebRootExists = Directory.Exists(defaultWebRoot);
|
|
|
|
if (!defaultWebRootExists)
|
|
|
|
{
|
|
|
|
// When we use dotnet run...
|
|
|
|
builder.UseContentRoot(Directory.GetCurrentDirectory());
|
|
|
|
}
|
|
|
|
host = builder.Build();
|
2019-07-08 05:12:39 +02:00
|
|
|
host.StartWithTasksAsync().GetAwaiter().GetResult();
|
2017-10-27 10:53:04 +02:00
|
|
|
var urls = host.ServerFeatures.Get<IServerAddressesFeature>().Addresses;
|
|
|
|
foreach (var url in urls)
|
|
|
|
{
|
2022-01-01 14:05:24 +01:00
|
|
|
// Some tools such as dotnet watch parse this exact log to open the browser
|
|
|
|
logger.LogInformation("Now listening on: " + url);
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
host.WaitForShutdown();
|
|
|
|
}
|
|
|
|
catch (ConfigException ex)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(ex.Message))
|
2021-11-22 09:16:08 +01:00
|
|
|
logs.Configuration.LogError(ex.Message);
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
catch (Exception e) when (PluginManager.IsExceptionByPlugin(e))
|
2021-04-01 05:27:22 +02:00
|
|
|
{
|
|
|
|
var pluginDir = new DataDirectories().Configure(conf).PluginDir;
|
|
|
|
PluginManager.DisablePlugin(pluginDir, e.Source);
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
finally
|
|
|
|
{
|
2018-01-15 06:42:51 +01:00
|
|
|
processor.Dispose();
|
2020-06-28 10:55:27 +02:00
|
|
|
if (host == null)
|
2021-11-22 09:16:08 +01:00
|
|
|
logs.Configuration.LogError("Configuration error");
|
2017-10-27 10:53:04 +02:00
|
|
|
if (host != null)
|
|
|
|
host.Dispose();
|
2018-10-15 17:37:42 +02:00
|
|
|
Serilog.Log.CloseAndFlush();
|
2017-10-27 10:53:04 +02:00
|
|
|
loggerProvider.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
}
|