using System.Text; using DRS9.Dashboard.Application.Services; using DRS9.Dashboard.Infrastructure.Data; using DRS9.Dashboard.Server.Middleware; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Configure Database builder.Services.AddDbContext(options => options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"))); // Configure JWT Authentication var jwtKey = builder.Configuration["Jwt:Key"] ?? throw new InvalidOperationException("JWT Key not configured"); var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? throw new InvalidOperationException("JWT Issuer not configured"); var jwtAudience = builder.Configuration["Jwt:Audience"] ?? throw new InvalidOperationException("JWT Audience not configured"); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = jwtIssuer, ValidAudience = jwtAudience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)) }; }); builder.Services.AddAuthorization(); // Add Blazor Server builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); // Configure Blazor Server Circuit builder.Services.AddServerSideBlazor() .AddCircuitOptions(options => { options.DetailedErrors = true; options.DisconnectedCircuitRetentionPeriod = TimeSpan.FromMinutes(3); }); // Register HttpClient and API Client Service builder.Services.AddHttpClient(client => { client.BaseAddress = new Uri("http://localhost:5000"); }); // Configure CORS builder.Services.AddCors(options => { options.AddPolicy("AllowAll", policy => { policy.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); // Register Application Services var jwtExpiry = int.TryParse(builder.Configuration["Jwt:ExpiryMinutes"], out var expiry) ? expiry : 43200; builder.Services.AddSingleton(new JwtTokenService( jwtKey, jwtIssuer, jwtAudience, jwtExpiry )); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddSingleton(); // Configure OpenAPI/Swagger builder.Services.AddEndpointsApiExplorer(); builder.Services.AddOpenApi(); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "DRS9 Dashboard API", Version = "v1", Description = "信息发布系统 API" }); // Add JWT Authentication to Swagger c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, Scheme = "Bearer" }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, Array.Empty() } }); }); var app = builder.Build(); // Initialize Database using (var scope = app.Services.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService(); await DataSeeder.SeedAsync(dbContext); } // Configure the HTTP request pipeline. app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "DRS9 Dashboard API v1"); }); app.UseCors("AllowAll"); // 静态文件 app.UseStaticFiles(); app.UseAuthentication(); app.UseAuthorization(); // Add anti-forgery for Blazor Server app.UseAntiforgery(); app.UseWebSockets(); app.MapControllers(); // Blazor Server app.MapRazorComponents() .AddInteractiveServerRenderMode(); // WebSocket endpoint app.Map("/ws", app => app.UseMiddleware()); app.Run();