using Microsoft.AspNetCore.StaticFiles; var builder = WebApplication.CreateBuilder(args); // 添加 CORS 策略 builder.Services.AddCors(options => { options.AddPolicy("AllowAll", builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); // 添加 HttpClient 工廠 builder.Services.AddHttpClient(); // 增加 HttpClient 工廠,並配置命名的 HttpClient builder.Services.AddHttpClient("VideoProcessingClient") .SetHandlerLifetime(TimeSpan.FromMinutes(5)) // 可選:設置處理程序的生命週期 .ConfigurePrimaryHttpMessageHandler(() => { // 可選:配置自定義的 HttpMessageHandler,例如 Proxy、SSL 等 return new HttpClientHandler(); }) .ConfigureHttpClient(client => { client.Timeout = TimeSpan.FromMinutes(10); // 設置超時為 10 分鐘 }); // Add services to the container. builder.Services.AddControllers(); // 設定最大請求體大小(例如,1 GB) builder.WebHost.ConfigureKestrel(options => { options.Limits.MaxRequestBodySize = 1073741824; // 1 GB options.ListenAnyIP(7860); // 只保留 HTTP 即可 //options.ListenAnyIP(7860, listenOptions => //{ // listenOptions.UseHttps(); // 監聽 HTTPS 8081 端口 //}); }); var app = builder.Build(); // 使用 CORS app.UseCors("AllowAll"); // 設定 `.onnx` 的 MIME 類型 var provider = new FileExtensionContentTypeProvider(); //provider.Mappings[".onnx"] = "application/octet-stream"; provider.Mappings[".wasm"] = "application/wasm"; // 為 WebAssembly 添加 MIME 類型 provider.Mappings[".data"] = "application/data"; // 為 WebAssembly 添加 MIME 類型 // 使用 DefaultFiles 中間件尋找 index.html app.UseDefaultFiles(); // 提供靜態文件,並使用自訂的 MIME 類型提供者與標頭 app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider, OnPrepareResponse = ctx => { Console.WriteLine($"Serving file: {ctx.File.PhysicalPath}"); var headers = ctx.Context.Response.Headers; headers["Cross-Origin-Opener-Policy"] = "same-origin"; headers["Cross-Origin-Embedder-Policy"] = "require-corp"; headers["Content-Security-Policy"] = "default-src 'self'; " + "script-src 'self' https://js.stripe.com https://m.stripe.network translate.googleapis.com translate.google.com www.google.com www.gstatic.com 'unsafe-inline'; " + "worker-src 'self' blob:; " + "connect-src 'self' https://api.stripe.com;"; } }); // Configure the HTTP request pipeline. app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();