55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using AccessQueueService.Data;
|
|
using AccessQueueService.Services;
|
|
using Serilog;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add Serilog configuration from appsettings and serilog.json
|
|
builder.Host.UseSerilog((context, services, configuration) =>
|
|
{
|
|
configuration
|
|
.ReadFrom.Configuration(context.Configuration)
|
|
.ReadFrom.Services(services)
|
|
.Enrich.FromLogContext();
|
|
});
|
|
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
builder.Services.AddSingleton<IAccessService, AccessService>();
|
|
builder.Services.AddSingleton<AccessQueueRepository>(sp =>
|
|
{
|
|
string? filePath = builder.Configuration.GetValue<string>("AccessQueue:BackupFilePath");
|
|
if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath))
|
|
{
|
|
try
|
|
{
|
|
var json = File.ReadAllText(filePath);
|
|
return AccessQueueRepository.FromState(json);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Failed to load state from {filePath}. Error message: {ex.Message}");
|
|
}
|
|
}
|
|
return new AccessQueueRepository();
|
|
});
|
|
builder.Services.AddHostedService<AccessCleanupBackgroundService>();
|
|
builder.Services.AddHostedService<AccessQueueSerializerService>();
|
|
|
|
var app = builder.Build();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|