Blazor & WASM in combination to get statistics from Spotify API for performing the song analysis. With separate microservices for auth, Spotify, user data tracking, and application, connected through gRPC with Polly.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Microsoft.AspNetCore.Server.Kestrel.Core;
  2. using NemAnBlazor.Services.Interfaces;
  3. using NemAnBlazor.Services;
  4. using ProtoBuf.Grpc.Server;
  5. using SpotifyService.Services;
  6. using GrpcShared.Interfaces;
  7. using Polly;
  8. using Polly.Extensions.Http;
  9. using GrpcShared.DTO.Auth;
  10. using IdentityProvider.Services;
  11. var builder = WebApplication.CreateBuilder(args);
  12. #if DEBUG
  13. //builder.WebHost.ConfigureKestrel(options =>
  14. //{
  15. // options.ListenLocalhost(5050, o => o.Protocols =
  16. // HttpProtocols.Http2);
  17. // options.ListenLocalhost(5051, o => o.Protocols =
  18. // HttpProtocols.Http1AndHttp2);
  19. //});
  20. #endif
  21. // Add services to the container.
  22. builder.Services.AddHttpClient("HttpClient", c =>
  23. {
  24. c.BaseAddress = new Uri(SpotifyService.GLOBALS.SPOTIFYURL);
  25. c.DefaultRequestHeaders.Add("Accept", SpotifyService.GLOBALS.MEDIATYPE);
  26. })
  27. .SetHandlerLifetime(TimeSpan.FromMinutes(5))
  28. .AddPolicyHandler(GetRetryPolicy())
  29. .AddPolicyHandler(GetCircuitBreaker())
  30. .AddPolicyHandler(GetBulkheadPolicy(50,200));
  31. IAsyncPolicy<HttpResponseMessage> GetBulkheadPolicy(int capacity, int queueLength)
  32. {
  33. //As soon as we hit 50 concurrent requests, the policy will add subsequent requests to the pending request queue.
  34. //Once the pending request queue is full, then the policy will start rejecting any other calls to the service.
  35. return Policy.BulkheadAsync<HttpResponseMessage>(capacity, queueLength);
  36. }
  37. IAsyncPolicy<HttpResponseMessage> GetCircuitBreaker()
  38. {
  39. return HttpPolicyExtensions
  40. .HandleTransientHttpError()
  41. //the circuit will be cut if 25% of requests fail in a 60 second window, with a minimum of 7 requests in the 60 second window,
  42. //then the circuit should be cut for 30 seconds:
  43. .AdvancedCircuitBreakerAsync(0.25, TimeSpan.FromSeconds(60),
  44. 7, TimeSpan.FromSeconds(30), OnBreak, OnReset, OnHalfOpen);
  45. }
  46. void OnHalfOpen()
  47. {
  48. Console.WriteLine("Circuit in test mode, one request will be allowed.");
  49. }
  50. void OnReset()
  51. {
  52. Console.WriteLine("Circuit closed, requests flow normally.");
  53. }
  54. void OnBreak(DelegateResult<HttpResponseMessage> result, TimeSpan ts)
  55. {
  56. Console.WriteLine("Circuit cut, requests will not flow.");
  57. }
  58. IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
  59. {
  60. return HttpPolicyExtensions
  61. // HttpRequestException, 5XX and 408
  62. .HandleTransientHttpError()
  63. // 404
  64. .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
  65. // Retry two times after delay
  66. .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)));
  67. }
  68. builder.Services.AddOptions();
  69. builder.Services.AddControllersWithViews();
  70. builder.Services.AddRazorPages();
  71. builder.Services.AddEndpointsApiExplorer();
  72. builder.Services.AddSwaggerGen();
  73. builder.Services.AddGrpc();
  74. builder.Services.AddCodeFirstGrpc();
  75. builder.Services.AddCodeFirstGrpcReflection();
  76. builder.Services.Configure<CodeRequest>(builder.Configuration.GetSection("AuthParams"));
  77. builder.Services.AddScoped<IIdentityService, IdentityService>();
  78. builder.Services.AddScoped<IAuthService, AuthService>();
  79. var app = builder.Build();
  80. app.UseSwagger();
  81. app.UseSwaggerUI();
  82. // Configure the HTTP request pipeline.
  83. if (app.Environment.IsDevelopment())
  84. {
  85. app.UseWebAssemblyDebugging();
  86. }
  87. else
  88. {
  89. app.UseExceptionHandler("/Error");
  90. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  91. app.UseHsts();
  92. }
  93. app.UseHttpsRedirection();
  94. app.UseBlazorFrameworkFiles();
  95. app.UseStaticFiles();
  96. app.UseRouting();
  97. app.UseGrpcWeb();
  98. app.MapRazorPages();
  99. app.MapControllers();
  100. //app.MapGrpcService<AuthService>();
  101. app.MapGrpcService<AuthService>().EnableGrpcWeb();
  102. app.MapGrpcService<TrackService>().EnableGrpcWeb();
  103. app.MapGrpcService<StatsService>().EnableGrpcWeb();
  104. app.MapGrpcService<IdentityService>().EnableGrpcWeb();
  105. app.MapCodeFirstGrpcReflectionService();
  106. app.MapFallbackToFile("index.html");
  107. app.Run();