2017-05-18 08:40:35 +02:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
|
|
|
|
using Basket.API.Infrastructure.Filters;
|
2017-04-17 12:28:12 +02:00
|
|
|
|
using Basket.API.IntegrationEvents.EventHandling;
|
|
|
|
|
using Basket.API.IntegrationEvents.Events;
|
2017-08-29 18:11:30 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-05-18 08:40:35 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2017-08-29 10:20:13 +02:00
|
|
|
|
using Microsoft.Azure.ServiceBus;
|
2017-05-03 10:59:36 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
2017-04-17 12:28:12 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
2017-08-29 10:20:13 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
|
2017-04-17 12:28:12 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
2017-05-18 08:40:35 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-04-17 12:28:12 +02:00
|
|
|
|
using Microsoft.Extensions.HealthChecks;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-10-17 20:10:18 -07:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2017-04-20 10:53:17 +02:00
|
|
|
|
using RabbitMQ.Client;
|
2017-05-27 18:35:28 +02:00
|
|
|
|
using StackExchange.Redis;
|
2017-07-12 12:10:10 +02:00
|
|
|
|
using Swashbuckle.AspNetCore.Swagger;
|
2017-08-29 10:20:13 +02:00
|
|
|
|
using System;
|
2017-07-12 12:10:10 +02:00
|
|
|
|
using System.Collections.Generic;
|
2017-08-29 18:11:30 +02:00
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2017-08-29 10:20:13 +02:00
|
|
|
|
using System.Threading.Tasks;
|
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 Startup
|
|
|
|
|
{
|
2017-08-29 10:20:13 +02:00
|
|
|
|
public Startup(IConfiguration configuration)
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
2017-08-29 10:20:13 +02:00
|
|
|
|
Configuration = configuration;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-29 10:20:13 +02:00
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
2016-09-06 17:09:19 -07: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-07-12 12:10:10 +02:00
|
|
|
|
{
|
2016-09-06 17:09:19 -07:00
|
|
|
|
// Add framework services.
|
2017-03-27 14:05:28 +02:00
|
|
|
|
services.AddMvc(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
|
2017-08-22 18:03:12 +02:00
|
|
|
|
options.Filters.Add(typeof(ValidateModelStateFilter));
|
2017-08-29 10:20:13 +02:00
|
|
|
|
|
2017-03-27 14:05:28 +02:00
|
|
|
|
}).AddControllersAsServices();
|
|
|
|
|
|
2017-08-29 18:11:30 +02:00
|
|
|
|
ConfigureAuthService(services);
|
|
|
|
|
|
2017-07-12 12:10:10 +02:00
|
|
|
|
services.AddHealthChecks(checks =>
|
|
|
|
|
{
|
2017-07-13 14:48:55 -07:00
|
|
|
|
checks.AddValueTaskCheck("HTTP Endpoint", () => new ValueTask<IHealthCheckResult>(HealthCheckResult.Healthy("Ok")),
|
|
|
|
|
TimeSpan.Zero //No cache for this HealthCheck, better just for demos
|
|
|
|
|
);
|
2017-07-12 12:10:10 +02:00
|
|
|
|
});
|
|
|
|
|
|
2017-08-29 18:11:30 +02:00
|
|
|
|
services.Configure<BasketSettings>(Configuration);
|
2017-08-29 10:20:13 +02:00
|
|
|
|
|
2017-02-26 13:03:46 -05:00
|
|
|
|
//By connecting here we are making sure that our service
|
2016-10-17 20:10:18 -07:00
|
|
|
|
//cannot start until redis is ready. This might slow down startup,
|
2017-02-26 13:03:46 -05:00
|
|
|
|
//but given that there is a delay on resolving the ip address
|
2016-10-17 20:10:18 -07:00
|
|
|
|
//and then creating the connection it seems reasonable to move
|
|
|
|
|
//that cost to startup instead of having the first request pay the
|
|
|
|
|
//penalty.
|
2017-04-20 10:53:17 +02:00
|
|
|
|
services.AddSingleton<ConnectionMultiplexer>(sp =>
|
|
|
|
|
{
|
2017-04-17 12:28:12 +02:00
|
|
|
|
var settings = sp.GetRequiredService<IOptions<BasketSettings>>().Value;
|
2017-08-29 10:20:13 +02:00
|
|
|
|
var configuration = ConfigurationOptions.Parse(settings.ConnectionString, true);
|
|
|
|
|
|
2017-05-27 18:35:28 +02:00
|
|
|
|
configuration.ResolveDns = true;
|
2017-04-17 12:28:12 +02:00
|
|
|
|
|
2017-05-27 18:35:28 +02:00
|
|
|
|
return ConnectionMultiplexer.Connect(configuration);
|
2016-10-17 20:10:18 -07:00
|
|
|
|
});
|
|
|
|
|
|
2017-04-20 10:53:17 +02:00
|
|
|
|
|
2017-05-24 19:23:14 +02:00
|
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
2017-04-17 12:28:12 +02:00
|
|
|
|
{
|
2017-05-24 15:33:38 +02:00
|
|
|
|
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
|
2017-04-20 10:53:17 +02:00
|
|
|
|
{
|
2017-05-24 15:33:38 +02:00
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
|
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
var serviceBusConnectionString = Configuration["EventBusConnection"];
|
|
|
|
|
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
|
2017-05-24 15:33:38 +02:00
|
|
|
|
|
2017-05-24 18:59:29 +02:00
|
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
|
2017-05-24 15:33:38 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
2017-06-30 08:59:26 +02:00
|
|
|
|
|
2017-05-24 15:33:38 +02:00
|
|
|
|
var factory = new ConnectionFactory()
|
|
|
|
|
{
|
2017-06-30 08:59:26 +02:00
|
|
|
|
HostName = Configuration["EventBusConnection"]
|
2017-05-24 15:33:38 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-04-17 12:28:12 +02:00
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
RegisterEventBus(services);
|
|
|
|
|
|
2017-05-18 21:50:51 +03:00
|
|
|
|
services.AddSwaggerGen(options =>
|
2016-12-19 10:20:02 +01:00
|
|
|
|
{
|
|
|
|
|
options.DescribeAllEnumsAsStrings();
|
2017-08-29 10:20:13 +02:00
|
|
|
|
options.SwaggerDoc("v1", new Info
|
2016-12-19 10:20:02 +01:00
|
|
|
|
{
|
|
|
|
|
Title = "Basket HTTP API",
|
|
|
|
|
Version = "v1",
|
|
|
|
|
Description = "The Basket Service HTTP API",
|
|
|
|
|
TermsOfService = "Terms Of Service"
|
|
|
|
|
});
|
2017-07-12 12:10:10 +02:00
|
|
|
|
|
|
|
|
|
options.AddSecurityDefinition("oauth2", new OAuth2Scheme
|
|
|
|
|
{
|
|
|
|
|
Type = "oauth2",
|
|
|
|
|
Flow = "implicit",
|
|
|
|
|
AuthorizationUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize",
|
|
|
|
|
TokenUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token",
|
|
|
|
|
Scopes = new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{ "basket", "Basket API" }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
options.OperationFilter<AuthorizeCheckOperationFilter>();
|
2016-12-19 10:20:02 +01:00
|
|
|
|
});
|
|
|
|
|
|
2016-11-29 15:10:16 +01:00
|
|
|
|
services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("CorsPolicy",
|
|
|
|
|
builder => builder.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials());
|
|
|
|
|
});
|
2017-05-08 13:36:31 +02:00
|
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
2016-10-17 20:10:18 -07:00
|
|
|
|
services.AddTransient<IBasketRepository, RedisBasketRepository>();
|
2017-05-08 13:36:31 +02:00
|
|
|
|
services.AddTransient<IIdentityService, IdentityService>();
|
2017-08-29 18:11:30 +02:00
|
|
|
|
|
2017-05-18 08:40:35 +02:00
|
|
|
|
services.AddOptions();
|
2017-05-11 13:44:38 +02:00
|
|
|
|
|
|
|
|
|
var container = new ContainerBuilder();
|
|
|
|
|
container.Populate(services);
|
2017-08-29 10:20:13 +02:00
|
|
|
|
|
2017-05-11 13:44:38 +02:00
|
|
|
|
return new AutofacServiceProvider(container.Build());
|
2017-05-03 10:59:36 +02:00
|
|
|
|
}
|
2017-03-10 18:34:58 +01:00
|
|
|
|
|
2017-08-29 10:20:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
app.UseCors("CorsPolicy");
|
2017-08-29 12:48:04 +02:00
|
|
|
|
|
2017-08-29 18:11:30 +02:00
|
|
|
|
ConfigureAuth(app);
|
2017-08-29 12:48:04 +02:00
|
|
|
|
|
2017-08-29 10:20:13 +02:00
|
|
|
|
app.UseMvcWithDefaultRoute();
|
|
|
|
|
|
|
|
|
|
app.UseSwagger()
|
|
|
|
|
.UseSwaggerUI(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
|
|
|
|
|
c.ConfigureOAuth2("basketswaggerui", "", "", "Basket Swagger UI");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ConfigureEventBus(app);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-29 18:11:30 +02:00
|
|
|
|
private void ConfigureAuthService(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
// prevent from mapping "sub" claim to nameidentifier.
|
|
|
|
|
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
|
|
|
|
|
|
|
|
|
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
|
|
|
|
|
|
|
|
|
|
services.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
|
|
|
|
|
}).AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Authority = identityUrl;
|
|
|
|
|
options.RequireHttpsMetadata = false;
|
|
|
|
|
options.Audience = "basket";
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void ConfigureAuth(IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
}
|
2017-08-29 10:20:13 +02:00
|
|
|
|
|
2017-05-26 01:35:44 +02:00
|
|
|
|
private void RegisterEventBus(IServiceCollection services)
|
2017-05-03 10:59:36 +02:00
|
|
|
|
{
|
2017-05-24 19:23:14 +02:00
|
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
2017-05-24 15:33:38 +02:00
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
|
2017-06-26 18:05:02 +02:00
|
|
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
|
2017-05-24 15:33:38 +02:00
|
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
|
|
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
2017-05-24 18:59:29 +02:00
|
|
|
|
var subscriptionClientName = Configuration["SubscriptionClientName"];
|
2017-05-24 15:33:38 +02:00
|
|
|
|
|
2017-08-29 10:20:13 +02:00
|
|
|
|
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
|
2017-06-26 18:05:02 +02:00
|
|
|
|
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope);
|
2017-05-24 15:33:38 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-03 10:59:36 +02:00
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
|
|
|
|
|
|
|
|
|
services.AddTransient<ProductPriceChangedIntegrationEventHandler>();
|
|
|
|
|
services.AddTransient<OrderStartedIntegrationEventHandler>();
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-29 10:20:13 +02:00
|
|
|
|
private void ConfigureEventBus(IApplicationBuilder app)
|
2017-04-17 12:28:12 +02:00
|
|
|
|
{
|
2017-05-03 10:59:36 +02:00
|
|
|
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
2017-08-29 10:20:13 +02:00
|
|
|
|
|
2017-05-11 13:44:38 +02:00
|
|
|
|
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>();
|
|
|
|
|
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>();
|
2017-04-17 12:28:12 +02:00
|
|
|
|
}
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
}
|