测试
This commit is contained in:
87
backend/HardwarePerformance.API/Program.cs
Normal file
87
backend/HardwarePerformance.API/Program.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System.IO.Compression;
|
||||
using HardwarePerformance.Infrastructure.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// 添加响应压缩服务
|
||||
builder.Services.AddResponseCompression(options =>
|
||||
{
|
||||
options.EnableForHttps = true;
|
||||
options.Providers.Add<BrotliCompressionProvider>();
|
||||
options.Providers.Add<GzipCompressionProvider>();
|
||||
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[]
|
||||
{
|
||||
"application/javascript",
|
||||
"application/json",
|
||||
"text/css",
|
||||
"text/html",
|
||||
"text/plain",
|
||||
"text/xml"
|
||||
});
|
||||
});
|
||||
|
||||
// 配置压缩级别
|
||||
builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
|
||||
{
|
||||
options.Level = CompressionLevel.Fastest;
|
||||
});
|
||||
|
||||
builder.Services.Configure<GzipCompressionProviderOptions>(options =>
|
||||
{
|
||||
options.Level = CompressionLevel.Fastest;
|
||||
});
|
||||
|
||||
// 配置Redis缓存
|
||||
builder.Services.AddStackExchangeRedisCache(options =>
|
||||
{
|
||||
options.Configuration = builder.Configuration.GetConnectionString("Redis") ?? "localhost:6379";
|
||||
options.InstanceName = "HardwarePerformance:";
|
||||
});
|
||||
|
||||
// 注册Redis缓存服务
|
||||
builder.Services.AddSingleton<IConnectionMultiplexer>(provider =>
|
||||
{
|
||||
var configuration = provider.GetRequiredService<IConfiguration>();
|
||||
var connectionString = configuration.GetConnectionString("Redis") ?? "localhost:6379";
|
||||
return ConnectionMultiplexer.Connect(connectionString);
|
||||
});
|
||||
|
||||
builder.Services.AddScoped<IRedisCacheService, RedisCacheService>();
|
||||
|
||||
// 配置CORS
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowAll", policy =>
|
||||
{
|
||||
policy.AllowAnyOrigin()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
// 添加响应压缩中间件
|
||||
app.UseResponseCompression();
|
||||
|
||||
app.UseCors("AllowAll");
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user