2019-07-23 10:07:50 +02:00
|
|
|
|
using Devspaces.Support;
|
|
|
|
|
using HealthChecks.UI.Client;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2019-07-23 10:07:50 +02:00
|
|
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-11-14 16:21:50 +01:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Config;
|
|
|
|
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Filters.Basket.API.Infrastructure.Filters;
|
2018-05-18 14:02:18 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Infrastructure;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services;
|
2018-05-18 14:02:18 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2019-07-23 10:07:50 +02:00
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
2018-05-18 14:02:18 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Polly;
|
|
|
|
|
using Polly.Extensions.Http;
|
2019-08-05 15:03:57 +02:00
|
|
|
|
using Swashbuckle.AspNetCore.Swagger;
|
2018-05-18 14:02:18 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2018-05-25 11:52:35 +02:00
|
|
|
|
using System.Net.Http;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator
|
|
|
|
|
{
|
|
|
|
|
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-11-30 17:43:22 +01:00
|
|
|
|
services.AddHealthChecks()
|
2019-01-03 17:11:56 +01:00
|
|
|
|
.AddCheck("self", () => HealthCheckResult.Healthy())
|
2018-11-30 17:43:22 +01:00
|
|
|
|
.AddUrlGroup(new Uri(Configuration["CatalogUrlHC"]), name: "catalogapi-check", tags: new string[] { "catalogapi" })
|
|
|
|
|
.AddUrlGroup(new Uri(Configuration["OrderingUrlHC"]), name: "orderingapi-check", tags: new string[] { "orderingapi" })
|
|
|
|
|
.AddUrlGroup(new Uri(Configuration["BasketUrlHC"]), name: "basketapi-check", tags: new string[] { "basketapi" })
|
|
|
|
|
.AddUrlGroup(new Uri(Configuration["IdentityUrlHC"]), name: "identityapi-check", tags: new string[] { "identityapi" })
|
|
|
|
|
.AddUrlGroup(new Uri(Configuration["MarketingUrlHC"]), name: "marketingapi-check", tags: new string[] { "marketingapi" })
|
|
|
|
|
.AddUrlGroup(new Uri(Configuration["PaymentUrlHC"]), name: "paymentapi-check", tags: new string[] { "paymentapi" })
|
|
|
|
|
.AddUrlGroup(new Uri(Configuration["LocationUrlHC"]), name: "locationapi-check", tags: new string[] { "locationapi" });
|
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
services.AddCustomMvc(Configuration)
|
|
|
|
|
.AddCustomAuthentication(Configuration)
|
2019-02-25 18:21:31 +01:00
|
|
|
|
.AddDevspaces()
|
2018-05-18 14:02:18 +02:00
|
|
|
|
.AddApplicationServices();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
|
{
|
2019-02-22 15:05:28 +00:00
|
|
|
|
loggerFactory.CreateLogger<Startup>().LogDebug("Using PATH BASE '{pathBase}'", pathBase);
|
2018-05-18 14:02:18 +02:00
|
|
|
|
app.UsePathBase(pathBase);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 15:03:57 +02:00
|
|
|
|
app.UseHealthChecks("/hc", new HealthCheckOptions()
|
|
|
|
|
{
|
|
|
|
|
Predicate = _ => true,
|
|
|
|
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.UseHealthChecks("/liveness", new HealthCheckOptions
|
|
|
|
|
{
|
|
|
|
|
Predicate = r => r.Name.Contains("self")
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
app.UseCors("CorsPolicy");
|
|
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
2018-11-14 16:21:50 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
|
|
|
app.UseHsts();
|
|
|
|
|
}
|
2018-05-18 14:02:18 +02:00
|
|
|
|
|
|
|
|
|
app.UseAuthentication();
|
2018-11-14 16:21:50 +01:00
|
|
|
|
app.UseHttpsRedirection();
|
2019-08-05 15:03:57 +02:00
|
|
|
|
app.UseMvc();
|
2018-05-18 14:02:18 +02:00
|
|
|
|
|
2018-11-15 12:50:37 +01:00
|
|
|
|
app.UseSwagger()
|
|
|
|
|
.UseSwaggerUI(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1");
|
|
|
|
|
//c.ConfigureOAuth2("Microsoft.eShopOnContainers.Web.Shopping.HttpAggregatorwaggerui", "", "", "Purchase BFF Swagger UI");
|
2019-08-27 09:40:31 +02:00
|
|
|
|
c.OAuthClientId("webshoppingaggswaggerui");
|
|
|
|
|
c.OAuthAppName("web shopping bff Swagger UI");
|
2018-11-15 12:50:37 +01:00
|
|
|
|
});
|
2018-05-18 14:02:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class ServiceCollectionExtensions
|
|
|
|
|
{
|
|
|
|
|
public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
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 = "webshoppingagg";
|
|
|
|
|
options.Events = new JwtBearerEvents()
|
|
|
|
|
{
|
|
|
|
|
OnAuthenticationFailed = async ctx =>
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
},
|
|
|
|
|
OnTokenValidated = async ctx =>
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
return services;
|
|
|
|
|
}
|
2018-11-15 12:50:37 +01:00
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
public static IServiceCollection AddCustomMvc(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
2018-02-27 14:32:25 +01:00
|
|
|
|
services.AddOptions();
|
2018-05-18 14:02:18 +02:00
|
|
|
|
services.Configure<UrlsConfig>(configuration.GetSection("urls"));
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2018-11-14 16:21:50 +01:00
|
|
|
|
services.AddMvc()
|
2019-08-05 15:03:57 +02:00
|
|
|
|
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
|
|
|
|
services.AddSwaggerGen(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DescribeAllEnumsAsStrings();
|
2019-08-05 15:03:57 +02:00
|
|
|
|
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2019-08-05 15:03:57 +02:00
|
|
|
|
Title = "Shopping Aggregator for Web Clients",
|
2018-02-27 14:32:25 +01:00
|
|
|
|
Version = "v1",
|
2019-08-05 15:03:57 +02:00
|
|
|
|
Description = "Shopping Aggregator for Web Clients",
|
|
|
|
|
TermsOfService = "Terms Of Service"
|
2018-02-27 14:32:25 +01:00
|
|
|
|
});
|
|
|
|
|
|
2019-08-05 15:03:57 +02:00
|
|
|
|
options.AddSecurityDefinition("oauth2", new OAuth2Scheme
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2019-08-05 15:03:57 +02:00
|
|
|
|
Type = "oauth2",
|
|
|
|
|
Flow = "implicit",
|
|
|
|
|
AuthorizationUrl = $"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize",
|
|
|
|
|
TokenUrl = $"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token",
|
|
|
|
|
Scopes = new Dictionary<string, string>()
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2019-08-27 09:40:31 +02:00
|
|
|
|
{ "webshoppingagg", "Shopping Aggregator for Web Clients" },
|
|
|
|
|
{ "basket", "basket api" }
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
options.OperationFilter<AuthorizeCheckOperationFilter>();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("CorsPolicy",
|
2019-01-04 13:39:25 +01:00
|
|
|
|
builder => builder
|
|
|
|
|
.SetIsOriginAllowed((host) => true)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials());
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
return services;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
2018-05-18 14:02:18 +02:00
|
|
|
|
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2018-05-18 14:02:18 +02:00
|
|
|
|
//register delegating handlers
|
|
|
|
|
services.AddTransient<HttpClientAuthorizationDelegatingHandler>();
|
|
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
//register http services
|
2019-02-25 18:21:31 +01:00
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
services.AddHttpClient<IBasketService, BasketService>()
|
|
|
|
|
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
|
2018-05-25 11:52:35 +02:00
|
|
|
|
.AddPolicyHandler(GetRetryPolicy())
|
2019-02-25 18:21:31 +01:00
|
|
|
|
.AddPolicyHandler(GetCircuitBreakerPolicy())
|
|
|
|
|
.AddDevspacesSupport();
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
services.AddHttpClient<ICatalogService, CatalogService>()
|
2018-05-25 11:52:35 +02:00
|
|
|
|
.AddPolicyHandler(GetRetryPolicy())
|
2019-02-25 18:21:31 +01:00
|
|
|
|
.AddPolicyHandler(GetCircuitBreakerPolicy())
|
|
|
|
|
.AddDevspacesSupport();
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
services.AddHttpClient<IOrderApiClient, OrderApiClient>()
|
|
|
|
|
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
|
2018-05-25 11:52:35 +02:00
|
|
|
|
.AddPolicyHandler(GetRetryPolicy())
|
2019-02-25 18:21:31 +01:00
|
|
|
|
.AddPolicyHandler(GetCircuitBreakerPolicy())
|
|
|
|
|
.AddDevspacesSupport();
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
return services;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
2018-05-25 11:52:35 +02:00
|
|
|
|
|
|
|
|
|
static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
|
|
|
|
|
{
|
|
|
|
|
return HttpPolicyExtensions
|
|
|
|
|
.HandleTransientHttpError()
|
|
|
|
|
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
|
|
|
|
|
.WaitAndRetryAsync(6, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
|
|
|
|
|
|
|
|
|
|
}
|
2018-11-15 12:50:37 +01:00
|
|
|
|
|
2018-05-25 11:52:35 +02:00
|
|
|
|
static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy()
|
|
|
|
|
{
|
|
|
|
|
return HttpPolicyExtensions
|
|
|
|
|
.HandleTransientHttpError()
|
|
|
|
|
.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30));
|
|
|
|
|
}
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|