2017-07-13 17:35:19 +02:00
|
|
|
|
using Basket.API.Infrastructure.Middlewares;
|
2017-08-29 10:20:13 +02:00
|
|
|
|
using Microsoft.AspNetCore;
|
2017-07-13 17:35:19 +02:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-10-11 16:26:44 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2017-08-29 10:20:13 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
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;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
|
2016-09-07 13:52:26 -07:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Basket.API
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2019-02-26 21:55:04 +00:00
|
|
|
|
public static readonly string Namespace = typeof(Program).Namespace;
|
|
|
|
|
public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
|
|
|
|
public static int Main(string[] args)
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
2019-01-31 13:33:36 +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 13:33:36 +00:00
|
|
|
|
var host = BuildWebHost(configuration, args);
|
|
|
|
|
|
2019-02-06 13:03:54 +00:00
|
|
|
|
Log.Information("Starting web host ({Application})...", AppName);
|
2019-01-31 13:33:36 +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 13:33:36 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
}
|
2017-08-29 10:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 13:33:36 +00:00
|
|
|
|
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
|
2017-08-29 10:20:13 +02:00
|
|
|
|
WebHost.CreateDefaultBuilder(args)
|
2019-01-31 13:33:36 +00:00
|
|
|
|
.CaptureStartupErrors(false)
|
2017-07-13 17:35:19 +02:00
|
|
|
|
.UseFailing(options =>
|
2019-01-31 13:33:36 +00:00
|
|
|
|
options.ConfigPath = "/Failing")
|
2016-09-06 17:09:19 -07:00
|
|
|
|
.UseStartup<Startup>()
|
2017-10-11 18:53:26 +02:00
|
|
|
|
.UseApplicationInsights()
|
2019-01-31 13:33:36 +00:00
|
|
|
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
|
|
|
.UseConfiguration(configuration)
|
|
|
|
|
.UseSerilog()
|
2016-09-06 17:09:19 -07:00
|
|
|
|
.Build();
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
|
|
|
|
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
|
|
|
|
|
|
|
|
|
|
return new LoggerConfiguration()
|
|
|
|
|
.MinimumLevel.Verbose()
|
2019-02-26 21:55:04 +00:00
|
|
|
|
.Enrich.WithProperty("ApplicationContext", AppName)
|
2019-01-31 13:33:36 +00:00
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.WriteTo.Console()
|
|
|
|
|
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
|
|
|
|
|
.ReadFrom.Configuration(configuration)
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IConfiguration GetConfiguration()
|
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
|
|
.AddEnvironmentVariables();
|
|
|
|
|
|
|
|
|
|
var config = builder.Build();
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|