281 lines
12 KiB
C#
281 lines
12 KiB
C#
namespace Microsoft.eShopOnContainers.Services.Ordering.API
|
|
{
|
|
using AspNetCore.Http;
|
|
using Autofac;
|
|
using Autofac.Extensions.DependencyInjection;
|
|
using global::Ordering.API.Application.IntegrationEvents;
|
|
using global::Ordering.API.Application.IntegrationEvents.Events;
|
|
using global::Ordering.API.Infrastructure.Filters;
|
|
using global::Ordering.API.Infrastructure.HostedServices;
|
|
using Infrastructure.AutofacModules;
|
|
using Infrastructure.Filters;
|
|
using Infrastructure.Services;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Azure.ServiceBus;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.HealthChecks;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Ordering.Infrastructure;
|
|
using RabbitMQ.Client;
|
|
using Swashbuckle.AspNetCore.Swagger;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Reflection;
|
|
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
|
{
|
|
// Add framework services.
|
|
services.AddMvc(options =>
|
|
{
|
|
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
|
|
}).AddControllersAsServices(); //Injecting Controllers themselves thru DI
|
|
//For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services
|
|
|
|
// Configure GracePeriodManager Hosted Service
|
|
services.AddSingleton<IHostedService, GracePeriodManagerService>();
|
|
|
|
services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
|
|
|
|
services.AddHealthChecks(checks =>
|
|
{
|
|
var minutes = 1;
|
|
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
|
|
{
|
|
minutes = minutesParsed;
|
|
}
|
|
checks.AddSqlCheck("OrderingDb", Configuration["ConnectionString"], TimeSpan.FromMinutes(minutes));
|
|
});
|
|
|
|
services.AddEntityFrameworkSqlServer()
|
|
.AddDbContext<OrderingContext>(options =>
|
|
{
|
|
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)
|
|
);
|
|
|
|
services.AddDbContext<IntegrationEventLogContext>(options =>
|
|
{
|
|
options.UseSqlServer(Configuration["ConnectionString"], opts =>
|
|
{
|
|
opts.MigrationsAssembly("Ordering.API");
|
|
});
|
|
});
|
|
|
|
|
|
services.Configure<OrderingSettings>(Configuration);
|
|
|
|
services.AddSwaggerGen(options =>
|
|
{
|
|
options.DescribeAllEnumsAsStrings();
|
|
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
|
|
{
|
|
Title = "Ordering HTTP API",
|
|
Version = "v1",
|
|
Description = "The Ordering Service HTTP API",
|
|
TermsOfService = "Terms Of Service"
|
|
});
|
|
|
|
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>()
|
|
{
|
|
{ "orders", "Ordering API" }
|
|
}
|
|
});
|
|
|
|
options.OperationFilter<AuthorizeCheckOperationFilter>();
|
|
});
|
|
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("CorsPolicy",
|
|
builder => builder.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials());
|
|
});
|
|
|
|
// Add application services.
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
services.AddTransient<IIdentityService, IdentityService>();
|
|
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
|
|
sp => (DbConnection c) => new IntegrationEventLogService(c));
|
|
|
|
services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
|
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
|
{
|
|
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
|
|
{
|
|
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
|
|
|
|
var serviceBusConnectionString = Configuration["EventBusConnection"];
|
|
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
|
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
|
{
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
|
|
|
|
|
var factory = new ConnectionFactory()
|
|
{
|
|
HostName = Configuration["EventBusConnection"]
|
|
};
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
|
|
{
|
|
factory.UserName = Configuration["EventBusUserName"];
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
|
|
{
|
|
factory.Password = Configuration["EventBusPassword"];
|
|
}
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
|
});
|
|
}
|
|
|
|
RegisterEventBus(services);
|
|
ConfigureAuthService(services);
|
|
services.AddOptions();
|
|
|
|
//configure autofac
|
|
|
|
var container = new ContainerBuilder();
|
|
container.Populate(services);
|
|
|
|
container.RegisterModule(new MediatorModule());
|
|
container.RegisterModule(new ApplicationModule(Configuration["ConnectionString"]));
|
|
|
|
return new AutofacServiceProvider(container.Build());
|
|
}
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
{
|
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
loggerFactory.AddDebug();
|
|
|
|
var pathBase = Configuration["PATH_BASE"];
|
|
if (!string.IsNullOrEmpty(pathBase))
|
|
{
|
|
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
|
|
app.UsePathBase(pathBase);
|
|
}
|
|
|
|
app.UseCors("CorsPolicy");
|
|
|
|
ConfigureAuth(app);
|
|
app.UseMvcWithDefaultRoute();
|
|
|
|
app.UseSwagger()
|
|
.UseSwaggerUI(c =>
|
|
{
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
|
|
c.ConfigureOAuth2("orderingswaggerui", "", "", "Ordering Swagger UI");
|
|
});
|
|
|
|
ConfigureEventBus(app);
|
|
}
|
|
|
|
private void ConfigureEventBus(IApplicationBuilder app)
|
|
{
|
|
var eventBus = app.ApplicationServices.GetRequiredService<BuildingBlocks.EventBus.Abstractions.IEventBus>();
|
|
|
|
eventBus.Subscribe<UserCheckoutAcceptedIntegrationEvent, IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
|
|
eventBus.Subscribe<GracePeriodConfirmedIntegrationEvent, IIntegrationEventHandler<GracePeriodConfirmedIntegrationEvent>>();
|
|
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>>();
|
|
eventBus.Subscribe<OrderStockRejectedIntegrationEvent, IIntegrationEventHandler<OrderStockRejectedIntegrationEvent>>();
|
|
eventBus.Subscribe<OrderPaymentFailedIntegrationEvent, IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>>();
|
|
eventBus.Subscribe<OrderPaymentSuccededIntegrationEvent, IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>>();
|
|
}
|
|
|
|
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 = "orders";
|
|
});
|
|
}
|
|
|
|
protected virtual void ConfigureAuth(IApplicationBuilder app)
|
|
{
|
|
app.UseAuthentication();
|
|
}
|
|
|
|
private void RegisterEventBus(IServiceCollection services)
|
|
{
|
|
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>();
|
|
}
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
|
}
|
|
}
|
|
}
|