Fix swagger
This commit is contained in:
@@ -3,13 +3,14 @@
|
|||||||
<TargetFramework>net10.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<PublishAot>true</PublishAot>
|
<PublishAot>false</PublishAot>
|
||||||
<InvariantGlobalization>true</InvariantGlobalization>
|
<InvariantGlobalization>true</InvariantGlobalization>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<NoWarn>$(NoWarn);1591</NoWarn>
|
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Grpc.AspNetCore" Version="2.76.0" />
|
<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" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
+31
-24
@@ -1,4 +1,9 @@
|
|||||||
|
using System.Text;
|
||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
using Nashel.Modules.Identity.Infrastructure;
|
||||||
|
using Nashel.Modules.Identity.Presentation.Endpoints;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
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();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Настройка конвейера HTTP-запросов.
|
// Настройка конвейера HTTP-запросов.
|
||||||
@@ -50,33 +76,14 @@ if (app.Environment.IsDevelopment())
|
|||||||
app.UseSwaggerUI(c =>
|
app.UseSwaggerUI(c =>
|
||||||
{
|
{
|
||||||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Nashel API v1");
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Nashel API v1");
|
||||||
// Чтобы Swagger UI открывался в корне (опционально)
|
|
||||||
// c.RoutePrefix = string.Empty;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var summaries = new[]
|
// Включение аутентификации и авторизации
|
||||||
{
|
app.UseAuthentication();
|
||||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
app.UseAuthorization();
|
||||||
};
|
|
||||||
|
|
||||||
app.MapGet("/weatherforecast", () =>
|
// Маппинг эндпоинтов модуля Identity
|
||||||
{
|
app.MapIdentityEndpoints();
|
||||||
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");
|
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
|
||||||
{
|
|
||||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"DefaultConnection": "Host=localhost;Database=nashel;Username=postgres;Password=postgres"
|
||||||
|
},
|
||||||
"JwtSettings": {
|
"JwtSettings": {
|
||||||
"Secret": "super_secret_key_change_me_please_this_is_for_development_only_12345",
|
"Secret": "super_secret_key_change_me_please_this_is_for_development_only_12345",
|
||||||
"Issuer": "Nashel",
|
"Issuer": "Nashel",
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Nashel.BuildingBlocks.Application.Abstractions;
|
using Nashel.BuildingBlocks.Application.Abstractions;
|
||||||
using Nashel.BuildingBlocks.Application.Behaviors;
|
using Nashel.BuildingBlocks.Application.Behaviors;
|
||||||
|
using Nashel.Modules.Identity.Application.Commands;
|
||||||
using Nashel.Modules.Identity.Domain.Repositories;
|
using Nashel.Modules.Identity.Domain.Repositories;
|
||||||
using Nashel.Modules.Identity.Domain.Services;
|
using Nashel.Modules.Identity.Domain.Services;
|
||||||
using Nashel.Modules.Identity.Infrastructure.Persistence;
|
using Nashel.Modules.Identity.Infrastructure.Persistence;
|
||||||
using Nashel.Modules.Identity.Infrastructure.Repositories;
|
using Nashel.Modules.Identity.Infrastructure.Repositories;
|
||||||
using Nashel.Modules.Identity.Infrastructure.Services;
|
using Nashel.Modules.Identity.Infrastructure.Services;
|
||||||
using Nashel.Modules.Identity.Application.Commands;
|
|
||||||
|
|
||||||
namespace Nashel.Modules.Identity.Infrastructure;
|
namespace Nashel.Modules.Identity.Infrastructure;
|
||||||
|
|
||||||
@@ -19,27 +20,18 @@ public static class DependencyInjection
|
|||||||
services.AddScoped<IAccountRepository, AccountRepository>();
|
services.AddScoped<IAccountRepository, AccountRepository>();
|
||||||
services.AddScoped<IJwtTokenGenerator, JwtTokenGenerator>();
|
services.AddScoped<IJwtTokenGenerator, JwtTokenGenerator>();
|
||||||
services.AddScoped<ICurrentUserService, CurrentUserService>();
|
services.AddScoped<ICurrentUserService, CurrentUserService>();
|
||||||
services.AddHttpContextAccessor(); // Ensure available
|
services.AddHttpContextAccessor();
|
||||||
|
|
||||||
// Add MediatR (auto-scan for handlers in Application layer)
|
|
||||||
services.AddMediatR(cfg =>
|
services.AddMediatR(cfg =>
|
||||||
{
|
{
|
||||||
cfg.RegisterServicesFromAssembly(typeof(RegisterUserCommand).Assembly);
|
cfg.RegisterServicesFromAssembly(typeof(RegisterUserCommand).Assembly);
|
||||||
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
|
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add DbContext
|
|
||||||
services.AddDbContext<IdentityDbContext>(options =>
|
services.AddDbContext<IdentityDbContext>(options =>
|
||||||
// Configure PostgreSQL inside Host or pass options builder
|
{
|
||||||
// Assume Host configures DbContext options or use connection string here
|
options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"));
|
||||||
// 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.
|
|
||||||
});
|
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user