remove appsettings
This commit is contained in:
@ -2,50 +2,21 @@
|
||||
using Barcode.Host.Server.Hubs;
|
||||
using Barcode.Host.Server.Models;
|
||||
using Barcode.Host.Server.Services;
|
||||
using Barcode.Host.Shared.Models;
|
||||
using Barcode.Host.Shared.Models.Stateless;
|
||||
using Serilog;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Barcode.Host.Server;
|
||||
|
||||
public class Program
|
||||
{
|
||||
|
||||
private static (string, WebApplicationOptions) Get(string[] args)
|
||||
{
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
string? assemblyName = assembly.GetName()?.Name;
|
||||
if (string.IsNullOrEmpty(assemblyName))
|
||||
throw new Exception();
|
||||
string baseAssemblyName = assemblyName.Split('.')[0];
|
||||
string webRootPath = Path.Combine(AppContext.BaseDirectory.Split(baseAssemblyName)[0], baseAssemblyName, "wwwroot");
|
||||
if (!Directory.Exists(webRootPath))
|
||||
webRootPath = string.Empty;
|
||||
WebApplicationOptions webApplicationOptions = new()
|
||||
{
|
||||
Args = args,
|
||||
ContentRootPath = AppContext.BaseDirectory,
|
||||
WebRootPath = webRootPath
|
||||
};
|
||||
return new(assemblyName, webApplicationOptions);
|
||||
}
|
||||
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
LoggerConfiguration loggerConfiguration = new();
|
||||
(string assemblyName, _) = Get(args);
|
||||
ILogger<Program>? logger = null;
|
||||
WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder(args);
|
||||
_ = webApplicationBuilder.Configuration.AddUserSecrets<Program>();
|
||||
AppSettings appSettings = Models.Binder.AppSettings.Get(webApplicationBuilder.Configuration);
|
||||
if (string.IsNullOrEmpty(appSettings.WorkingDirectoryName))
|
||||
throw new Exception("Working directory name must have a value!");
|
||||
string workingDirectory = IWorkingDirectory.GetWorkingDirectory(assemblyName, appSettings.WorkingDirectoryName);
|
||||
Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory);
|
||||
_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, webApplicationBuilder.Configuration);
|
||||
_ = SerilogHostBuilderExtensions.UseSerilog(webApplicationBuilder.Host);
|
||||
Log.Logger = loggerConfiguration.CreateLogger();
|
||||
Serilog.ILogger log = Log.ForContext<Program>();
|
||||
if (string.IsNullOrEmpty(appSettings.Company))
|
||||
throw new Exception("Company name must have a value!");
|
||||
try
|
||||
{
|
||||
if (appSettings.IsStaging && appSettings.IsDevelopment)
|
||||
@ -68,6 +39,7 @@ public class Program
|
||||
_ = webApplicationBuilder.Services.AddHostedService<TimedHostedService>();
|
||||
_ = webApplicationBuilder.Services.AddSwaggerGen();
|
||||
WebApplication webApplication = webApplicationBuilder.Build();
|
||||
logger = webApplication.Services.GetRequiredService<ILogger<Program>>();
|
||||
if (appSettings.IsDevelopment)
|
||||
{
|
||||
_ = webApplication.UseSwagger();
|
||||
@ -79,25 +51,23 @@ public class Program
|
||||
_ = webApplication.UseExceptionHandler("/Error");
|
||||
_ = webApplication.UseHsts();
|
||||
}
|
||||
_ = webApplication.Lifetime.ApplicationStopped.Register(Log.CloseAndFlush);
|
||||
_ = webApplication.UseStaticFiles();
|
||||
_ = webApplication.UseRouting();
|
||||
_ = webApplication.UseAuthorization();
|
||||
_ = webApplication.MapControllers();
|
||||
_ = webApplication.MapRazorPages();
|
||||
_ = webApplication.MapHub<NotificationHub>($"/{nameof(NotificationHub)}");
|
||||
log.Information("Starting Web Application");
|
||||
logger.LogInformation("Starting Web Application");
|
||||
logger.LogCritical("{Company}", appSettings.Company);
|
||||
webApplication.Run();
|
||||
return 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.Fatal(ex, "Host terminated unexpectedly");
|
||||
return 1;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
try
|
||||
{ logger?.LogCritical(ex, "Host terminated unexpectedly"); }
|
||||
catch (Exception) { }
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user