RobotApp/RobotApp/Program.cs
Đăng Nguyễn a51cfe80c8 update
2025-12-23 09:52:42 +07:00

109 lines
3.4 KiB
C#

using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using MudBlazor.Services;
using NLog.Web;
using RobotApp.Client;
using RobotApp.Client.Services;
using RobotApp.Components;
using RobotApp.Components.Account;
using RobotApp.Data;
using RobotApp.Hubs;
using RobotApp.Services;
using RobotApp.Services.Robot;
using RobotApp.Services.Robot.Simulation;
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseNLog();
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents()
.AddAuthenticationStateSerialization();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
builder.Services.AddMudServices();
builder.Services.AddAuthorization();
// Add Controllers for API endpoints
builder.Services.AddControllers();
builder.Services.AddSignalR();
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
Action<DbContextOptionsBuilder> appDbOptions = options => options.UseSqlite(connectionString, b => b.MigrationsAssembly("RobotApp"));
builder.Services.AddDbContext<ApplicationDbContext>(appDbOptions);
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
options.Lockout.AllowedForNewUsers = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.Password.RequireDigit = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
builder.Services.AddSingleton(typeof(RobotApp.Services.Logger<>));
// Add SignalR
builder.Services.AddSignalR();
builder.Services.AddRobotSimulation();
builder.Services.AddRobot();
// Add RobotMonitorService
builder.Services.AddSingleton<RobotApp.Services.RobotMonitorService>();
builder.Services.AddHostedService(sp => sp.GetRequiredService<RobotApp.Services.RobotMonitorService>());
//builder.Services.AddScoped<RobotStateClient>();
builder.Services.AddHostedService<RobotStatePublisher>();
var app = builder.Build();
await app.Services.SeedApplicationDbAsync();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();
app.MapStaticAssets();
// Map API Controllers
app.MapControllers();
// Map SignalR Hub
app.MapHub<RobotMonitorHub>("/hubs/robotMonitor");
app.MapHub<RobotHub>("/hubs/robot");
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(RobotApp.Client._Imports).Assembly);
// Add additional endpoints required by the Identity /Account Razor components.
app.MapAdditionalIdentityEndpoints();
app.Run();