Skip to content

Commit 0a0b013

Browse files
committed
change startup routing
1 parent 8b0ec90 commit 0a0b013

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
using Microsoft.Extensions.Diagnostics.HealthChecks;
2727
using Microsoft.Extensions.Logging;
2828
using Microsoft.Extensions.Options;
29+
using Microsoft.OpenApi.Models;
2930
using RabbitMQ.Client;
3031
using System;
32+
using System.Collections.Generic;
3133
using System.Data.Common;
3234
using System.IO;
3335
using System.Reflection;
@@ -52,7 +54,7 @@ public void ConfigureServices(IServiceCollection services)
5254
.AddCustomOptions(Configuration)
5355
.AddIntegrationServices(Configuration)
5456
.AddEventBus(Configuration)
55-
.AddSwagger()
57+
.AddSwagger(Configuration)
5658
.AddCustomHealthCheck(Configuration);
5759

5860
var container = new ContainerBuilder();
@@ -76,11 +78,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
7678
}
7779

7880
app.UseCors("CorsPolicy");
79-
81+
app.UseAuthorization();
8082
app.UseRouting();
8183
app.UseEndpoints(endpoints =>
8284
{
8385
endpoints.MapDefaultControllerRoute();
86+
endpoints.MapControllers();
8487
endpoints.MapGet("/_proto/", async ctx =>
8588
{
8689
ctx.Response.ContentType = "text/plain";
@@ -136,14 +139,10 @@ public static IServiceCollection AddAppInsight(this IServiceCollection services,
136139

137140
public static IServiceCollection AddCustomMVC(this IServiceCollection services, IConfiguration configuration)
138141
{
139-
services.AddMvc(options =>
142+
services.AddControllers(options =>
140143
{
141144
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
142-
})
143-
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
144-
.AddControllersAsServices();
145-
146-
services.AddControllers();
145+
});
147146

148147
services.AddCors(options =>
149148
{
@@ -259,7 +258,7 @@ public static IServiceCollection AddCustomOptions(this IServiceCollection servic
259258
return services;
260259
}
261260

262-
public static IServiceCollection AddSwagger(this IServiceCollection services)
261+
public static IServiceCollection AddSwagger(this IServiceCollection services, IConfiguration configuration)
263262
{
264263
services.AddSwaggerGen(options =>
265264
{
@@ -270,6 +269,22 @@ public static IServiceCollection AddSwagger(this IServiceCollection services)
270269
Version = "v1",
271270
Description = "The Catalog Microservice HTTP API. This is a Data-Driven/CRUD microservice sample"
272271
});
272+
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
273+
{
274+
Type = SecuritySchemeType.OAuth2,
275+
Flows = new OpenApiOAuthFlows()
276+
{
277+
Implicit = new OpenApiOAuthFlow()
278+
{
279+
AuthorizationUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
280+
TokenUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
281+
Scopes = new Dictionary<string, string>()
282+
{
283+
{ "catalog", "Catalog API" }
284+
}
285+
}
286+
}
287+
});
273288
});
274289

275290
return services;

src/Services/Location/Locations.API/Startup.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
77
using Microsoft.AspNetCore.Hosting;
88
using Microsoft.AspNetCore.Http;
9-
using Microsoft.AspNetCore.Mvc;
109
using Microsoft.Azure.ServiceBus;
1110
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
1211
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
@@ -42,14 +41,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
4241
{
4342
RegisterAppInsights(services);
4443

45-
services
46-
.AddCustomHealthCheck(Configuration)
47-
.AddMvc(options =>
48-
{
49-
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
50-
})
51-
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
52-
.AddControllersAsServices();
44+
services.AddCustomHealthCheck(Configuration);
5345

5446
services.AddControllers(options =>
5547
{
@@ -177,10 +169,12 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
177169

178170
ConfigureAuth(app);
179171

172+
app.UseAuthorization();
180173
app.UseRouting();
181174
app.UseEndpoints(endpoints =>
182175
{
183176
endpoints.MapDefaultControllerRoute();
177+
endpoints.MapControllers();
184178
endpoints.MapHealthChecks("/hc", new HealthCheckOptions()
185179
{
186180
Predicate = _ => true,

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
5555
.AddCustomConfiguration(Configuration)
5656
.AddEventBus(Configuration)
5757
.AddCustomAuthentication(Configuration);
58-
59-
services.AddControllers();
60-
6158
//configure autofac
6259

6360
var container = new ContainerBuilder();
@@ -86,10 +83,12 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
8683

8784
ConfigureAuth(app);
8885

86+
app.UseAuthorization();
8987
app.UseRouting();
9088
app.UseEndpoints(endpoints =>
9189
{
9290
endpoints.MapDefaultControllerRoute();
91+
endpoints.MapControllers();
9392
endpoints.MapHealthChecks("/hc", new HealthCheckOptions()
9493
{
9594
Predicate = _ => true,
@@ -149,13 +148,17 @@ public static IServiceCollection AddApplicationInsights(this IServiceCollection
149148
public static IServiceCollection AddCustomMvc(this IServiceCollection services)
150149
{
151150
// Add framework services.
152-
services.AddMvc(options =>
153-
{
154-
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
155-
})
156-
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
157-
.AddControllersAsServices(); //Injecting Controllers themselves thru DI
158-
//For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services
151+
services.AddControllers(options =>
152+
{
153+
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
154+
});
155+
//services.AddMvc(options =>
156+
// {
157+
// options.Filters.Add(typeof(HttpGlobalExceptionFilter));
158+
// })
159+
// .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
160+
// .AddControllersAsServices(); //Injecting Controllers themselves thru DI
161+
// //For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services
159162

160163
services.AddCors(options =>
161164
{

src/Services/Webhooks/Webhooks.API/Startup.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.AspNetCore.Builder;
77
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
88
using Microsoft.AspNetCore.Http;
9-
using Microsoft.AspNetCore.Mvc;
109
using Microsoft.Azure.ServiceBus;
1110
using Microsoft.EntityFrameworkCore;
1211
using Microsoft.EntityFrameworkCore.Diagnostics;
@@ -47,7 +46,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
4746
{
4847
services
4948
.AddAppInsight(Configuration)
50-
.AddCustomMVC(Configuration)
49+
.AddCustomRouting(Configuration)
5150
.AddCustomDbContext(Configuration)
5251
.AddSwagger(Configuration)
5352
.AddCustomHealthCheck(Configuration)
@@ -62,8 +61,6 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
6261
.AddTransient<IWebhooksRetriever, WebhooksRetriever>()
6362
.AddTransient<IWebhooksSender, WebhooksSender>();
6463

65-
services.AddControllers();
66-
6764
var container = new ContainerBuilder();
6865
container.Populate(services);
6966
return new AutofacServiceProvider(container.Build());
@@ -85,10 +82,12 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
8582

8683
ConfigureAuth(app);
8784

85+
app.UseAuthorization();
8886
app.UseRouting();
8987
app.UseEndpoints(endpoints =>
9088
{
9189
endpoints.MapDefaultControllerRoute();
90+
endpoints.MapControllers();
9291
endpoints.MapHealthChecks("/hc", new HealthCheckOptions()
9392
{
9493
Predicate = _ => true,
@@ -142,14 +141,12 @@ public static IServiceCollection AddAppInsight(this IServiceCollection services,
142141
return services;
143142
}
144143

145-
public static IServiceCollection AddCustomMVC(this IServiceCollection services, IConfiguration configuration)
144+
public static IServiceCollection AddCustomRouting(this IServiceCollection services, IConfiguration configuration)
146145
{
147-
services.AddMvc(options =>
146+
services.AddControllers(options =>
148147
{
149148
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
150-
})
151-
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
152-
.AddControllersAsServices();
149+
});
153150

154151
services.AddCors(options =>
155152
{
@@ -209,7 +206,7 @@ public static IServiceCollection AddSwagger(this IServiceCollection services, IC
209206
TokenUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
210207
Scopes = new Dictionary<string, string>()
211208
{
212-
{ "marketing", "Marketing API" }
209+
{ "webhooks", "Webhooks API" }
213210
}
214211
}
215212
}

0 commit comments

Comments
 (0)