132 lines
4.7 KiB
C#
Raw Normal View History

2018-01-26 16:09:36 +00:00
using System;
using System.Collections.Generic;
2018-01-30 08:50:44 +00:00
using System.IdentityModel.Tokens.Jwt;
2018-01-26 16:09:36 +00:00
using System.Linq;
using System.Threading.Tasks;
2018-01-30 08:50:44 +00:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
2018-01-26 16:09:36 +00:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
2018-01-29 15:58:08 +00:00
using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http;
2018-01-26 16:09:36 +00:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
2018-01-29 15:58:08 +00:00
using PurchaseBff.Config;
2018-01-26 16:09:36 +00:00
using PurchaseBff.Filters.Basket.API.Infrastructure.Filters;
2018-01-29 15:58:08 +00:00
using PurchaseBff.Services;
2018-01-26 16:09:36 +00:00
using Swashbuckle.AspNetCore.Swagger;
namespace PurchaseBff
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
2018-01-29 15:58:08 +00:00
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IHttpClient, StandardHttpClient>();
services.AddTransient<ICatalogService, CatalogService>();
services.AddTransient<IBasketService, BasketService>();
services.AddOptions();
services.Configure<UrlsConfig>(Configuration.GetSection("urls"));
2018-01-26 16:09:36 +00:00
services.AddMvc();
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
{
Title = "Purchase BFF API Gateway",
Version = "v1",
Description = "BFF API Gateway for Purchase features",
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>()
{
{ "purchasebff", "Purchase BFF API Gateway" }
}
});
options.OperationFilter<AuthorizeCheckOperationFilter>();
});
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
2018-01-30 08:50:44 +00:00
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
var identityUrl = Configuration.GetValue<string>("urls:identity");
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = identityUrl;
options.RequireHttpsMetadata = false;
options.Audience = "ocelot";
options.Events = new JwtBearerEvents()
{
OnAuthenticationFailed = async ctx =>
{
int i = 0;
}
};
});
2018-01-26 16:09:36 +00: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)
{
var pathBase = Configuration["PATH_BASE"];
if (!string.IsNullOrEmpty(pathBase))
{
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
app.UsePathBase(pathBase);
}
app.UseCors("CorsPolicy");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
app.UseSwagger().UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1");
c.ConfigureOAuth2("purchasebffwaggerui", "", "", "Purchase BFF Swagger UI");
});
}
}
}