71 lines
2.2 KiB
C#
71 lines
2.2 KiB
C#
using IM.InitCommon.Management;
|
|
|
|
using IM.InitCommon;
|
|
using Microsoft.Extensions.FileProviders;
|
|
|
|
namespace FileService.WebApi
|
|
{
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.ConfigureDbConfiguration();
|
|
|
|
builder.Services.AddControllers();
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
builder.ConfigExtraServices();
|
|
|
|
var app = builder.Build();
|
|
if (app.ApplyMigrationsIfRequested(args)) return;
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseAppDefault();
|
|
app.MapManagementHealth();
|
|
|
|
// 仅 FileService 暴露公开桶目录为静态直链,不动共享 UseAppDefault
|
|
UsePublicStaticFiles(app);
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
}
|
|
|
|
private static void UsePublicStaticFiles(WebApplication app)
|
|
{
|
|
var storage = app.Configuration.GetSection("StorageOptions").Get<StorageOptions>();
|
|
if (storage?.Providers == null ||
|
|
!storage.Providers.TryGetValue(storage.DefaultProviderCode, out var provider))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(provider.LocalRootPath) || string.IsNullOrEmpty(provider.PublicBucket))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var publicPath = Path.Combine(provider.LocalRootPath, provider.PublicBucket);
|
|
Directory.CreateDirectory(publicPath);
|
|
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
{
|
|
FileProvider = new PhysicalFileProvider(publicPath),
|
|
RequestPath = "/static",
|
|
OnPrepareResponse = ctx =>
|
|
ctx.Context.Response.Headers["Cache-Control"] = "public,max-age=2592000"
|
|
});
|
|
}
|
|
}
|
|
}
|