Skip to content
Permalink
Branch: master
Find file Copy path
2 contributors

Users who have contributed to this file

@spetz @GooRiOn
104 lines (93 sloc) 3.62 KB
using System;
using System.Reflection;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Consul;
using DShop.Api.Services;
using DShop.Common;
using DShop.Common.Authentication;
using DShop.Common.Consul;
using DShop.Common.Dispatchers;
using DShop.Common.Jaeger;
using DShop.Common.Mvc;
using DShop.Common.RabbitMq;
using DShop.Common.Redis;
using DShop.Common.RestEase;
using DShop.Common.Swagger;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace DShop.Api
{
public class Startup
{
private static readonly string[] Headers = new []{ "X-Operation", "X-Resource", "X-Total-Count" };
public IContainer Container { get; private set; }
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddCustomMvc();
services.AddSwaggerDocs();
services.AddConsul();
services.AddJwt();
services.AddJaeger();
services.AddOpenTracing();
services.AddRedis();
services.AddAuthorization(x => x.AddPolicy("admin", p => p.RequireRole("admin")));
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", cors =>
cors.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.WithExposedHeaders(Headers));
});
services.RegisterServiceForwarder<IOperationsService>("operations-service");
services.RegisterServiceForwarder<ICustomersService>("customers-service");
services.RegisterServiceForwarder<IOrdersService>("orders-service");
services.RegisterServiceForwarder<IProductsService>("products-service");
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(Assembly.GetEntryAssembly())
.AsImplementedInterfaces();
builder.Populate(services);
builder.AddRabbitMq();
builder.AddDispatchers();
Container = builder.Build();
return new AutofacServiceProvider(Container);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
IApplicationLifetime applicationLifetime, IConsulClient client,
IStartupInitializer startupInitializer)
{
if (env.IsDevelopment() || env.EnvironmentName == "local")
{
app.UseDeveloperExceptionPage();
}
app.UseCors("CorsPolicy");
app.UseAllForwardedHeaders();
app.UseSwaggerDocs();
app.UseErrorHandler();
app.UseAuthentication();
app.UseAccessTokenValidator();
app.UseServiceId();
app.UseMvc();
app.UseRabbitMq();
var consulServiceId = app.UseConsul();
applicationLifetime.ApplicationStopped.Register(() =>
{
client.Agent.ServiceDeregister(consulServiceId);
Container.Dispose();
});
startupInitializer.InitializeAsync();
}
}
}
You can’t perform that action at this time.