| | |
| | | using Microsoft.Extensions.Hosting.WindowsServices; |
| | | using APIGetway.Filter; |
| | | |
| | | var builder = WebApplication.CreateBuilder(new WebApplicationOptions |
| | | { |
| | | Args = args, |
| | | ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : null |
| | | // IIS 进程内托管时工作目录不是站点目录,固定为程序目录才能读到 appsettings.json |
| | | ContentRootPath = AppContext.BaseDirectory |
| | | }); |
| | | |
| | | builder.Services.AddWindowsService(options => options.ServiceName = "APIGetway"); |
| | | |
| | | builder.Services.AddCors(options => |
| | | { |
| | | options.AddPolicy("frontend-dev", policy => policy |
| | | .WithOrigins("http://192.168.0.102:32976") // 换成前端真实地址+端口 |
| | | .WithMethods("GET", "POST", "PUT", "DELETE") |
| | | .WithHeaders("Content-Type", "X-Username", "Authorization")); |
| | | }); |
| | | |
| | | // YARP 反向代理 |
| | | builder.Services.AddReverseProxy() |
| | |
| | | |
| | | var app = builder.Build(); |
| | | |
| | | // 拦截对 DLL/LOG 等敏感后缀的访问,直接返回 406(需在静态文件与反向代理之前) |
| | | app.UseFileExtensionFilter(); |
| | | |
| | | app.UseUpstreamErrorPageFilter(); |
| | | |
| | | app.UseDefaultFiles(); |
| | | app.UseStaticFiles(); |
| | | |
| | | // YARP 转发:匹配 /API_NW/ 前缀的请求转发到后端 |
| | | // 跨域中间件:YARP 路由带 CorsPolicy 时会往端点写 CORS 元数据, |
| | | // 缺了 UseCors 就会在请求时抛 "contains CORS metadata, but a middleware was not found" |
| | | app.UseCors(); |
| | | |
| | | // YARP 转发:按 appsettings.json 中的 Routes 规则转发到后端 |
| | | app.MapReverseProxy(); |
| | | |
| | | app.Run(); |