2017-06-13 17:31:37 +02:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2017-05-30 15:01:58 +02:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-06-13 17:31:37 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
2017-05-30 15:01:58 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure;
|
2017-06-13 17:31:37 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Filters;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Repositories;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services;
|
2017-05-30 15:01:58 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-06-13 17:31:37 +02:00
|
|
|
|
using RabbitMQ.Client;
|
2017-05-30 15:01:58 +02:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System;
|
2017-06-30 08:59:26 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
|
|
|
|
|
using Microsoft.Azure.ServiceBus;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Swashbuckle.AspNetCore.Swagger;
|
2017-05-30 15:01:58 +02:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Locations.API
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public IConfigurationRoot Configuration { get; }
|
|
|
|
|
|
|
|
|
|
public Startup(IHostingEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(env.ContentRootPath)
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
|
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
|
|
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
builder.AddUserSecrets(typeof(Startup).GetTypeInfo().Assembly);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.AddEnvironmentVariables();
|
|
|
|
|
|
|
|
|
|
Configuration = builder.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
2017-06-13 17:31:37 +02:00
|
|
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
2017-05-30 15:01:58 +02:00
|
|
|
|
{
|
|
|
|
|
// Add framework services.
|
2017-06-08 17:33:55 +02:00
|
|
|
|
services.AddMvc(options =>
|
2017-05-30 15:01:58 +02:00
|
|
|
|
{
|
2017-06-08 17:33:55 +02:00
|
|
|
|
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
|
|
|
|
|
}).AddControllersAsServices();
|
2017-05-30 15:01:58 +02:00
|
|
|
|
|
2017-06-08 17:33:55 +02:00
|
|
|
|
services.Configure<LocationSettings>(Configuration);
|
2017-06-13 17:31:37 +02:00
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
2017-06-13 17:31:37 +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-06-13 17:31:37 +02:00
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
2017-06-13 17:31:37 +02:00
|
|
|
|
{
|
2017-06-30 08:59:26 +02:00
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
2017-06-13 17:31:37 +02:00
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
var factory = new ConnectionFactory()
|
|
|
|
|
{
|
|
|
|
|
HostName = Configuration["EventBusConnection"]
|
|
|
|
|
};
|
2017-06-13 17:31:37 +02:00
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RegisterEventBus(services);
|
2017-06-13 17:31:37 +02:00
|
|
|
|
|
2017-05-30 15:01:58 +02:00
|
|
|
|
// Add framework services.
|
|
|
|
|
services.AddSwaggerGen(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DescribeAllEnumsAsStrings();
|
|
|
|
|
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
|
|
|
|
|
{
|
|
|
|
|
Title = "eShopOnContainers - Location HTTP API",
|
|
|
|
|
Version = "v1",
|
|
|
|
|
Description = "The Location Microservice HTTP API. This is a Data-Driven/CRUD microservice sample",
|
|
|
|
|
TermsOfService = "Terms Of Service"
|
|
|
|
|
});
|
2017-06-30 08:59:26 +02:00
|
|
|
|
|
|
|
|
|
options.AddSecurityDefinition("oauth2", new OAuth2Scheme
|
|
|
|
|
{
|
|
|
|
|
Type = "oauth2",
|
|
|
|
|
Flow = "implicit",
|
|
|
|
|
AuthorizationUrl = "http://localhost:5105/connect/authorize",
|
|
|
|
|
TokenUrl = "http://localhost:5105/connect/token",
|
|
|
|
|
Scopes = new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{ "locations", "Locations API" }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
options.OperationFilter<AuthorizeCheckOperationFilter>();
|
|
|
|
|
|
2017-05-30 15:01:58 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("CorsPolicy",
|
|
|
|
|
builder => builder.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials());
|
|
|
|
|
});
|
2017-06-01 20:16:19 +02:00
|
|
|
|
|
|
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
|
|
|
services.AddTransient<IIdentityService, IdentityService>();
|
|
|
|
|
services.AddTransient<ILocationsService, LocationsService>();
|
2017-06-13 17:31:37 +02:00
|
|
|
|
services.AddTransient<ILocationsRepository, LocationsRepository>();
|
|
|
|
|
|
|
|
|
|
//configure autofac
|
|
|
|
|
var container = new ContainerBuilder();
|
|
|
|
|
container.Populate(services);
|
|
|
|
|
|
|
|
|
|
return new AutofacServiceProvider(container.Build());
|
2017-05-30 15:01:58 +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, ILoggerFactory loggerFactory)
|
|
|
|
|
{
|
|
|
|
|
//Configure logs
|
|
|
|
|
|
|
|
|
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
|
|
|
loggerFactory.AddDebug();
|
|
|
|
|
|
|
|
|
|
app.UseCors("CorsPolicy");
|
|
|
|
|
|
|
|
|
|
ConfigureAuth(app);
|
|
|
|
|
|
|
|
|
|
app.UseMvcWithDefaultRoute();
|
|
|
|
|
|
|
|
|
|
app.UseSwagger()
|
|
|
|
|
.UseSwaggerUI(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
|
2017-06-30 08:59:26 +02:00
|
|
|
|
c.ConfigureOAuth2("swaggerui", "", "", "Swagger UI");
|
2017-05-30 15:01:58 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
LocationsContextSeed.SeedAsync(app, loggerFactory)
|
|
|
|
|
.Wait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void ConfigureAuth(IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
|
|
|
|
|
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
|
|
|
|
|
{
|
|
|
|
|
Authority = identityUrl.ToString(),
|
|
|
|
|
ApiName = "locations",
|
|
|
|
|
RequireHttpsMetadata = false
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-06-13 17:31:37 +02:00
|
|
|
|
|
2017-06-30 08:59:26 +02:00
|
|
|
|
private void RegisterEventBus(IServiceCollection services)
|
2017-06-13 17:31:37 +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-06-13 17:31:37 +02:00
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
2017-06-30 08:59:26 +02:00
|
|
|
|
}
|
2017-05-30 15:01:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|