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.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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