2017-08-29 12:03:20 +02:00
|
|
|
|
using Microsoft.AspNetCore;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-09-13 14:53:06 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
2017-10-11 16:26:44 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2017-09-13 14:53:06 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-08-29 12:03:20 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-09-13 14:53:06 +02:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2018-12-13 12:13:59 +01:00
|
|
|
|
using Serilog;
|
2018-07-31 09:38:00 +02:00
|
|
|
|
using System;
|
2017-02-23 09:34:00 -08:00
|
|
|
|
using System.IO;
|
2019-01-31 10:43:32 +00:00
|
|
|
|
|
2016-09-07 13:52:26 -07:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2019-02-06 13:03:54 +00:00
|
|
|
|
public static readonly string AppName = typeof(Program).Namespace;
|
|
|
|
|
public static readonly string ShortAppName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1);
|
2019-01-31 10:43:32 +00:00
|
|
|
|
|
|
|
|
|
public static int Main(string[] args)
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
2019-01-31 10:43:32 +00:00
|
|
|
|
var configuration = GetConfiguration();
|
|
|
|
|
|
|
|
|
|
Log.Logger = CreateSerilogLogger(configuration);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-02-06 13:03:54 +00:00
|
|
|
|
Log.Information("Configuring web host ({Application})...", AppName);
|
2019-01-31 10:43:32 +00:00
|
|
|
|
var host = BuildWebHost(configuration, args);
|
|
|
|
|
|
2019-02-06 13:03:54 +00:00
|
|
|
|
Log.Information("Applying migrations ({Application})...", AppName);
|
2019-01-31 10:43:32 +00:00
|
|
|
|
host.MigrateDbContext<CatalogContext>((context, services) =>
|
2017-09-13 14:53:06 +02:00
|
|
|
|
{
|
|
|
|
|
var env = services.GetService<IHostingEnvironment>();
|
|
|
|
|
var settings = services.GetService<IOptions<CatalogSettings>>();
|
|
|
|
|
var logger = services.GetService<ILogger<CatalogContextSeed>>();
|
|
|
|
|
|
|
|
|
|
new CatalogContextSeed()
|
2019-02-06 13:03:54 +00:00
|
|
|
|
.SeedAsync(context, env, settings, logger)
|
|
|
|
|
.Wait();
|
2017-09-13 14:53:06 +02:00
|
|
|
|
})
|
2019-01-31 10:43:32 +00:00
|
|
|
|
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
|
|
|
|
|
|
2019-02-06 13:03:54 +00:00
|
|
|
|
Log.Information("Starting web host ({Application})...", AppName);
|
2019-01-31 10:43:32 +00:00
|
|
|
|
host.Run();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-02-06 13:03:54 +00:00
|
|
|
|
Log.Fatal(ex, "Program terminated unexpectedly ({Application})!", AppName);
|
2019-01-31 10:43:32 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
}
|
2017-08-29 12:03:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 10:43:32 +00:00
|
|
|
|
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
|
2017-08-29 12:03:20 +02:00
|
|
|
|
WebHost.CreateDefaultBuilder(args)
|
2019-01-31 10:43:32 +00:00
|
|
|
|
.CaptureStartupErrors(false)
|
|
|
|
|
.UseStartup<Startup>()
|
2017-10-11 18:53:26 +02:00
|
|
|
|
.UseApplicationInsights()
|
2017-10-11 16:26:44 +02:00
|
|
|
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
2016-12-29 15:25:36 +01:00
|
|
|
|
.UseWebRoot("Pics")
|
2019-01-31 10:43:32 +00:00
|
|
|
|
.UseConfiguration(configuration)
|
|
|
|
|
.UseSerilog()
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
|
2018-07-31 09:38:00 +02:00
|
|
|
|
|
2019-01-31 10:43:32 +00:00
|
|
|
|
return new LoggerConfiguration()
|
|
|
|
|
.MinimumLevel.Verbose()
|
2019-02-06 13:03:54 +00:00
|
|
|
|
.Enrich.WithProperty("Application", AppName)
|
2019-01-31 10:43:32 +00:00
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.WriteTo.Console()
|
|
|
|
|
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
|
|
|
|
|
.ReadFrom.Configuration(configuration)
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
}
|
2018-07-31 09:38:00 +02:00
|
|
|
|
|
2019-01-31 10:43:32 +00:00
|
|
|
|
private static IConfiguration GetConfiguration()
|
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
|
|
.AddEnvironmentVariables();
|
2018-07-31 09:38:00 +02:00
|
|
|
|
|
2019-01-31 10:43:32 +00:00
|
|
|
|
var config = builder.Build();
|
2018-07-31 09:38:00 +02:00
|
|
|
|
|
2019-01-31 10:43:32 +00:00
|
|
|
|
if (config.GetValue<bool>("UseVault", false))
|
|
|
|
|
{
|
|
|
|
|
builder.AddAzureKeyVault(
|
|
|
|
|
$"https://{config["Vault:Name"]}.vault.azure.net/",
|
|
|
|
|
config["Vault:ClientId"],
|
|
|
|
|
config["Vault:ClientSecret"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder.Build();
|
|
|
|
|
}
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
2017-08-29 12:03:20 +02:00
|
|
|
|
}
|