94 lines
3.3 KiB
C#
94 lines
3.3 KiB
C#
using Microsoft.AspNetCore;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.CommandLineUtils;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Serilog;
|
|
using Strata.Configuration.Client.DependencyInjection;
|
|
using Strata.healthchecks.Biz;
|
|
using Strata.Logging.DependencyInjection;
|
|
using Strata.Logging.Serilog.Formatting;
|
|
using Strata.RxNorm.Biz.DbContexts;
|
|
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace Strata.RxNorm.Api
|
|
{
|
|
[ExcludeFromCodeCoverage]
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
try
|
|
{
|
|
var commandLineApplication = new CommandLineApplication(false);
|
|
var doMigrate = commandLineApplication.Option(
|
|
"--ef-migrate",
|
|
"Apply entity framework migrations and exit",
|
|
CommandOptionType.NoValue);
|
|
|
|
commandLineApplication.HelpOption("-? | -h | --help");
|
|
commandLineApplication.OnExecute(() =>
|
|
{
|
|
ExecuteApp(args, doMigrate);
|
|
return 0;
|
|
});
|
|
commandLineApplication.Execute(args);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Logger = new LoggerConfiguration()
|
|
.WriteTo.Console(new LogStashFormatter())
|
|
.CreateLogger();
|
|
Log.Fatal(ex, "There was an unhandled exception that caused the application to crash");
|
|
|
|
//This is for serilog to ensure that all of the cached logs are flushed when the app crashes
|
|
//This is useful so we can capture logs when the application crashes during startup:
|
|
Log.CloseAndFlush();
|
|
Environment.Exit(1);
|
|
}
|
|
}
|
|
|
|
public static void ExecuteApp(string[] args, CommandOption doMigrate)
|
|
{
|
|
var webHost = CreateWebHostBuilder(args).Build();
|
|
|
|
if (doMigrate.HasValue())
|
|
{
|
|
Log.Debug("Applying Entity Framework migrations");
|
|
using var scope = webHost.Services.CreateScope();
|
|
using var context = scope.ServiceProvider.GetService<RxNormDbContext>();
|
|
|
|
try
|
|
{
|
|
context.Database.SetCommandTimeout(TimeSpan.FromMinutes(10));
|
|
context.Database.Migrate();
|
|
Log.Debug("All done, closing app");
|
|
Log.CloseAndFlush();
|
|
Environment.Exit(Environment.ExitCode);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "There was an unhandled exception that caused the program to crash");
|
|
Log.CloseAndFlush();
|
|
Environment.Exit(1);
|
|
}
|
|
}
|
|
|
|
// no flags provided, so just run the webhost
|
|
webHost.Run();
|
|
}
|
|
|
|
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
|
WebHost.CreateDefaultBuilder(args)
|
|
.UseStrataConfiguration()
|
|
.UseStrataLogging()
|
|
.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, "Strata.healthchecks.Jazz")
|
|
.UseStrataHealthChecks()
|
|
.UseStartup<Startup>();
|
|
}
|
|
}
|
|
|