87 lines
3.3 KiB
C#
87 lines
3.3 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using ProxmoxConnector.Server.Application.Services.Environment;
|
|
using ProxmoxConnector.Server.Application.Utils;
|
|
using Serilog;
|
|
using Serilog.Core;
|
|
using Serilog.Events;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace ProxmoxConnector.Server
|
|
{
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var isContainer = args.Contains("--docker");
|
|
if (!isContainer)
|
|
{
|
|
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
|
|
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
|
|
Directory.SetCurrentDirectory(pathToContentRoot);
|
|
}
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true, reloadOnChange: true)
|
|
.AddEnvironmentVariables()
|
|
.Build();
|
|
|
|
var configurator = new EnvironmentConfigurator(configuration);
|
|
configurator.Configure();
|
|
var (databasePath, workspace) = configurator.GetStorageTools();
|
|
|
|
var loggingLevelParam = configuration.GetValue<string>("Logging:LogLevel:Default");
|
|
if (!Enum.TryParse(loggingLevelParam, out LogEventLevel loggingLevel))
|
|
throw new Exception($"Unknown logging level '{loggingLevelParam}'.");
|
|
|
|
var loggingLevelSwitch = new LoggingLevelSwitch(loggingLevel);
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
.MinimumLevel.ControlledBy(loggingLevelSwitch)
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
|
.Enrich.FromLogContext()
|
|
.WriteTo.Console()
|
|
.WriteTo.SQLite(databasePath, "__Logs", levelSwitch: loggingLevelSwitch)
|
|
.CreateLogger();
|
|
|
|
try
|
|
{
|
|
var urls = configuration.GetValue<string>("urls");
|
|
Log.Information($"Starting Proxmox connector {SystemRegistry.GetVersion()}...");
|
|
Log.Information($"API listening on {urls}");
|
|
Log.Information($"Workspace configured at '{workspace}' path");
|
|
Console.WriteLine("Application started. Press Ctrl+C to shut down.");
|
|
CreateHostBuilder(args, configuration).Build().Run();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "CDN Server host terminated unexpectedly");
|
|
}
|
|
finally
|
|
{
|
|
Log.CloseAndFlush();
|
|
}
|
|
}
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration)
|
|
{
|
|
var builder = Host.CreateDefaultBuilder(args)
|
|
.ConfigureWebHostDefaults(webBuilder =>
|
|
{
|
|
webBuilder.UseStartup<Startup>()
|
|
.UseConfiguration(configuration)
|
|
.UseSerilog();
|
|
});
|
|
|
|
return builder;
|
|
}
|
|
}
|
|
}
|