213 lines
8.6 KiB
C#
Raw Normal View History

2019-07-23 10:07:50 +02:00
using Devspaces.Support;
2020-12-14 16:47:42 +01:00
using GrpcBasket;
2019-07-23 10:07:50 +02:00
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Authentication.JwtBearer;
2018-01-26 16:09:36 +00:00
using Microsoft.AspNetCore.Builder;
2019-07-23 10:07:50 +02:00
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
2018-01-26 16:09:36 +00:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
2018-02-15 17:30:39 +01:00
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config;
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Filters.Basket.API.Infrastructure.Filters;
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Infrastructure;
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2019-07-23 10:07:50 +02:00
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
2020-12-14 16:47:42 +01:00
using Microsoft.Extensions.Options;
2019-08-19 09:24:58 +02:00
using Microsoft.OpenApi.Models;
2019-07-23 10:07:50 +02:00
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
2018-01-26 16:09:36 +00:00
2018-02-15 17:30:39 +01:00
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator
2018-01-26 16:09:36 +00:00
{
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)
{
services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy())
.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" });
2019-08-05 15:03:57 +02:00
services.AddCustomMvc(Configuration)
.AddCustomAuthentication(Configuration)
.AddDevspaces()
2020-12-14 16:47:42 +01:00
.AddHttpServices()
.AddGrpcServices();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment 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);
app.UsePathBase(pathBase);
}
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
2019-08-27 09:40:31 +02:00
app.UseSwagger().UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1");
c.OAuthClientId("mobileshoppingaggswaggerui");
c.OAuthClientSecret(string.Empty);
c.OAuthRealm(string.Empty);
c.OAuthAppName("Purchase BFF Swagger UI");
});
2019-08-19 09:24:58 +02:00
app.UseRouting();
2020-06-08 21:58:48 +02:00
app.UseCors("CorsPolicy");
2019-08-19 09:24:58 +02:00
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapControllers();
endpoints.MapHealthChecks("/hc", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
endpoints.MapHealthChecks("/liveness", new HealthCheckOptions
{
Predicate = r => r.Name.Contains("self")
});
});
}
}
2018-01-29 15:58:08 +00:00
public static class ServiceCollectionExtensions
{
2019-08-05 15:03:57 +02:00
public static IServiceCollection AddCustomMvc(this IServiceCollection services, IConfiguration configuration)
{
2018-01-29 15:58:08 +00:00
services.AddOptions();
services.Configure<UrlsConfig>(configuration.GetSection("urls"));
2019-08-05 15:03:57 +02:00
2019-08-19 09:24:58 +02:00
services.AddControllers()
.AddNewtonsoftJson();
2019-08-05 15:03:57 +02:00
2018-01-26 16:09:36 +00:00
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
2019-08-19 09:24:58 +02:00
options.SwaggerDoc("v1", new OpenApiInfo
2018-01-26 16:09:36 +00:00
{
2019-08-05 15:03:57 +02:00
Title = "Shopping Aggregator for Mobile Clients",
2018-01-26 16:09:36 +00:00
Version = "v1",
2019-08-19 09:24:58 +02:00
Description = "Shopping Aggregator for Mobile Clients"
2018-01-26 16:09:36 +00:00
});
2019-08-19 09:24:58 +02:00
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
2018-01-26 16:09:36 +00:00
{
2019-08-19 09:24:58 +02:00
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
2018-01-26 16:09:36 +00:00
{
2019-08-19 09:24:58 +02:00
Implicit = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
TokenUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
2019-08-19 09:24:58 +02:00
Scopes = new Dictionary<string, string>()
{
{ "mobileshoppingagg", "Shopping Aggregator for Mobile Clients" }
}
}
2018-01-26 16:09:36 +00:00
}
});
options.OperationFilter<AuthorizeCheckOperationFilter>();
});
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder
2018-01-26 16:09:36 +00:00
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed((host) => true)
2018-01-26 16:09:36 +00:00
.AllowCredentials());
});
2018-01-30 08:50:44 +00:00
return services;
}
public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
{
2019-08-19 09:24:58 +02:00
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");
var identityUrl = configuration.GetValue<string>("urls:identity");
2018-01-30 08:50:44 +00:00
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
2018-01-30 08:50:44 +00:00
{
options.Authority = identityUrl;
options.RequireHttpsMetadata = false;
2018-02-15 17:30:39 +01:00
options.Audience = "mobileshoppingagg";
2018-01-30 08:50:44 +00:00
});
2018-01-26 16:09:36 +00:00
return services;
}
public static IServiceCollection AddHttpServices(this IServiceCollection services)
2018-01-26 16:09:36 +00:00
{
//register delegating handlers
services.AddTransient<HttpClientAuthorizationDelegatingHandler>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
2018-01-26 16:09:36 +00:00
//register http services
2018-01-26 16:09:36 +00:00
services.AddHttpClient<ICatalogService, CatalogService>()
.AddDevspacesSupport();
services.AddHttpClient<IOrderApiClient, OrderApiClient>()
.AddDevspacesSupport();
2018-01-26 16:09:36 +00:00
services.AddHttpClient<IOrderingService, OrderingService>()
.AddDevspacesSupport();
return services;
2018-01-26 16:09:36 +00:00
}
2020-12-14 16:47:42 +01:00
public static IServiceCollection AddGrpcServices(this IServiceCollection services)
{
2020-12-15 10:46:50 +01:00
services.AddSingleton<GrpcExceptionInterceptor>();
services.AddScoped<IBasketService, BasketService>();
2020-12-14 16:47:42 +01:00
services.AddGrpcClient<Basket.BasketClient>((services, options) =>
{
2020-12-15 10:46:50 +01:00
var basketApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcBasket;
2020-12-14 16:47:42 +01:00
options.Address = new Uri(basketApi);
2020-12-15 10:46:50 +01:00
}).AddInterceptor<GrpcExceptionInterceptor>();
2020-12-14 16:47:42 +01:00
return services;
}
2018-01-26 16:09:36 +00:00
}
}