void.cat/VoidCat/Program.cs

76 lines
2.4 KiB
C#
Raw Normal View History

2022-02-16 16:33:00 +00:00
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
2022-02-10 16:36:53 +00:00
using Prometheus;
2022-02-16 23:19:31 +00:00
using StackExchange.Redis;
2022-01-25 17:57:07 +00:00
using VoidCat.Model;
using VoidCat.Services;
2022-02-16 16:33:00 +00:00
using VoidCat.Services.Abstractions;
2022-01-25 16:17:48 +00:00
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
2022-01-28 10:32:00 +00:00
var configuration = builder.Configuration;
var voidSettings = configuration.GetSection("Settings").Get<VoidSettings>();
services.AddSingleton(voidSettings);
2022-01-28 11:14:39 +00:00
var seqSettings = configuration.GetSection("Seq");
builder.Logging.AddSeq(seqSettings);
2022-01-28 10:32:00 +00:00
2022-02-16 23:19:31 +00:00
var useRedis = !string.IsNullOrEmpty(voidSettings.Redis);
if (useRedis)
{
var cx = await ConnectionMultiplexer.ConnectAsync(voidSettings.Redis);
services.AddSingleton(cx);
services.AddSingleton(cx.GetDatabase());
}
2022-01-25 16:17:48 +00:00
services.AddRouting();
2022-01-25 23:39:51 +00:00
services.AddControllers().AddNewtonsoftJson();
2022-02-16 16:33:00 +00:00
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new()
{
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = voidSettings.JwtSettings.Issuer,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(voidSettings.JwtSettings.Key))
};
});
2022-01-28 00:18:27 +00:00
2022-02-08 18:20:59 +00:00
services.AddScoped<IFileMetadataStore, LocalDiskFileMetadataStore>();
2022-02-16 16:33:00 +00:00
services.AddScoped<IFileStore, LocalDiskFileStore>();
2022-02-16 23:19:31 +00:00
services.AddScoped<IAggregateStatsCollector, AggregateStatsCollector>();
2022-02-10 16:36:53 +00:00
services.AddScoped<IStatsCollector, PrometheusStatsCollector>();
2022-02-16 23:19:31 +00:00
if (useRedis)
{
services.AddScoped<RedisStatsController>();
services.AddScoped<IStatsCollector>(svc => svc.GetRequiredService<RedisStatsController>());
services.AddScoped<IStatsReporter>(svc => svc.GetRequiredService<RedisStatsController>());
}
else
{
services.AddMemoryCache();
services.AddScoped<InMemoryStatsController>();
services.AddScoped<IStatsReporter>(svc => svc.GetRequiredService<InMemoryStatsController>());
services.AddScoped<IStatsCollector>(svc => svc.GetRequiredService<InMemoryStatsController>());
}
2022-01-25 16:17:48 +00:00
var app = builder.Build();
app.UseStaticFiles();
2022-02-16 16:33:00 +00:00
app.UseAuthentication();
2022-01-25 16:17:48 +00:00
app.UseRouting();
2022-02-16 16:33:00 +00:00
app.UseAuthorization();
2022-01-25 16:17:48 +00:00
app.UseEndpoints(ep =>
{
ep.MapControllers();
2022-02-10 16:36:53 +00:00
ep.MapMetrics();
2022-01-25 16:17:48 +00:00
ep.MapFallbackToFile("index.html");
});
app.Run();