using System.Text; using FreeSql; using F9WebAiTest.Api.Hubs; using F9WebAiTest.Api.Services; using F9WebAiTest.Application; using F9WebAiTest.Application.Services; using F9WebAiTest.Application.Services.Notice; using F9WebAiTest.Infrastructure; using F9WebAiTest.Infrastructure.Data; using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.SignalR; using Microsoft.IdentityModel.Tokens; var builder = WebApplication.CreateBuilder(args); // 分层注册 builder.Services.AddInfrastructure(builder.Configuration); builder.Services.AddApplication(); // JWT 认证 var jwtKey = builder.Configuration["Jwt:Key"]; builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = builder.Configuration["Jwt:Issuer"], ValidAudience = builder.Configuration["Jwt:Audience"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)) }; // SignalR 与报表模板/数据下载通过 query string 传递 token options.Events = new JwtBearerEvents { OnMessageReceived = context => { var accessToken = context.Request.Query["access_token"]; if (string.IsNullOrEmpty(accessToken)) return Task.CompletedTask; var path = context.HttpContext.Request.Path; if (path.StartsWithSegments("/hubs") || path.StartsWithSegments("/api/sys/report")) { context.Token = accessToken; } return Task.CompletedTask; } }; }); builder.Services.AddAuthorization(); builder.Services.AddControllers() .AddJsonOptions(options => { // long 序列化为字符串,避免雪花 ID 超出 JS 安全整数范围导致精度丢失 options.JsonSerializerOptions.Converters.Add(new F9WebAiTest.Api.JsonConverters.LongToStringJsonConverter()); }); // SignalR 实时推送 + 自定义用户标识 + 即时推送器(创建消息时直接推送,无后台投递服务) builder.Services.AddSignalR(); builder.Services.AddSingleton(); builder.Services.AddScoped(); // 业务状态变更事件 → SignalR 推送(多端状态同步) builder.Services.AddScoped, BusinessChangedEventHandler>(); // CORS builder.Services.AddCors(options => { options.AddPolicy("AllowAll", policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); }); // NSwag(OpenAPI 文档 + Swagger UI + 可生成 TS/C# 客户端) builder.Services.AddEndpointsApiExplorer(); builder.Services.AddOpenApiDocument(settings => { settings.Title = "企业 RBAC 管理系统 API"; settings.Version = "v1"; settings.Description = "基于 NSwag 生成的 OpenAPI 文档,可用于生成 TypeScript/C# 客户端"; settings.AddSecurity("Bearer", new NSwag.OpenApiSecurityScheme { Type = NSwag.OpenApiSecuritySchemeType.Http, Scheme = "bearer", BearerFormat = "JWT", Description = "请输入 access token(格式:Bearer {token})" }); settings.OperationProcessors.Add(new NSwag.Generation.Processors.Security.AspNetCoreOperationSecurityScopeProcessor("Bearer")); }); var app = builder.Build(); // 初始化数据库(FreeSql CodeFirst 建表 + 种子) using (var scope = app.Services.CreateScope()) { var fsql = scope.ServiceProvider.GetRequiredService(); var snowflake = scope.ServiceProvider.GetRequiredService(); DbSeeder.Seed(fsql, snowflake); } // 前端静态资源:Vue 打包产物放在 wwwroot。 // IIS 单站点部署时前端与 API 同源(浏览器访问 /api/...),无需跨域与反向代理。 app.UseDefaultFiles(); app.UseStaticFiles(); // NSwag 中间件(swagger 文档 + UI) app.UseOpenApi(); app.UseSwaggerUi(); app.UseCors("AllowAll"); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.MapHub("/hubs/notification"); // 前端使用 history 路由,未匹配到 API/静态文件的路径统一交给 index.html app.MapFallbackToFile("index.html"); app.Run();