Added SignalR for real-time progress updates during weather data fetch. Refactored WeatherPage to use a new reusable WeatherGrid component and SignalRHelper. Improved loading UI with a radial progress indicator.
75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
using Api.SignalR;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddOpenApi();
|
|
builder.Services.AddSignalR();
|
|
builder.Services.AddHostedService<SignalRSendService>();
|
|
builder.Services.AddSingleton<IUserIdProvider, UserProvider>();
|
|
builder.Services.AddOpenApiDocument(config => { config.Title = "NSwag Demo API"; });
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(policy =>
|
|
{
|
|
policy.WithOrigins("http://localhost:5173")
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapOpenApi();
|
|
app.UseOpenApi();
|
|
app.UseSwaggerUi();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseCors();
|
|
|
|
var summaries = new[]
|
|
{
|
|
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
|
};
|
|
|
|
app.MapGet("/weatherforecast", async () =>
|
|
{
|
|
var hub = app.Services.GetRequiredService<IHubContext<WeatherUpdateHub>>();
|
|
for (var i = 0; i < 100; i++)
|
|
{
|
|
await hub.Clients.All.SendAsync("ProgressUpdate", "None", $$"""{"percentage": "{{i}}", "message": "{{DateTime.Now.Millisecond}}"}""");
|
|
await Task.Delay(100);
|
|
}
|
|
|
|
|
|
var forecast = Enumerable.Range(1, 2500).Select(index =>
|
|
new WeatherForecast
|
|
(
|
|
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
|
Random.Shared.Next(-20, 55),
|
|
summaries[Random.Shared.Next(summaries.Length)]
|
|
))
|
|
.ToArray();
|
|
|
|
return forecast;
|
|
})
|
|
.WithName("GetWeatherForecast");
|
|
|
|
|
|
// Add SignalR hub
|
|
app.MapHub<WeatherUpdateHub>("/WeatherUpdateHub");
|
|
|
|
app.Run();
|
|
|
|
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
|
{
|
|
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
|
} |