Files
git.stella-ops.org/src/BinaryIndex/StellaOps.BinaryIndex.WebService/Program.cs

83 lines
2.9 KiB
C#

using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using StackExchange.Redis;
using StellaOps.BinaryIndex.Cache;
using StellaOps.BinaryIndex.Core.Resolution;
using StellaOps.BinaryIndex.VexBridge;
using StellaOps.BinaryIndex.WebService.Middleware;
using StellaOps.BinaryIndex.WebService.Services;
using StellaOps.Auth.ServerIntegration;
using StellaOps.BinaryIndex.WebService.Telemetry;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Configure options
builder.Services.Configure<ResolutionServiceOptions>(
builder.Configuration.GetSection(ResolutionServiceOptions.SectionName));
builder.Services.AddSingleton<IValidateOptions<ResolutionCacheOptions>, ResolutionCacheOptionsValidator>();
builder.Services.AddOptions<ResolutionCacheOptions>()
.Bind(builder.Configuration.GetSection(ResolutionCacheOptions.SectionName))
.ValidateOnStart();
// Add Redis/Valkey connection
var redisConnectionString = builder.Configuration.GetConnectionString("Redis") ?? "localhost:6379";
builder.Services.AddSingleton<IConnectionMultiplexer>(_ =>
ConnectionMultiplexer.Connect(redisConnectionString));
// Add services
builder.Services.TryAddSingleton(TimeProvider.System);
builder.Services.TryAddSingleton<IRandomSource, SystemRandomSource>();
builder.Services.AddSingleton<IResolutionCacheService, ResolutionCacheService>();
builder.Services.AddScoped<ResolutionService>();
builder.Services.AddScoped<IResolutionService>(sp =>
new CachedResolutionService(
sp.GetRequiredService<ResolutionService>(),
sp.GetRequiredService<IResolutionCacheService>(),
sp.GetRequiredService<IOptions<ResolutionCacheOptions>>(),
sp.GetRequiredService<IOptions<ResolutionServiceOptions>>(),
sp.GetRequiredService<TimeProvider>(),
sp.GetRequiredService<ILogger<CachedResolutionService>>()));
// Add VexBridge
builder.Services.AddBinaryVexBridge(builder.Configuration);
// Add telemetry
builder.Services.AddResolutionTelemetry();
// Add rate limiting
builder.Services.AddResolutionRateLimiting(options =>
builder.Configuration.GetSection("RateLimiting").Bind(options));
// Add health checks
builder.Services.AddHealthChecks()
.AddRedis(redisConnectionString, name: "redis");
builder.Services.AddStellaOpsCors(builder.Environment, builder.Configuration);
builder.TryAddStellaOpsLocalBinding("binaryindex");
var app = builder.Build();
app.LogStellaOpsLocalHostname("binaryindex");
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseStellaOpsCors();
app.UseHttpsRedirection();
app.UseResolutionRateLimiting();
app.UseAuthorization();
app.MapControllers();
app.MapHealthChecks("/health");
app.Run();