Files
BabyVideoService/Http/HttpService.cs
2026-02-08 16:58:32 +08:00

75 lines
2.4 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
namespace ACBuildService;
public static class HttpService
{
public static WebApplication App { get; private set; }
public static readonly string BaseRootPath = $"{AppContext.BaseDirectory}wwwroot/";
public static async Task Start()
{
var builder = WebApplication.CreateBuilder();
// 添加控制器服务
builder.Services.AddControllers();
builder.WebHost.UseKestrel(options =>
{
//设置最大1G, 这里的单位是byte
options.Limits.MaxRequestBodySize = int.MaxValue;
// HTTP
if (ACBuildService.App.Settings.HttpPort > 0)
{
options.ListenAnyIP(ACBuildService.App.Settings.HttpPort);
}
})
.ConfigureLogging(logging => { logging.SetMinimumLevel(LogLevel.Error); });
builder.Services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
});
var contentTypeProvider = new FileExtensionContentTypeProvider();
contentTypeProvider.Mappings.Add(".bundle", "application/octet-stream");
contentTypeProvider.Mappings.Add(".meta", "application/octet-stream");
contentTypeProvider.Mappings.Add(".bytes", "application/octet-stream");
Log.Info($"wwwroot={BaseRootPath}");
App = builder.Build();
// 针对 files 文件夹配置
var fileServerOptions = new FileServerOptions
{
RequestPath = "/files",
EnableDirectoryBrowsing = true,
FileProvider = new PhysicalFileProvider(BaseRootPath),
StaticFileOptions =
{
ContentTypeProvider = contentTypeProvider
}
};
App.UseFileServer(fileServerOptions);
// 使用控制器路由
App.MapControllers();
// 不再需要手动映射路由
// App.MapGet("/", HttpHandler.Index);
// App.MapPost("/api", HttpHandler.Api);
await App.StartAsync();
Log.Info("http 服务启动成功");
}
}