88 lines
2.9 KiB
C#
Raw Normal View History

2018-11-13 09:30:16 +01:00
using Microsoft.AspNetCore.Builder;
2018-01-11 11:20:38 +01:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
2018-01-11 11:20:38 +01:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
2019-01-02 09:06:45 +01:00
using OcelotApiGw.Enums;
using OcelotApiGw.Orchestrators;
using OcelotApiGw.Services;
using System.Linq;
2018-01-11 11:20:38 +01:00
namespace OcelotApiGw
{
public class Startup
{
2019-01-02 09:06:45 +01:00
private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration)
{
2019-01-02 09:06:45 +01:00
_configuration = configuration;
}
2018-01-11 11:20:38 +01:00
public void ConfigureServices(IServiceCollection services)
{
2019-01-02 09:06:45 +01:00
var identityUrl = _configuration.GetValue<string>("IdentityUrl");
2018-01-30 08:50:44 +00:00
var authenticationProviderKey = "IdentityApiKey";
services.AddCors(options =>
{
2019-01-02 09:06:45 +01:00
options.AddPolicy("CorsPolicy", builder =>
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
})
.AddTransient<IOrchestratorStrategy, Kubernetes>()
.AddTransient<IOrchestratorStrategy, ServiceFabric>()
.AddAuthentication()
.AddJwtBearer(authenticationProviderKey, options =>
{
options.Authority = identityUrl;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
ValidAudiences = new[] { "orders", "basket", "locations", "marketing", "mobileshoppingagg", "webshoppingagg" }
};
});
2019-01-02 09:06:45 +01:00
services.AddHttpClient<ISettingService, SettingService>();
2019-01-02 09:06:45 +01:00
var orchestratorType = _configuration.GetValue<OrchestratorType>("OrchestratorType");
2018-01-30 08:50:44 +00:00
2019-01-02 09:06:45 +01:00
var config = new ConfigurationBuilder()
.AddConfiguration(_configuration);
var cfg = services.BuildServiceProvider()
.GetServices<IOrchestratorStrategy>()
.Single(strategy => strategy.OrchestratorType == orchestratorType)
.ConfigureOrchestrator(config)
.Build();
services.AddOcelot(cfg);
2018-01-11 11:20:38 +01:00
}
2018-01-18 14:45:46 +01:00
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
2018-01-11 11:20:38 +01:00
{
2019-01-02 09:06:45 +01:00
var pathBase = _configuration.GetValue<string>("PATH_BASE");
2018-03-14 18:25:26 +01:00
if (!string.IsNullOrEmpty(pathBase))
{
app.UsePathBase(pathBase);
}
2018-01-11 11:20:38 +01:00
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
2019-01-02 09:06:45 +01:00
loggerFactory.AddConsole(_configuration.GetSection("Logging"));
2019-01-02 09:06:45 +01:00
app.UseCors("CorsPolicy")
.UseOcelot()
.Wait();
2018-01-11 11:20:38 +01:00
}
}
}