|
1 | 1 | var builder = WebApplication.CreateBuilder(args);
|
2 | 2 |
|
3 |
| -builder.Services.AddHealthChecks() |
4 |
| - .AddCheck("self", () => HealthCheckResult.Healthy()) |
5 |
| - .AddUrlGroup(new Uri(builder.Configuration["CatalogUrlHC"]), name: "catalogapi-check", tags: new string[] { "catalogapi" }) |
6 |
| - .AddUrlGroup(new Uri(builder.Configuration["OrderingUrlHC"]), name: "orderingapi-check", tags: new string[] { "orderingapi" }) |
7 |
| - .AddUrlGroup(new Uri(builder.Configuration["BasketUrlHC"]), name: "basketapi-check", tags: new string[] { "basketapi" }) |
8 |
| - .AddUrlGroup(new Uri(builder.Configuration["IdentityUrlHC"]), name: "identityapi-check", tags: new string[] { "identityapi" }) |
9 |
| - .AddUrlGroup(new Uri(builder.Configuration["PaymentUrlHC"]), name: "paymentapi-check", tags: new string[] { "paymentapi" }); |
| 3 | +builder.AddServiceDefaults(); |
10 | 4 |
|
11 |
| -builder.Services.Configure<UrlsConfig>(builder.Configuration.GetSection("urls")); |
12 |
| - |
13 |
| -builder.Services.AddControllers() |
14 |
| - .AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = true); |
15 |
| - |
16 |
| -builder.Services.AddSwaggerGen(options => |
17 |
| -{ |
18 |
| - options.SwaggerDoc("v1", new OpenApiInfo |
19 |
| - { |
20 |
| - Title = "Shopping Aggregator for Mobile Clients", |
21 |
| - Version = "v1", |
22 |
| - Description = "Shopping Aggregator for Mobile Clients" |
23 |
| - }); |
24 |
| - var identityUrl = builder.Configuration.GetSection("Identity").GetValue<string>("ExternalUrl"); |
25 |
| - options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme |
26 |
| - { |
27 |
| - Type = SecuritySchemeType.OAuth2, |
28 |
| - Flows = new OpenApiOAuthFlows() |
29 |
| - { |
30 |
| - Implicit = new OpenApiOAuthFlow() |
31 |
| - { |
32 |
| - AuthorizationUrl = new Uri($"{identityUrl}/connect/authorize"), |
33 |
| - TokenUrl = new Uri($"{identityUrl}/connect/token"), |
34 |
| - |
35 |
| - Scopes = new Dictionary<string, string>() |
36 |
| - { |
37 |
| - { "mobileshoppingagg", "Shopping Aggregator for Mobile Clients" } |
38 |
| - } |
39 |
| - } |
40 |
| - } |
41 |
| - }); |
42 |
| - |
43 |
| - options.OperationFilter<AuthorizeCheckOperationFilter>(); |
44 |
| -}); |
| 5 | +builder.Services.AddReverseProxy(builder.Configuration); |
| 6 | +builder.Services.AddControllers(); |
45 | 7 |
|
| 8 | +builder.Services.AddHealthChecks(builder.Configuration); |
46 | 9 | builder.Services.AddCors(options =>
|
47 | 10 | {
|
| 11 | + // TODO: Read allowed origins from configuration |
48 | 12 | options.AddPolicy("CorsPolicy",
|
49 | 13 | builder => builder
|
| 14 | + .SetIsOriginAllowed((host) => true) |
50 | 15 | .AllowAnyMethod()
|
51 | 16 | .AllowAnyHeader()
|
52 |
| - .SetIsOriginAllowed((host) => true) |
53 | 17 | .AllowCredentials());
|
54 | 18 | });
|
55 | 19 |
|
56 |
| -JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub"); |
| 20 | +builder.Services.AddApplicationServices(); |
| 21 | +builder.Services.AddGrpcServices(); |
57 | 22 |
|
58 |
| -var identityUrl = builder.Configuration.GetValue<string>("urls:identity"); |
59 |
| - |
60 |
| -builder.Services.AddAuthentication(options => |
61 |
| -{ |
62 |
| - options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; |
63 |
| - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; |
64 |
| - |
65 |
| -}) |
66 |
| -.AddJwtBearer(options => |
67 |
| -{ |
68 |
| - options.Authority = identityUrl; |
69 |
| - options.RequireHttpsMetadata = false; |
70 |
| - options.Audience = "mobileshoppingagg"; |
71 |
| - options.TokenValidationParameters = new TokenValidationParameters |
72 |
| - { |
73 |
| - ValidateAudience = false |
74 |
| - }; |
75 |
| -}); |
76 |
| - |
77 |
| -builder.Services.AddAuthorization(options => |
78 |
| -{ |
79 |
| - options.AddPolicy("ApiScope", policy => |
80 |
| - { |
81 |
| - policy.RequireAuthenticatedUser(); |
82 |
| - policy.RequireClaim("scope", "mobileshoppingagg"); |
83 |
| - }); |
84 |
| -}); |
85 |
| - |
86 |
| -builder.Services.AddTransient<HttpClientAuthorizationDelegatingHandler>(); |
87 |
| -builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); |
88 |
| -builder.Services.AddHttpClient<IOrderApiClient, OrderApiClient>(); |
89 |
| - |
90 |
| -builder.Services.AddTransient<GrpcExceptionInterceptor>(); |
91 |
| -builder.Services.AddScoped<IBasketService, BasketService>(); |
92 |
| -builder.Services.AddGrpcClient<Basket.BasketClient>((services, options) => |
93 |
| -{ |
94 |
| - var basketApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcBasket; |
95 |
| - options.Address = new Uri(basketApi); |
96 |
| -}).AddInterceptor<GrpcExceptionInterceptor>(); |
97 |
| -builder.Services.AddScoped<ICatalogService, CatalogService>(); |
98 |
| -builder.Services.AddGrpcClient<Catalog.CatalogClient>((services, options) => |
99 |
| -{ |
100 |
| - var catalogApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcCatalog; |
101 |
| - options.Address = new Uri(catalogApi); |
102 |
| -}).AddInterceptor<GrpcExceptionInterceptor>(); |
103 |
| -builder.Services.AddScoped<IOrderingService, OrderingService>(); |
104 |
| -builder.Services.AddGrpcClient<GrpcOrdering.OrderingGrpc.OrderingGrpcClient>((services, options) => |
105 |
| -{ |
106 |
| - var orderingApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcOrdering; |
107 |
| - options.Address = new Uri(orderingApi); |
108 |
| -}).AddInterceptor<GrpcExceptionInterceptor>(); |
| 23 | +builder.Services.Configure<UrlsConfig>(builder.Configuration.GetSection("urls")); |
109 | 24 |
|
110 | 25 | var app = builder.Build();
|
111 | 26 |
|
112 |
| -var pathBase = app.Configuration["PATH_BASE"]; |
113 |
| - |
114 |
| -if (!string.IsNullOrEmpty(pathBase)) |
115 |
| -{ |
116 |
| - app.UsePathBase(pathBase); |
117 |
| -} |
118 |
| - |
119 |
| -app.UseSwagger().UseSwaggerUI(c => |
120 |
| -{ |
121 |
| - c.SwaggerEndpoint($"{(!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty)}/swagger/v1/swagger.json", "Purchase BFF V1"); |
| 27 | +app.UseServiceDefaults(); |
122 | 28 |
|
123 |
| - c.OAuthClientId("mobileshoppingaggswaggerui"); |
124 |
| - c.OAuthClientSecret(string.Empty); |
125 |
| - c.OAuthRealm(string.Empty); |
126 |
| - c.OAuthAppName("Purchase BFF Swagger UI"); |
127 |
| -}); |
| 29 | +app.UseHttpsRedirection(); |
128 | 30 |
|
129 |
| -app.UseRouting(); |
130 | 31 | app.UseCors("CorsPolicy");
|
131 | 32 | app.UseAuthentication();
|
132 | 33 | app.UseAuthorization();
|
133 |
| -app.MapDefaultControllerRoute(); |
| 34 | + |
134 | 35 | app.MapControllers();
|
135 |
| -app.MapHealthChecks("/hc", new HealthCheckOptions() |
136 |
| -{ |
137 |
| - Predicate = _ => true, |
138 |
| - ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse |
139 |
| -}); |
140 |
| -app.MapHealthChecks("/liveness", new HealthCheckOptions |
141 |
| -{ |
142 |
| - Predicate = r => r.Name.Contains("self") |
143 |
| -}); |
| 36 | +app.MapReverseProxy(); |
144 | 37 |
|
145 | 38 | await app.RunAsync();
|
0 commit comments