55using Microsoft . AspNetCore . Diagnostics . HealthChecks ;
66using Microsoft . AspNetCore . Hosting ;
77using Microsoft . AspNetCore . Http ;
8+ using Microsoft . AspNetCore . Mvc ;
89using Microsoft . eShopOnContainers . Mobile . Shopping . HttpAggregator . Config ;
910using Microsoft . eShopOnContainers . Mobile . Shopping . HttpAggregator . Filters . Basket . API . Infrastructure . Filters ;
1011using Microsoft . eShopOnContainers . Mobile . Shopping . HttpAggregator . Infrastructure ;
1314using Microsoft . Extensions . DependencyInjection ;
1415using Microsoft . Extensions . Diagnostics . HealthChecks ;
1516using Microsoft . Extensions . Logging ;
16- using Microsoft . OpenApi . Models ;
1717using Polly ;
1818using Polly . Extensions . Http ;
19+ using Swashbuckle . AspNetCore . Swagger ;
1920using System ;
2021using System . Collections . Generic ;
2122using 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 ;
0 commit comments