From 0a0b013b73d7a96f77cf37f30a1b5aef89c57709 Mon Sep 17 00:00:00 2001 From: Erik Pique Date: Fri, 26 Jul 2019 11:11:31 +0200 Subject: [PATCH] change startup routing --- src/Services/Catalog/Catalog.API/Startup.cs | 33 ++++++++++++++----- .../Location/Locations.API/Startup.cs | 12 ++----- src/Services/Ordering/Ordering.API/Startup.cs | 23 +++++++------ src/Services/Webhooks/Webhooks.API/Startup.cs | 17 ++++------ 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index 2a5f2bfb8..732e7cddd 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -26,8 +26,10 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Microsoft.OpenApi.Models; using RabbitMQ.Client; using System; +using System.Collections.Generic; using System.Data.Common; using System.IO; using System.Reflection; @@ -52,7 +54,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API .AddCustomOptions(Configuration) .AddIntegrationServices(Configuration) .AddEventBus(Configuration) - .AddSwagger() + .AddSwagger(Configuration) .AddCustomHealthCheck(Configuration); var container = new ContainerBuilder(); @@ -76,11 +78,12 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API } app.UseCors("CorsPolicy"); - + app.UseAuthorization(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); + endpoints.MapControllers(); endpoints.MapGet("/_proto/", async ctx => { ctx.Response.ContentType = "text/plain"; @@ -136,14 +139,10 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API public static IServiceCollection AddCustomMVC(this IServiceCollection services, IConfiguration configuration) { - services.AddMvc(options => + services.AddControllers(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); - }) - .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) - .AddControllersAsServices(); - - services.AddControllers(); + }); services.AddCors(options => { @@ -259,7 +258,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API return services; } - public static IServiceCollection AddSwagger(this IServiceCollection services) + public static IServiceCollection AddSwagger(this IServiceCollection services, IConfiguration configuration) { services.AddSwaggerGen(options => { @@ -270,6 +269,22 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API Version = "v1", Description = "The Catalog Microservice HTTP API. This is a Data-Driven/CRUD microservice sample" }); + options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme + { + Type = SecuritySchemeType.OAuth2, + Flows = new OpenApiOAuthFlows() + { + Implicit = new OpenApiOAuthFlow() + { + AuthorizationUrl = new Uri($"{configuration.GetValue("IdentityUrlExternal")}/connect/authorize"), + TokenUrl = new Uri($"{configuration.GetValue("IdentityUrlExternal")}/connect/token"), + Scopes = new Dictionary() + { + { "catalog", "Catalog API" } + } + } + } + }); }); return services; diff --git a/src/Services/Location/Locations.API/Startup.cs b/src/Services/Location/Locations.API/Startup.cs index 2199157c8..ff7c79416 100644 --- a/src/Services/Location/Locations.API/Startup.cs +++ b/src/Services/Location/Locations.API/Startup.cs @@ -6,7 +6,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.ServiceBus; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; @@ -42,14 +41,7 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API { RegisterAppInsights(services); - services - .AddCustomHealthCheck(Configuration) - .AddMvc(options => - { - options.Filters.Add(typeof(HttpGlobalExceptionFilter)); - }) - .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) - .AddControllersAsServices(); + services.AddCustomHealthCheck(Configuration); services.AddControllers(options => { @@ -177,10 +169,12 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API ConfigureAuth(app); + app.UseAuthorization(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); + endpoints.MapControllers(); endpoints.MapHealthChecks("/hc", new HealthCheckOptions() { Predicate = _ => true, diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index 4e9480e70..b3dfa840c 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -55,9 +55,6 @@ .AddCustomConfiguration(Configuration) .AddEventBus(Configuration) .AddCustomAuthentication(Configuration); - - services.AddControllers(); - //configure autofac var container = new ContainerBuilder(); @@ -86,10 +83,12 @@ ConfigureAuth(app); + app.UseAuthorization(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); + endpoints.MapControllers(); endpoints.MapHealthChecks("/hc", new HealthCheckOptions() { Predicate = _ => true, @@ -149,13 +148,17 @@ public static IServiceCollection AddCustomMvc(this IServiceCollection services) { // Add framework services. - services.AddMvc(options => - { - options.Filters.Add(typeof(HttpGlobalExceptionFilter)); - }) - .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) - .AddControllersAsServices(); //Injecting Controllers themselves thru DI - //For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services + services.AddControllers(options => + { + options.Filters.Add(typeof(HttpGlobalExceptionFilter)); + }); + //services.AddMvc(options => + // { + // options.Filters.Add(typeof(HttpGlobalExceptionFilter)); + // }) + // .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) + // .AddControllersAsServices(); //Injecting Controllers themselves thru DI + // //For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services services.AddCors(options => { diff --git a/src/Services/Webhooks/Webhooks.API/Startup.cs b/src/Services/Webhooks/Webhooks.API/Startup.cs index 14781383d..4d3bd2c81 100644 --- a/src/Services/Webhooks/Webhooks.API/Startup.cs +++ b/src/Services/Webhooks/Webhooks.API/Startup.cs @@ -6,7 +6,6 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.ServiceBus; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; @@ -47,7 +46,7 @@ namespace Webhooks.API { services .AddAppInsight(Configuration) - .AddCustomMVC(Configuration) + .AddCustomRouting(Configuration) .AddCustomDbContext(Configuration) .AddSwagger(Configuration) .AddCustomHealthCheck(Configuration) @@ -62,8 +61,6 @@ namespace Webhooks.API .AddTransient() .AddTransient(); - services.AddControllers(); - var container = new ContainerBuilder(); container.Populate(services); return new AutofacServiceProvider(container.Build()); @@ -85,10 +82,12 @@ namespace Webhooks.API ConfigureAuth(app); + app.UseAuthorization(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); + endpoints.MapControllers(); endpoints.MapHealthChecks("/hc", new HealthCheckOptions() { Predicate = _ => true, @@ -142,14 +141,12 @@ namespace Webhooks.API return services; } - public static IServiceCollection AddCustomMVC(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddCustomRouting(this IServiceCollection services, IConfiguration configuration) { - services.AddMvc(options => + services.AddControllers(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); - }) - .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) - .AddControllersAsServices(); + }); services.AddCors(options => { @@ -209,7 +206,7 @@ namespace Webhooks.API TokenUrl = new Uri($"{configuration.GetValue("IdentityUrlExternal")}/connect/token"), Scopes = new Dictionary() { - { "marketing", "Marketing API" } + { "webhooks", "Webhooks API" } } } }