Fix swagger

This commit is contained in:
Халимов Рустам
2026-02-10 00:38:21 +03:00
parent ae69d85fe2
commit 65b86e718d
4 changed files with 42 additions and 39 deletions

View File

@@ -3,13 +3,14 @@
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PublishAot>true</PublishAot>
<PublishAot>false</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.76.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,4 +1,9 @@
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Nashel.Modules.Identity.Infrastructure;
using Nashel.Modules.Identity.Presentation.Endpoints;
var builder = WebApplication.CreateBuilder(args);
@@ -41,6 +46,27 @@ builder.Services.AddSwaggerGen(options =>
}
});
// Регистрация модуля Identity
builder.Services.AddIdentityModule(builder.Configuration);
// Настройка аутентификации
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JwtSettings:Issuer"],
ValidAudience = builder.Configuration["JwtSettings:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtSettings:Secret"] ?? "super_secret_key_change_me_please_this_is_for_development_only_12345"))
};
});
builder.Services.AddAuthorization();
var app = builder.Build();
// Настройка конвейера HTTP-запросов.
@@ -50,33 +76,14 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Nashel API v1");
// Чтобы Swagger UI открывался в корне (опционально)
// c.RoutePrefix = string.Empty;
});
}
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
// Включение аутентификации и авторизации
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
// Маппинг эндпоинтов модуля Identity
app.MapIdentityEndpoints();
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

View File

@@ -6,6 +6,9 @@
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=nashel;Username=postgres;Password=postgres"
},
"JwtSettings": {
"Secret": "super_secret_key_change_me_please_this_is_for_development_only_12345",
"Issuer": "Nashel",

View File

@@ -1,14 +1,15 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nashel.BuildingBlocks.Application.Abstractions;
using Nashel.BuildingBlocks.Application.Behaviors;
using Nashel.Modules.Identity.Application.Commands;
using Nashel.Modules.Identity.Domain.Repositories;
using Nashel.Modules.Identity.Domain.Services;
using Nashel.Modules.Identity.Infrastructure.Persistence;
using Nashel.Modules.Identity.Infrastructure.Repositories;
using Nashel.Modules.Identity.Infrastructure.Services;
using Nashel.Modules.Identity.Application.Commands;
namespace Nashel.Modules.Identity.Infrastructure;
@@ -19,27 +20,18 @@ public static class DependencyInjection
services.AddScoped<IAccountRepository, AccountRepository>();
services.AddScoped<IJwtTokenGenerator, JwtTokenGenerator>();
services.AddScoped<ICurrentUserService, CurrentUserService>();
services.AddHttpContextAccessor(); // Ensure available
services.AddHttpContextAccessor();
// Add MediatR (auto-scan for handlers in Application layer)
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssembly(typeof(RegisterUserCommand).Assembly);
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
});
// Add DbContext
services.AddDbContext<IdentityDbContext>(options =>
// Configure PostgreSQL inside Host or pass options builder
// Assume Host configures DbContext options or use connection string here
// If "NativeAOT" -> Npgsql DataSource approach is preferred in Host Program.cs.
// But standard AddDbContext works too for now.
// Let's assume connection string is provided in configuration or options.
// For now, simple AddDbContext
{
// options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"));
// This requires Microsoft.EntityFrameworkCore.Npgsql package in Infrastructure.
});
{
options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"));
});
return services;
}