This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Update/webshoppingagg webapp builder #2092
Merged
ReubenBond
merged 3 commits into
dotnet-architecture:dev
from
erjain:update/webshoppingagg-webap-builder
Apr 27, 2023
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
239 changes: 220 additions & 19 deletions
239
src/ApiGateways/Web.Bff.Shopping/aggregator/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,225 @@ | ||
await BuildWebHost(args).RunAsync(); | ||
var appName = "Ordering.API"; | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.WebHost.ConfigureAppConfiguration(cb => | ||
{ | ||
var sources = cb.Sources; | ||
sources.Insert(3, new Microsoft.Extensions.Configuration.Json.JsonConfigurationSource() | ||
{ | ||
Optional = true, | ||
Path = "appsettings.localhost.json", | ||
ReloadOnChange = false | ||
}); | ||
}); | ||
builder.Host.UseSerilog(CreateSerilogLogger(builder.Configuration)); | ||
builder.Services.AddHealthChecks() | ||
.AddCheck("self", () => HealthCheckResult.Healthy()) | ||
.AddUrlGroup(new Uri(builder.Configuration["CatalogUrlHC"]), name: "catalogapi-check", tags: new string[] { "catalogapi" }) | ||
.AddUrlGroup(new Uri(builder.Configuration["OrderingUrlHC"]), name: "orderingapi-check", tags: new string[] { "orderingapi" }) | ||
.AddUrlGroup(new Uri(builder.Configuration["BasketUrlHC"]), name: "basketapi-check", tags: new string[] { "basketapi" }) | ||
.AddUrlGroup(new Uri(builder.Configuration["IdentityUrlHC"]), name: "identityapi-check", tags: new string[] { "identityapi" }) | ||
.AddUrlGroup(new Uri(builder.Configuration["PaymentUrlHC"]), name: "paymentapi-check", tags: new string[] { "paymentapi" }); | ||
builder.Services.AddCustomMvc(builder.Configuration) | ||
.AddCustomAuthentication(builder.Configuration) | ||
//.AddCustomAuthorization(Configuration) | ||
.AddApplicationServices() | ||
.AddGrpcServices(); | ||
var app = builder.Build(); | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
else | ||
{ | ||
app.UseExceptionHandler("/Home/Error"); | ||
} | ||
var pathBase = builder.Configuration["PATH_BASE"]; | ||
if (!string.IsNullOrEmpty(pathBase)) | ||
{ | ||
app.UsePathBase(pathBase); | ||
} | ||
|
||
IWebHost BuildWebHost(string[] args) => | ||
WebHost | ||
.CreateDefaultBuilder(args) | ||
.ConfigureAppConfiguration(cb => | ||
app.UseHttpsRedirection(); | ||
|
||
app.UseSwagger().UseSwaggerUI(c => | ||
{ | ||
c.SwaggerEndpoint($"{(!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty)}/swagger/v1/swagger.json", "Purchase BFF V1"); | ||
|
||
c.OAuthClientId("webshoppingaggswaggerui"); | ||
c.OAuthClientSecret(string.Empty); | ||
c.OAuthRealm(string.Empty); | ||
c.OAuthAppName("web shopping bff Swagger UI"); | ||
}); | ||
|
||
app.UseRouting(); | ||
app.UseCors("CorsPolicy"); | ||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
app.MapDefaultControllerRoute(); | ||
app.MapControllers(); | ||
app.MapHealthChecks("/hc", new HealthCheckOptions() | ||
{ | ||
Predicate = _ => true, | ||
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse | ||
}); | ||
app.MapHealthChecks("/liveness", new HealthCheckOptions | ||
{ | ||
Predicate = r => r.Name.Contains("self") | ||
}); | ||
|
||
try | ||
{ | ||
Log.Information("Starts Web Application ({ApplicationContext})...", Program.AppName); | ||
await app.RunAsync(); | ||
|
||
return 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName); | ||
return 1; | ||
} | ||
finally | ||
{ | ||
Log.CloseAndFlush(); | ||
} | ||
|
||
Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) | ||
{ | ||
var seqServerUrl = configuration["Serilog:SeqServerUrl"]; | ||
var logstashUrl = configuration["Serilog:LogstashgUrl"]; | ||
return new LoggerConfiguration() | ||
.MinimumLevel.Verbose() | ||
.Enrich.WithProperty("ApplicationContext", Program.AppName) | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console() | ||
//.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl) | ||
//.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl, null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not commit commented-out code. Just delete it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure Reuben |
||
.ReadFrom.Configuration(configuration) | ||
.CreateLogger(); | ||
} | ||
public partial class Program | ||
{ | ||
|
||
public static string Namespace = typeof(Program).Assembly.GetName().Name; | ||
public static string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1); | ||
} | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub"); | ||
|
||
var identityUrl = configuration.GetValue<string>("urls:identity"); | ||
services.AddAuthentication("Bearer") | ||
.AddJwtBearer(options => | ||
{ | ||
var sources = cb.Sources; | ||
sources.Insert(3, new Microsoft.Extensions.Configuration.Json.JsonConfigurationSource() | ||
options.Authority = identityUrl; | ||
options.RequireHttpsMetadata = false; | ||
options.Audience = "webshoppingagg"; | ||
options.TokenValidationParameters = new TokenValidationParameters | ||
{ | ||
Optional = true, | ||
Path = "appsettings.localhost.json", | ||
ReloadOnChange = false | ||
ValidateAudience = false | ||
}; | ||
}); | ||
|
||
return services; | ||
} | ||
public static IServiceCollection AddCustomMvc(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
services.AddOptions(); | ||
services.Configure<UrlsConfig>(configuration.GetSection("urls")); | ||
|
||
services.AddControllers() | ||
.AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = true); | ||
|
||
services.AddSwaggerGen(options => | ||
{ | ||
//options.DescribeAllEnumsAsStrings(); | ||
|
||
options.SwaggerDoc("v1", new OpenApiInfo | ||
{ | ||
Title = "Shopping Aggregator for Web Clients", | ||
Version = "v1", | ||
Description = "Shopping Aggregator for Web Clients" | ||
}); | ||
}) | ||
.UseStartup<Startup>() | ||
.UseSerilog((builderContext, config) => | ||
|
||
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme | ||
{ | ||
Type = SecuritySchemeType.OAuth2, | ||
Flows = new OpenApiOAuthFlows() | ||
{ | ||
Implicit = new OpenApiOAuthFlow() | ||
{ | ||
AuthorizationUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"), | ||
TokenUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"), | ||
|
||
Scopes = new Dictionary<string, string>() | ||
{ | ||
{ "webshoppingagg", "Shopping Aggregator for Web Clients" } | ||
} | ||
} | ||
} | ||
}); | ||
|
||
options.OperationFilter<AuthorizeCheckOperationFilter>(); | ||
}); | ||
|
||
services.AddCors(options => | ||
{ | ||
options.AddPolicy("CorsPolicy", | ||
builder => builder | ||
.SetIsOriginAllowed((host) => true) | ||
.AllowAnyMethod() | ||
.AllowAnyHeader() | ||
.AllowCredentials()); | ||
}); | ||
|
||
return services; | ||
} | ||
public static IServiceCollection AddApplicationServices(this IServiceCollection services) | ||
{ | ||
//register delegating handlers | ||
services.AddTransient<HttpClientAuthorizationDelegatingHandler>(); | ||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); | ||
|
||
//register http services | ||
|
||
services.AddHttpClient<IOrderApiClient, OrderApiClient>() | ||
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>(); | ||
|
||
return services; | ||
} | ||
|
||
public static IServiceCollection AddGrpcServices(this IServiceCollection services) | ||
{ | ||
services.AddTransient<GrpcExceptionInterceptor>(); | ||
|
||
services.AddScoped<IBasketService, BasketService>(); | ||
|
||
services.AddGrpcClient<Basket.BasketClient>((services, options) => | ||
{ | ||
var basketApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcBasket; | ||
options.Address = new Uri(basketApi); | ||
}).AddInterceptor<GrpcExceptionInterceptor>(); | ||
|
||
services.AddScoped<ICatalogService, CatalogService>(); | ||
|
||
services.AddGrpcClient<Catalog.CatalogClient>((services, options) => | ||
{ | ||
config | ||
.MinimumLevel.Information() | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console(); | ||
}) | ||
.Build(); | ||
var catalogApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcCatalog; | ||
options.Address = new Uri(catalogApi); | ||
}).AddInterceptor<GrpcExceptionInterceptor>(); | ||
|
||
services.AddScoped<IOrderingService, OrderingService>(); | ||
|
||
services.AddGrpcClient<OrderingGrpc.OrderingGrpcClient>((services, options) => | ||
{ | ||
var orderingApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcOrdering; | ||
options.Address = new Uri(orderingApi); | ||
}).AddInterceptor<GrpcExceptionInterceptor>(); | ||
|
||
return services; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is extremely weird. Hard no on this approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be we can remove this section?