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;
|
2019-08-19 09:24:58 +02:00
|
|
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
2017-10-11 16:26:44 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
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-08-19 09:24:58 +02:00
|
|
|
|
using System.Net;
|
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-26 21:59:16 +00:00
|
|
|
|
Log.Information("Configuring web host ({ApplicationContext})...", AppName);
|
2019-01-31 13:33:36 +00:00
|
|
|
|
var host = BuildWebHost(configuration, args);
|
|
|
|
|
|
2019-02-26 21:59:16 +00:00
|
|
|
|
Log.Information("Starting web host ({ApplicationContext})...", AppName);
|
2019-01-31 13:33:36 +00:00
|
|
|
|
host.Run();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-02-26 21:59:16 +00:00
|
|
|
|
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", 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)
|
2019-08-19 09:24:58 +02:00
|
|
|
|
.ConfigureKestrel(options =>
|
|
|
|
|
{
|
|
|
|
|
var ports = GetDefinedPorts(configuration);
|
|
|
|
|
options.Listen(IPAddress.Any, ports.httpPort, listenOptions =>
|
|
|
|
|
{
|
|
|
|
|
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
options.Listen(IPAddress.Any, ports.grpcPort, listenOptions =>
|
|
|
|
|
{
|
|
|
|
|
listenOptions.Protocols = HttpProtocols.Http2;
|
|
|
|
|
});
|
2019-08-26 13:57:43 +02:00
|
|
|
|
|
2019-08-19 09:24:58 +02:00
|
|
|
|
})
|
2020-04-07 08:59:01 +02:00
|
|
|
|
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration))
|
2019-10-15 11:23:25 +02:00
|
|
|
|
.UseFailing(options => {
|
|
|
|
|
options.ConfigPath = "/Failing";
|
|
|
|
|
options.NotFilteredPaths.AddRange(new[] {"/hc","/liveness"});
|
|
|
|
|
})
|
2016-09-06 17:09:19 -07:00
|
|
|
|
.UseStartup<Startup>()
|
2019-01-31 13:33:36 +00:00
|
|
|
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
|
|
|
.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"];
|
2019-03-19 15:57:47 +01:00
|
|
|
|
var logstashUrl = configuration["Serilog:LogstashgUrl"];
|
2019-01-31 13:33:36 +00:00
|
|
|
|
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)
|
2019-03-19 15:57:47 +01:00
|
|
|
|
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl)
|
2019-01-31 13:33:36 +00:00
|
|
|
|
.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();
|
|
|
|
|
}
|
2019-08-19 09:24:58 +02:00
|
|
|
|
|
|
|
|
|
private static (int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
var grpcPort = config.GetValue("GRPC_PORT", 5001);
|
|
|
|
|
var port = config.GetValue("PORT", 80);
|
|
|
|
|
return (port, grpcPort);
|
|
|
|
|
}
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
}
|