2026-09-28 02:09:17 +04:00
|
|
|
using MemeVault.Api.Middleware;
|
2026-09-28 02:02:05 +04:00
|
|
|
using MemeVault.Api.Auth;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using MemeVault.Api.Options;
|
2026-09-28 01:47:01 +04:00
|
|
|
using MemeVault.Infrastructure;
|
2026-09-28 02:02:05 +04:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2026-09-28 02:18:57 +04:00
|
|
|
using MemeVault.Infrastructure.Persistence;
|
|
|
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
2026-09-28 02:02:05 +04:00
|
|
|
using Prometheus;
|
2026-09-28 01:47:01 +04:00
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
2026-09-28 02:02:05 +04:00
|
|
|
// ── Infrastructure (DB + Redis) ───────────────────────────────────────────────
|
|
|
|
|
builder.Services.AddInfrastructure(builder.Configuration);
|
|
|
|
|
|
|
|
|
|
// ── MediatR ───────────────────────────────────────────────────────────────────
|
2026-09-28 01:47:01 +04:00
|
|
|
builder.Services.AddMediatR(cfg =>
|
|
|
|
|
cfg.RegisterServicesFromAssembly(
|
|
|
|
|
typeof(MemeVault.Application.Commands.SaveMedia.SaveMediaCommand).Assembly));
|
|
|
|
|
|
2026-09-28 02:02:05 +04:00
|
|
|
// ── Telegram initData + JWT ───────────────────────────────────────────────────
|
|
|
|
|
builder.Services.Configure<TelegramOptions>(
|
|
|
|
|
builder.Configuration.GetSection(TelegramOptions.Section));
|
|
|
|
|
builder.Services.Configure<JwtOptions>(
|
|
|
|
|
builder.Configuration.GetSection(JwtOptions.Section));
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton<TelegramInitDataValidator>();
|
|
|
|
|
builder.Services.AddSingleton<JwtTokenService>();
|
2026-09-28 02:09:17 +04:00
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
|
builder.Services.AddScoped<CurrentUser>();
|
2026-09-28 02:02:05 +04:00
|
|
|
|
|
|
|
|
var jwtOpts = builder.Configuration.GetSection(JwtOptions.Section).Get<JwtOptions>()!;
|
|
|
|
|
|
|
|
|
|
builder.Services
|
|
|
|
|
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.AddJwtBearer(o =>
|
|
|
|
|
{
|
|
|
|
|
o.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
ValidIssuer = jwtOpts.Issuer,
|
|
|
|
|
ValidAudience = jwtOpts.Audience,
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(
|
|
|
|
|
Encoding.UTF8.GetBytes(jwtOpts.SecretKey)),
|
|
|
|
|
ClockSkew = TimeSpan.FromSeconds(30),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-28 02:09:17 +04:00
|
|
|
builder.Services.AddProblemDetails();
|
|
|
|
|
builder.Services.AddExceptionHandler<DomainExceptionHandler>();
|
2026-09-28 02:02:05 +04:00
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
|
2026-09-28 02:18:57 +04:00
|
|
|
// ── OpenAPI ───────────────────────────────────────────────────────────────────
|
|
|
|
|
builder.Services.AddHealthChecks()
|
|
|
|
|
.AddDbContextCheck<AppDbContext>("postgres");
|
|
|
|
|
|
2026-09-28 02:02:05 +04:00
|
|
|
// ── OpenAPI ───────────────────────────────────────────────────────────────────
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
2026-09-28 01:47:01 +04:00
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2026-09-28 02:09:17 +04:00
|
|
|
app.UseExceptionHandler();
|
2026-09-28 01:47:01 +04:00
|
|
|
app.UseHttpMetrics();
|
2026-09-28 02:02:05 +04:00
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
2026-09-28 01:47:01 +04:00
|
|
|
|
2026-09-28 02:18:57 +04:00
|
|
|
app.MapHealthChecks("/health");
|
2026-09-28 01:47:01 +04:00
|
|
|
|
2026-09-28 02:02:05 +04:00
|
|
|
app.MapControllers();
|
2026-09-28 01:47:01 +04:00
|
|
|
app.MapMetrics();
|
|
|
|
|
|
|
|
|
|
app.Run();
|