Skip to content

Commit c524848

Browse files
committed
change bff to 2.2 and fix mvc routing
1 parent 7fba79b commit c524848

16 files changed

Lines changed: 185 additions & 137 deletions

File tree

src/ApiGateways/ApiGw-Base/Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
1+
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS base
22
WORKDIR /app
33
EXPOSE 80
44

5-
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
5+
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
66
WORKDIR /src
7-
87
COPY scripts scripts/
98

109
COPY src/ApiGateways/*/*.csproj /src/csproj-files/

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
1+
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS base
22
WORKDIR /app
33
EXPOSE 80
44

5-
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
5+
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
66
WORKDIR /src
77

88
COPY scripts scripts/

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Filters/AuthorizeCheckOperationFilter.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Filters
22
{
33
using Microsoft.AspNetCore.Authorization;
4-
using Microsoft.OpenApi.Models;
4+
using Swashbuckle.AspNetCore.Swagger;
55
using Swashbuckle.AspNetCore.SwaggerGen;
66
using System.Collections.Generic;
77
using System.Linq;
@@ -10,27 +10,22 @@ namespace Basket.API.Infrastructure.Filters
1010
{
1111
public class AuthorizeCheckOperationFilter : IOperationFilter
1212
{
13-
public void Apply(OpenApiOperation operation, OperationFilterContext context)
13+
public void Apply(Operation operation, OperationFilterContext context)
1414
{
1515
// Check for authorize attribute
1616
var hasAuthorize = context.MethodInfo.DeclaringType.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any() ||
1717
context.MethodInfo.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any();
1818

1919
if (!hasAuthorize) return;
2020

21-
operation.Responses.TryAdd("401", new OpenApiResponse { Description = "Unauthorized" });
22-
operation.Responses.TryAdd("403", new OpenApiResponse { Description = "Forbidden" });
21+
operation.Responses.TryAdd("401", new Response { Description = "Unauthorized" });
22+
operation.Responses.TryAdd("403", new Response { Description = "Forbidden" });
2323

24-
var oAuthScheme = new OpenApiSecurityScheme
24+
operation.Security = new List<IDictionary<string, IEnumerable<string>>>
2525
{
26-
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
27-
};
28-
29-
operation.Security = new List<OpenApiSecurityRequirement>
30-
{
31-
new OpenApiSecurityRequirement
26+
new Dictionary<string, IEnumerable<string>>
3227
{
33-
[ oAuthScheme ] = new [] { "Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator" }
28+
{ "oauth2", new [] { "Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator" } }
3429
}
3530
};
3631
}

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<!--<Project Sdk="Microsoft.NET.Sdk.Web">
22
33
<PropertyGroup>
44
<TargetFramework>$(NetCoreTargetVersion)</TargetFramework>
@@ -30,4 +30,36 @@
3030
<ProjectReference Include="..\..\..\BuildingBlocks\Devspaces.Support\Devspaces.Support.csproj" />
3131
</ItemGroup>
3232
33+
</Project>-->
34+
<Project Sdk="Microsoft.NET.Sdk.Web">
35+
36+
<PropertyGroup>
37+
<TargetFramework>netcoreapp2.2</TargetFramework>
38+
<AssemblyName>Mobile.Shopping.HttpAggregator</AssemblyName>
39+
<RootNamespace>Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator</RootNamespace>
40+
<DockerComposeProjectPath>..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
41+
<LangVersion>$(LangVersion)</LangVersion>
42+
</PropertyGroup>
43+
44+
<ItemGroup>
45+
<Folder Include="wwwroot\" />
46+
</ItemGroup>
47+
48+
<ItemGroup>
49+
<PackageReference Include="AspNetCore.HealthChecks.Uris" Version="2.2.2" />
50+
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="2.2.3" />
51+
<PackageReference Include="Microsoft.AspNetCore.App" />
52+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
53+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="2.2.0" />
54+
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
55+
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
56+
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
57+
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="2.2.0" />
58+
</ItemGroup>
59+
60+
<ItemGroup>
61+
<ProjectReference Include="..\..\..\BuildingBlocks\Devspaces.Support\Devspaces.Support.csproj" />
62+
</ItemGroup>
63+
3364
</Project>
65+

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
66
using Microsoft.AspNetCore.Hosting;
77
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Mvc;
89
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config;
910
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Filters.Basket.API.Infrastructure.Filters;
1011
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Infrastructure;
@@ -13,9 +14,9 @@
1314
using Microsoft.Extensions.DependencyInjection;
1415
using Microsoft.Extensions.Diagnostics.HealthChecks;
1516
using Microsoft.Extensions.Logging;
16-
using Microsoft.OpenApi.Models;
1717
using Polly;
1818
using Polly.Extensions.Http;
19+
using Swashbuckle.AspNetCore.Swagger;
1920
using System;
2021
using System.Collections.Generic;
2122
using System.IdentityModel.Tokens.Jwt;
@@ -45,12 +46,10 @@ public void ConfigureServices(IServiceCollection services)
4546
.AddUrlGroup(new Uri(Configuration["PaymentUrlHC"]), name: "paymentapi-check", tags: new string[] { "paymentapi" })
4647
.AddUrlGroup(new Uri(Configuration["LocationUrlHC"]), name: "locationapi-check", tags: new string[] { "locationapi" });
4748

48-
services.AddCustomRouting(Configuration)
49+
services.AddCustomMvc(Configuration)
4950
.AddCustomAuthentication(Configuration)
5051
.AddDevspaces()
5152
.AddHttpServices();
52-
53-
services.AddControllers().AddNewtonsoftJson();
5453
}
5554

5655
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -64,6 +63,17 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6463
app.UsePathBase(pathBase);
6564
}
6665

66+
app.UseHealthChecks("/hc", new HealthCheckOptions()
67+
{
68+
Predicate = _ => true,
69+
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
70+
});
71+
72+
app.UseHealthChecks("/liveness", new HealthCheckOptions
73+
{
74+
Predicate = r => r.Name.Contains("self")
75+
});
76+
6777
app.UseCors("CorsPolicy");
6878

6979
if (env.IsDevelopment())
@@ -76,68 +86,52 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
7686
app.UseHsts();
7787
}
7888

79-
app.UseHttpsRedirection();
80-
app.UseRouting();
8189
app.UseAuthentication();
82-
app.UseAuthorization();
83-
app.UseEndpoints(endpoints =>
84-
{
85-
endpoints.MapDefaultControllerRoute();
86-
endpoints.MapControllers();
87-
endpoints.MapHealthChecks("/hc", new HealthCheckOptions()
88-
{
89-
Predicate = _ => true,
90-
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
91-
});
92-
endpoints.MapHealthChecks("/liveness", new HealthCheckOptions
93-
{
94-
Predicate = r => r.Name.Contains("self")
95-
});
96-
});
90+
app.UseHttpsRedirection();
91+
app.UseMvc();
9792

9893
app.UseSwagger().UseSwaggerUI(c =>
99-
{
100-
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1");
101-
102-
c.OAuthClientId("Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregatorwaggerui");
103-
c.OAuthClientSecret(string.Empty);
104-
c.OAuthRealm(string.Empty);
105-
c.OAuthAppName("Purchase BFF Swagger UI");
106-
});
94+
{
95+
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1");
96+
97+
c.OAuthClientId("Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregatorwaggerui");
98+
c.OAuthClientSecret(string.Empty);
99+
c.OAuthRealm(string.Empty);
100+
c.OAuthAppName("Purchase BFF Swagger UI");
101+
});
107102
}
108103
}
109104

110105
public static class ServiceCollectionExtensions
111106
{
112-
public static IServiceCollection AddCustomRouting(this IServiceCollection services, IConfiguration configuration)
107+
public static IServiceCollection AddCustomMvc(this IServiceCollection services, IConfiguration configuration)
113108
{
114109
services.AddOptions();
115110
services.Configure<UrlsConfig>(configuration.GetSection("urls"));
116-
services.AddControllers().AddNewtonsoftJson();
111+
112+
services.AddMvc()
113+
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
114+
117115
services.AddSwaggerGen(options =>
118116
{
119117
options.DescribeAllEnumsAsStrings();
120-
options.SwaggerDoc("v1", new OpenApiInfo
118+
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
121119
{
122-
Title = "eShopOnContainers - Shopping Aggregator for Mobile Clients",
120+
Title = "Shopping Aggregator for Mobile Clients",
123121
Version = "v1",
124-
Description = "Shopping Aggregator for Mobile Clients"
122+
Description = "Shopping Aggregator for Mobile Clients",
123+
TermsOfService = "Terms Of Service"
125124
});
126125

127-
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
126+
options.AddSecurityDefinition("oauth2", new OAuth2Scheme
128127
{
129-
Type = SecuritySchemeType.OAuth2,
130-
Flows = new OpenApiOAuthFlows()
128+
Type = "oauth2",
129+
Flow = "implicit",
130+
AuthorizationUrl = $"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize",
131+
TokenUrl = $"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token",
132+
Scopes = new Dictionary<string, string>()
131133
{
132-
Implicit = new OpenApiOAuthFlow()
133-
{
134-
AuthorizationUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
135-
TokenUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
136-
Scopes = new Dictionary<string, string>()
137-
{
138-
{ "marketing", "Marketing API" }
139-
}
140-
}
134+
{ "mobileshoppingagg", "Shopping Aggregator for Mobile Clients" }
141135
}
142136
});
143137

@@ -172,6 +166,15 @@ public static IServiceCollection AddCustomAuthentication(this IServiceCollection
172166
options.Authority = identityUrl;
173167
options.RequireHttpsMetadata = false;
174168
options.Audience = "mobileshoppingagg";
169+
options.Events = new JwtBearerEvents()
170+
{
171+
OnAuthenticationFailed = async ctx =>
172+
{
173+
},
174+
OnTokenValidated = async ctx =>
175+
{
176+
}
177+
};
175178
});
176179

177180
return services;

src/ApiGateways/Web.Bff.Shopping/aggregator/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
1+
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS base
22
WORKDIR /app
33
EXPOSE 80
44

5-
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
5+
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
66
WORKDIR /src
77

88
COPY scripts scripts/

src/ApiGateways/Web.Bff.Shopping/aggregator/Filters/AuthorizeCheckOperationFilter.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Filters
22
{
33
using Microsoft.AspNetCore.Authorization;
4-
using Microsoft.OpenApi.Models;
4+
using Swashbuckle.AspNetCore.Swagger;
55
using Swashbuckle.AspNetCore.SwaggerGen;
66
using System.Collections.Generic;
77
using System.Linq;
@@ -10,27 +10,22 @@ namespace Basket.API.Infrastructure.Filters
1010
{
1111
public class AuthorizeCheckOperationFilter : IOperationFilter
1212
{
13-
public void Apply(OpenApiOperation operation, OperationFilterContext context)
13+
public void Apply(Operation operation, OperationFilterContext context)
1414
{
1515
// Check for authorize attribute
1616
var hasAuthorize = context.MethodInfo.DeclaringType.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any() ||
1717
context.MethodInfo.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any();
1818

1919
if (!hasAuthorize) return;
2020

21-
operation.Responses.TryAdd("401", new OpenApiResponse { Description = "Unauthorized" });
22-
operation.Responses.TryAdd("403", new OpenApiResponse { Description = "Forbidden" });
21+
operation.Responses.TryAdd("401", new Response { Description = "Unauthorized" });
22+
operation.Responses.TryAdd("403", new Response { Description = "Forbidden" });
2323

24-
var oAuthScheme = new OpenApiSecurityScheme
24+
operation.Security = new List<IDictionary<string, IEnumerable<string>>>
2525
{
26-
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
27-
};
28-
29-
operation.Security = new List<OpenApiSecurityRequirement>
30-
{
31-
new OpenApiSecurityRequirement
26+
new Dictionary<string, IEnumerable<string>>
3227
{
33-
[ oAuthScheme ] = new [] { "Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator" }
28+
{ "oauth2", new [] { "Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator" } }
3429
}
3530
};
3631
}

0 commit comments

Comments
 (0)