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;
|
2020-12-15 14:42:56 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Basket.API;
|
2017-10-11 16:26:44 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2021-06-08 17:15:15 +08:00
|
|
|
|
using Azure.Identity;
|
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;
|
2021-06-08 17:15:15 +08:00
|
|
|
|
using Azure.Core;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
var configuration = GetConfiguration();
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
Log.Logger = CreateSerilogLogger(configuration);
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.Information("Configuring web host ({ApplicationContext})...", Program.AppName);
|
|
|
|
|
var host = BuildWebHost(configuration, args);
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
Log.Information("Starting web host ({ApplicationContext})...", Program.AppName);
|
|
|
|
|
host.Run();
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
}
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
|
|
|
|
|
WebHost.CreateDefaultBuilder(args)
|
|
|
|
|
.CaptureStartupErrors(false)
|
|
|
|
|
.ConfigureKestrel(options =>
|
|
|
|
|
{
|
|
|
|
|
var ports = GetDefinedPorts(configuration);
|
|
|
|
|
options.Listen(IPAddress.Any, ports.httpPort, listenOptions =>
|
2019-01-31 13:33:36 +00:00
|
|
|
|
{
|
2020-12-15 14:42:56 +01:00
|
|
|
|
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
options.Listen(IPAddress.Any, ports.grpcPort, listenOptions =>
|
2019-01-31 13:33:36 +00:00
|
|
|
|
{
|
2020-12-15 14:42:56 +01:00
|
|
|
|
listenOptions.Protocols = HttpProtocols.Http2;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration))
|
|
|
|
|
.UseFailing(options =>
|
2019-01-31 13:33:36 +00:00
|
|
|
|
{
|
2020-12-15 14:42:56 +01:00
|
|
|
|
options.ConfigPath = "/Failing";
|
|
|
|
|
options.NotFilteredPaths.AddRange(new[] { "/hc", "/liveness" });
|
|
|
|
|
})
|
|
|
|
|
.UseStartup<Startup>()
|
|
|
|
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
|
|
|
.UseSerilog()
|
|
|
|
|
.Build();
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
ILogger CreateSerilogLogger(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
|
|
|
|
|
var logstashUrl = configuration["Serilog:LogstashgUrl"];
|
|
|
|
|
return new LoggerConfiguration()
|
|
|
|
|
.MinimumLevel.Verbose()
|
|
|
|
|
.Enrich.WithProperty("ApplicationContext", Program.AppName)
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.WriteTo.Console()
|
|
|
|
|
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
|
|
|
|
|
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl)
|
|
|
|
|
.ReadFrom.Configuration(configuration)
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
}
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
IConfiguration GetConfiguration()
|
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
|
|
.AddEnvironmentVariables();
|
2019-01-31 13:33:36 +00:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
var config = builder.Build();
|
2019-08-19 09:24:58 +02:00
|
|
|
|
|
2020-12-15 14:42:56 +01:00
|
|
|
|
if (config.GetValue<bool>("UseVault", false))
|
|
|
|
|
{
|
2021-06-08 17:15:15 +08:00
|
|
|
|
TokenCredential credential = new ClientSecretCredential(
|
|
|
|
|
config["Vault:TenantId"],
|
2020-12-15 14:42:56 +01:00
|
|
|
|
config["Vault:ClientId"],
|
|
|
|
|
config["Vault:ClientSecret"]);
|
2021-06-08 17:15:15 +08:00
|
|
|
|
builder.AddAzureKeyVault(new Uri($"https://{config["Vault:Name"]}.vault.azure.net/"), credential);
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
2020-12-15 14:42:56 +01:00
|
|
|
|
|
|
|
|
|
return builder.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(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
|
|
|
|
}
|
2020-12-15 14:42:56 +01:00
|
|
|
|
|
2021-01-20 17:57:32 +01:00
|
|
|
|
public class Program
|
|
|
|
|
{
|
2020-12-15 14:42:56 +01:00
|
|
|
|
|
2020-12-16 09:53:08 +01:00
|
|
|
|
public static string Namespace = typeof(Startup).Namespace;
|
2020-12-15 14:42:56 +01:00
|
|
|
|
public static string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
|
2021-06-08 17:15:15 +08:00
|
|
|
|
}
|