-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
87 lines (69 loc) · 2.83 KB
/
Copy pathProgram.cs
File metadata and controls
87 lines (69 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using ArtReferenceAPI.Configuration;
using ArtReferenceAPI.Data;
using ArtReferenceAPI.Services;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.Configure<ImageSettings>(builder.Configuration.GetSection(ImageSettings.SectionName));
builder.Services.Configure<CacheSettings>(builder.Configuration.GetSection(CacheSettings.SectionName));
// Register new repository and filter service
builder.Services.AddScoped<IImageRepository, ImageRepository>();
builder.Services.AddSingleton<ImageFilterService>();
// Update existing service registration to use dependency injection
builder.Services.AddScoped<ImageService>();
builder.Services.AddMemoryCache();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAngularApp",
policy =>
{
policy.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseCors("AllowAngularApp");
app.UseAuthorization();
// Initialize image cache on startup
using (var scope = app.Services.CreateScope())
{
var imageService = scope.ServiceProvider.GetRequiredService<ImageService>();
_ = imageService.InitializeCacheAsync(); // Fire and forget initialization
}
// Serve static image files
var imageSettings = app.Services.GetRequiredService<IOptions<ImageSettings>>().Value;
if (!string.IsNullOrEmpty(imageSettings?.RootPath) &&
Directory.Exists(imageSettings.RootPath) &&
!string.IsNullOrEmpty(imageSettings.BaseServePath))
{
app.Logger.LogInformation("Serving static files from: {PhysicalPath} at URL base: {RequestPath}",
imageSettings.RootPath, imageSettings.BaseServePath);
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(imageSettings.RootPath),
RequestPath = imageSettings.BaseServePath
});
}
else
{
app.Logger.LogWarning("ImageSettings.RootPath is not configured correctly, directory does not exist, or BaseServePath is missing. Static images will not be served.");
app.Logger.LogWarning("Current settings - RootPath: '{RootPath}', BaseServePath: '{BaseServePath}', Directory Exists: {DirExists}",
imageSettings?.RootPath,
imageSettings?.BaseServePath,
(imageSettings != null && !string.IsNullOrEmpty(imageSettings.RootPath) ? Directory.Exists(imageSettings.RootPath).ToString() : "N/A (RootPath null/empty)"));
}
app.MapControllers();
app.Run();