void.cat/VoidCat/Program.cs

122 lines
3.5 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-21 09:39:59 +00:00
using Newtonsoft.Json;
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-02-21 09:39:59 +00:00
using VoidCat.Services.InMemory;
2022-02-17 15:52:49 +00:00
using VoidCat.Services.Migrations;
2022-02-21 12:54:57 +00:00
using VoidCat.Services.Paywall;
2022-02-21 09:39:59 +00:00
using VoidCat.Services.Redis;
2022-02-21 22:35:06 +00:00
using VoidCat.Services.Stats;
using VoidCat.Services.Users;
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-02-21 12:54:57 +00:00
services.AddSingleton(voidSettings.Strike ?? new());
2022-01-28 10:32:00 +00:00
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-02-21 09:39:59 +00:00
services.AddControllers().AddNewtonsoftJson((opt) =>
{
opt.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
2022-02-21 13:36:22 +00:00
opt.SerializerSettings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
opt.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
2022-02-21 09:39:59 +00:00
});
2022-02-21 22:35:06 +00:00
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-21 22:35:06 +00:00
services.AddAuthorization((opt) =>
{
opt.AddPolicy(Policies.RequireAdmin, (auth) =>
{
auth.RequireRole(Roles.Admin);
});
});
2022-02-17 15:52:49 +00:00
// void.cat services
2022-02-21 12:54:57 +00:00
//
2022-02-17 15:52:49 +00:00
services.AddVoidMigrations();
2022-02-21 12:54:57 +00:00
// file storage
services.AddTransient<IFileMetadataStore, LocalDiskFileMetadataStore>();
services.AddTransient<IFileStore, LocalDiskFileStore>();
// stats
services.AddTransient<IAggregateStatsCollector, AggregateStatsCollector>();
services.AddTransient<IStatsCollector, PrometheusStatsCollector>();
// paywall
services.AddVoidPaywall();
2022-02-21 22:35:06 +00:00
// users
services.AddTransient<IUserStore, UserStore>();
services.AddTransient<IUserManager, UserManager>();
2022-02-16 23:19:31 +00:00
if (useRedis)
{
2022-02-21 22:35:06 +00:00
services.AddTransient<ICache, RedisCache>();
2022-02-21 12:54:57 +00:00
services.AddTransient<RedisStatsController>();
services.AddTransient<IStatsCollector>(svc => svc.GetRequiredService<RedisStatsController>());
services.AddTransient<IStatsReporter>(svc => svc.GetRequiredService<RedisStatsController>());
2022-02-16 23:19:31 +00:00
}
else
{
services.AddMemoryCache();
2022-02-21 22:35:06 +00:00
services.AddTransient<ICache, InMemoryCache>();
2022-02-21 12:54:57 +00:00
services.AddTransient<InMemoryStatsController>();
services.AddTransient<IStatsReporter>(svc => svc.GetRequiredService<InMemoryStatsController>());
services.AddTransient<IStatsCollector>(svc => svc.GetRequiredService<InMemoryStatsController>());
2022-02-16 23:19:31 +00:00
}
2022-01-25 16:17:48 +00:00
var app = builder.Build();
2022-02-17 15:52:49 +00:00
// run migrations
var migrations = app.Services.GetServices<IMigration>();
foreach (var migration in migrations)
{
await migration.Migrate();
}
2022-01-25 16:17:48 +00:00
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");
});
2022-02-21 22:35:06 +00:00
app.Run();