2017-05-11 13:44:38 +02:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2017-04-28 15:04:38 +02:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-08-29 11:17:09 +02:00
|
|
|
|
using Microsoft.Azure.ServiceBus;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
|
2017-04-28 15:04:38 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-09-06 14:36:22 +03:00
|
|
|
|
using Microsoft.Extensions.HealthChecks;
|
2017-04-28 15:04:38 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-05-17 19:42:45 +02:00
|
|
|
|
using Payment.API.IntegrationEvents.EventHandling;
|
2017-08-29 11:17:09 +02:00
|
|
|
|
using Payment.API.IntegrationEvents.Events;
|
|
|
|
|
using RabbitMQ.Client;
|
|
|
|
|
using System;
|
2017-09-06 14:36:22 +03:00
|
|
|
|
using System.Threading.Tasks;
|
2017-10-02 16:20:59 +02:00
|
|
|
|
using Swashbuckle.AspNetCore.Swagger;
|
2017-04-28 15:04:38 +02:00
|
|
|
|
|
|
|
|
|
namespace Payment.API
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2017-08-29 11:17:09 +02:00
|
|
|
|
public Startup(IConfiguration configuration)
|
2017-04-28 15:04:38 +02:00
|
|
|
|
{
|
2017-08-29 11:17:09 +02:00
|
|
|
|
Configuration = configuration;
|
2017-04-28 15:04:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-29 11:17:09 +02:00
|
|
|
|
public IConfiguration Configuration { get; }
|
2017-04-28 15:04:38 +02:00
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
2017-05-11 13:44:38 +02:00
|
|
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
2017-04-28 15:04:38 +02:00
|
|
|
|
{
|
2017-10-11 18:53:26 +02:00
|
|
|
|
services.AddApplicationInsightsTelemetry(Configuration);
|
2017-08-29 11:17:09 +02:00
|
|
|
|
|
2017-05-17 19:42:45 +02:00
|
|
|
|
services.Configure<PaymentSettings>(Configuration);
|
2017-08-29 11:17:09 +02:00
|
|
|
|
|
2017-10-11 18:53:26 +02:00
|
|
|
|
if (Configuration.GetValue<string>("OrchestratorType").Equals("K8S"))
|
|
|
|
|
{
|
|
|
|
|
// Enable K8s telemetry initializer
|
|
|
|
|
services.EnableKubernetes();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
2017-05-15 19:15:33 +02:00
|
|
|
|
{
|
2017-06-30 08:59:26 +02:00
|
|
|
|
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
|
|
|
|
|
|
|
|
|
|
var serviceBusConnectionString = Configuration["EventBusConnection"];
|
|
|
|
|
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
|
2017-05-15 19:15:33 +02:00
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
2017-05-15 19:15:33 +02:00
|
|
|
|
{
|
2017-06-30 08:59:26 +02:00
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
|
|
|
|
var factory = new ConnectionFactory()
|
|
|
|
|
{
|
|
|
|
|
HostName = Configuration["EventBusConnection"]
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-05 15:55:17 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
|
|
|
|
|
{
|
|
|
|
|
factory.UserName = Configuration["EventBusUserName"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
|
|
|
|
|
{
|
|
|
|
|
factory.Password = Configuration["EventBusPassword"];
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-05-17 11:57:02 +02:00
|
|
|
|
|
2017-09-06 14:36:22 +03:00
|
|
|
|
services.AddHealthChecks(checks =>
|
2017-04-28 15:04:38 +02:00
|
|
|
|
{
|
2017-09-06 14:36:22 +03:00
|
|
|
|
checks.AddValueTaskCheck("HTTP Endpoint", () => new ValueTask<IHealthCheckResult>(HealthCheckResult.Healthy("Ok")));
|
2017-04-28 15:04:38 +02:00
|
|
|
|
});
|
2017-05-11 13:44:38 +02:00
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
RegisterEventBus(services);
|
2017-05-15 19:15:33 +02:00
|
|
|
|
|
2017-05-11 13:44:38 +02:00
|
|
|
|
var container = new ContainerBuilder();
|
|
|
|
|
container.Populate(services);
|
|
|
|
|
return new AutofacServiceProvider(container.Build());
|
2017-04-28 15:04:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
2017-10-11 18:53:26 +02:00
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
|
|
|
{
|
|
|
|
|
loggerFactory.AddAzureWebAppDiagnostics();
|
|
|
|
|
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
|
|
|
|
|
|
2017-09-07 19:18:53 +02:00
|
|
|
|
var pathBase = Configuration["PATH_BASE"];
|
|
|
|
|
if (!string.IsNullOrEmpty(pathBase))
|
|
|
|
|
{
|
|
|
|
|
app.UsePathBase(pathBase);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 19:15:33 +02:00
|
|
|
|
ConfigureEventBus(app);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
private void RegisterEventBus(IServiceCollection services)
|
2017-05-17 11:57:02 +02:00
|
|
|
|
{
|
2017-06-30 08:59:26 +02:00
|
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
|
|
|
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
|
|
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
|
|
|
|
var subscriptionClientName = Configuration["SubscriptionClientName"];
|
|
|
|
|
|
|
|
|
|
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
|
|
|
|
|
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
|
|
|
|
}
|
2017-05-17 11:57:02 +02:00
|
|
|
|
|
2017-06-30 16:31:22 +02:00
|
|
|
|
services.AddTransient<OrderStatusChangedToStockConfirmedIntegrationEventHandler>();
|
2017-06-30 08:59:26 +02:00
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
2017-05-17 11:57:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 19:15:33 +02:00
|
|
|
|
private void ConfigureEventBus(IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
2017-06-30 16:31:22 +02:00
|
|
|
|
eventBus.Subscribe<OrderStatusChangedToStockConfirmedIntegrationEvent, OrderStatusChangedToStockConfirmedIntegrationEventHandler>();
|
2017-04-28 15:04:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-17 19:42:45 +02:00
|
|
|
|
}
|