2016-11-21 12:41:36 +01:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
2016-12-22 13:20:12 +01:00
|
|
|
|
using AspNetCore.Http;
|
2016-11-24 14:58:37 +01:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
2017-04-28 15:04:38 +02:00
|
|
|
|
using global::Ordering.API.Application.IntegrationEvents;
|
2017-05-11 11:51:13 +02:00
|
|
|
|
using global::Ordering.API.Application.IntegrationEvents.Events;
|
2017-03-17 16:28:05 +01:00
|
|
|
|
using global::Ordering.API.Infrastructure.Middlewares;
|
2017-05-09 13:58:48 +02:00
|
|
|
|
using global::Ordering.API.Application.IntegrationCommands.Commands;
|
|
|
|
|
using global::Ordering.API.Application.IntegrationEvents.Events;
|
|
|
|
|
using global::Ordering.API.Application.Sagas;
|
2016-11-24 14:58:37 +01:00
|
|
|
|
using Infrastructure;
|
2017-01-10 11:37:36 +01:00
|
|
|
|
using Infrastructure.Auth;
|
2016-11-24 14:58:37 +01:00
|
|
|
|
using Infrastructure.AutofacModules;
|
|
|
|
|
using Infrastructure.Filters;
|
2016-12-22 13:20:12 +01:00
|
|
|
|
using Infrastructure.Services;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-05-03 10:59:36 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
2017-03-31 10:30:56 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-03-23 19:10:55 +01:00
|
|
|
|
using Microsoft.Extensions.HealthChecks;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-11-22 18:40:47 +01:00
|
|
|
|
using Ordering.Infrastructure;
|
2017-04-20 10:53:17 +02:00
|
|
|
|
using RabbitMQ.Client;
|
2016-11-24 14:58:37 +01:00
|
|
|
|
using System;
|
2017-03-31 10:30:56 +02:00
|
|
|
|
using System.Data.Common;
|
2016-11-22 18:40:47 +01:00
|
|
|
|
using System.Reflection;
|
2017-05-11 13:55:40 +02:00
|
|
|
|
using global::Ordering.API.Application.IntegrationEvents.EventHandling;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public Startup(IHostingEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(env.ContentRootPath)
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.AddJsonFile("settings.json", optional: true, reloadOnChange: true)
|
|
|
|
|
.AddJsonFile($"settings.{env.EnvironmentName}.json", optional: true);
|
2016-11-21 10:02:52 +01:00
|
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
2017-03-03 12:03:31 +01:00
|
|
|
|
builder.AddUserSecrets(typeof(Startup).GetTypeInfo().Assembly);
|
2016-11-21 10:02:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.AddEnvironmentVariables();
|
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
Configuration = builder.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IConfigurationRoot Configuration { get; }
|
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
|
|
|
|
// Add framework services.
|
2016-12-07 13:57:31 +01:00
|
|
|
|
services.AddMvc(options =>
|
2016-11-24 14:58:37 +01:00
|
|
|
|
{
|
2017-03-27 14:05:28 +02:00
|
|
|
|
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
|
2017-02-26 12:22:38 -08:00
|
|
|
|
}).AddControllersAsServices(); //Injecting Controllers themselves thru DI
|
|
|
|
|
//For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services
|
2016-09-09 17:24:24 -07:00
|
|
|
|
|
2017-03-23 19:10:55 +01:00
|
|
|
|
|
|
|
|
|
services.AddHealthChecks(checks =>
|
|
|
|
|
{
|
2017-03-31 12:47:56 +02:00
|
|
|
|
checks.AddSqlCheck("OrderingDb", Configuration["ConnectionString"]);
|
2017-03-23 19:10:55 +01:00
|
|
|
|
});
|
2017-03-31 10:30:56 +02:00
|
|
|
|
|
2016-11-22 18:40:47 +01:00
|
|
|
|
services.AddEntityFrameworkSqlServer()
|
2017-01-26 15:08:51 -08:00
|
|
|
|
.AddDbContext<OrderingContext>(options =>
|
|
|
|
|
{
|
2017-04-17 12:28:12 +02:00
|
|
|
|
options.UseSqlServer(Configuration["ConnectionString"],
|
|
|
|
|
sqlServerOptionsAction: sqlOptions =>
|
|
|
|
|
{
|
|
|
|
|
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
|
|
|
|
|
sqlOptions.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
ServiceLifetime.Scoped //Showing explicitly that the DbContext is shared across the HTTP request scope (graph of objects started in the HTTP request)
|
2017-01-26 15:08:51 -08:00
|
|
|
|
);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
2016-11-21 12:41:36 +01:00
|
|
|
|
services.AddSwaggerGen();
|
|
|
|
|
services.ConfigureSwaggerGen(options =>
|
2016-11-21 10:02:52 +01:00
|
|
|
|
{
|
2017-01-10 11:37:36 +01:00
|
|
|
|
options.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
|
2016-11-21 12:41:36 +01:00
|
|
|
|
options.DescribeAllEnumsAsStrings();
|
|
|
|
|
options.SingleApiVersion(new Swashbuckle.Swagger.Model.Info()
|
|
|
|
|
{
|
|
|
|
|
Title = "Ordering HTTP API",
|
|
|
|
|
Version = "v1",
|
|
|
|
|
Description = "The Ordering Service HTTP API",
|
2017-03-03 12:03:31 +01:00
|
|
|
|
TermsOfService = "Terms Of Service"
|
2016-11-21 12:41:36 +01:00
|
|
|
|
});
|
2016-11-21 10:02:52 +01:00
|
|
|
|
});
|
2016-09-12 20:28:55 -07:00
|
|
|
|
|
2016-12-12 10:15:24 +01:00
|
|
|
|
services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("CorsPolicy",
|
|
|
|
|
builder => builder.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials());
|
|
|
|
|
});
|
|
|
|
|
|
2016-12-22 13:20:12 +01:00
|
|
|
|
// Add application services.
|
|
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
2017-03-03 12:03:31 +01:00
|
|
|
|
services.AddTransient<IIdentityService, IdentityService>();
|
2017-03-31 10:30:56 +02:00
|
|
|
|
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
|
2017-04-03 13:13:40 +02:00
|
|
|
|
sp => (DbConnection c) => new IntegrationEventLogService(c));
|
2017-03-31 10:30:56 +02:00
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
2017-04-03 13:13:40 +02:00
|
|
|
|
services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
|
2017-04-20 10:53:17 +02:00
|
|
|
|
|
2017-04-29 21:58:11 -07:00
|
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
2017-04-20 10:53:17 +02:00
|
|
|
|
{
|
2017-04-29 21:58:11 -07:00
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
2017-04-20 10:53:17 +02:00
|
|
|
|
|
|
|
|
|
var factory = new ConnectionFactory()
|
|
|
|
|
{
|
|
|
|
|
HostName = Configuration["EventBusConnection"]
|
|
|
|
|
};
|
|
|
|
|
|
2017-04-29 21:58:11 -07:00
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
2017-04-20 10:53:17 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
2017-05-11 18:34:07 +02:00
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
|
|
|
|
services.AddTransient<IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
|
2017-05-09 13:58:48 +02:00
|
|
|
|
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
|
2017-05-11 18:34:07 +02:00
|
|
|
|
services.AddTransient<IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>>();
|
|
|
|
|
services.AddTransient<IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>>();
|
2016-11-24 14:58:37 +01:00
|
|
|
|
services.AddOptions();
|
|
|
|
|
|
|
|
|
|
//configure autofac
|
|
|
|
|
|
|
|
|
|
var container = new ContainerBuilder();
|
|
|
|
|
container.Populate(services);
|
|
|
|
|
|
|
|
|
|
container.RegisterModule(new MediatorModule());
|
2017-03-03 12:03:31 +01:00
|
|
|
|
container.RegisterModule(new ApplicationModule(Configuration["ConnectionString"]));
|
2016-11-24 14:58:37 +01:00
|
|
|
|
|
|
|
|
|
return new AutofacServiceProvider(container.Build());
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-03 16:34:41 +02:00
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
|
|
|
{
|
|
|
|
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
|
|
|
loggerFactory.AddDebug();
|
2017-03-27 14:05:28 +02:00
|
|
|
|
|
2016-12-12 10:15:24 +01:00
|
|
|
|
app.UseCors("CorsPolicy");
|
|
|
|
|
|
2017-03-17 16:28:05 +01:00
|
|
|
|
app.UseFailingMiddleware();
|
|
|
|
|
|
2017-03-06 16:06:32 +01:00
|
|
|
|
ConfigureAuth(app);
|
2016-12-07 13:57:31 +01:00
|
|
|
|
app.UseMvcWithDefaultRoute();
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
|
|
|
|
app.UseSwagger()
|
|
|
|
|
.UseSwaggerUi();
|
2016-12-07 13:57:31 +01:00
|
|
|
|
|
|
|
|
|
OrderingContextSeed.SeedAsync(app).Wait();
|
2017-05-11 13:44:38 +02:00
|
|
|
|
ConfigureEventBus(app);
|
2017-03-31 10:30:56 +02:00
|
|
|
|
|
|
|
|
|
var integrationEventLogContext = new IntegrationEventLogContext(
|
|
|
|
|
new DbContextOptionsBuilder<IntegrationEventLogContext>()
|
|
|
|
|
.UseSqlServer(Configuration["ConnectionString"], b => b.MigrationsAssembly("Ordering.API"))
|
|
|
|
|
.Options);
|
|
|
|
|
integrationEventLogContext.Database.Migrate();
|
2017-05-08 10:48:06 +02:00
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
2017-03-06 16:06:32 +01:00
|
|
|
|
|
2017-05-03 16:34:41 +02:00
|
|
|
|
private void ConfigureEventBus(IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
2017-05-11 11:51:13 +02:00
|
|
|
|
|
2017-05-11 18:34:07 +02:00
|
|
|
|
eventBus.Subscribe<UserCheckoutAcceptedIntegrationEvent, IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
|
2017-05-03 16:34:41 +02:00
|
|
|
|
|
2017-05-11 16:33:11 +02:00
|
|
|
|
eventBus.Subscribe<ConfirmGracePeriodCommandMsg, IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>();
|
2017-05-09 13:58:48 +02:00
|
|
|
|
|
2017-05-11 18:34:07 +02:00
|
|
|
|
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>>();
|
2017-05-08 13:59:59 +02:00
|
|
|
|
|
2017-05-11 18:34:07 +02:00
|
|
|
|
eventBus.Subscribe<OrderStockNotConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>>();
|
2017-05-03 16:34:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-06 16:06:32 +01:00
|
|
|
|
protected virtual void ConfigureAuth(IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
|
|
|
|
|
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
|
|
|
|
|
{
|
|
|
|
|
Authority = identityUrl.ToString(),
|
|
|
|
|
ScopeName = "orders",
|
|
|
|
|
RequireHttpsMetadata = false
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
}
|