0
0
RhSolutions-Api/RhSolutions.Api/Program.cs

98 lines
3.1 KiB
C#
Raw Normal View History

2022-12-14 09:53:10 +03:00
using Microsoft.EntityFrameworkCore;
2023-05-11 07:55:26 +03:00
using RhSolutions.Models;
2022-12-14 09:53:10 +03:00
using RhSolutions.Api.Services;
2023-09-19 14:56:55 +03:00
using RhSolutions.Api.Middleware;
2024-02-08 17:11:11 +03:00
using RhSolutions.Parsers;
2024-01-14 23:03:52 +03:00
using Microsoft.OpenApi.Models;
using System.Reflection;
2024-02-07 16:32:34 +03:00
using Microsoft.AspNetCore.Identity;
using System.Text;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System.Security.Claims;
2022-12-14 09:53:10 +03:00
var builder = WebApplication.CreateBuilder(args);
2024-02-07 16:32:34 +03:00
ConnectionStringsUtil connectionStringsUtil = new(builder.Configuration);
2022-12-14 09:53:10 +03:00
builder.Services.AddDbContext<RhSolutionsContext>(opts =>
{
2024-02-07 16:32:34 +03:00
opts.UseNpgsql(connectionStringsUtil.GetRhDbString());
2022-12-14 09:53:10 +03:00
if (builder.Environment.IsDevelopment())
{
opts.EnableSensitiveDataLogging(true);
}
});
2024-02-07 16:32:34 +03:00
builder.Services.AddDbContext<IdentityContext>(opts =>
{
opts.UseNpgsql(connectionStringsUtil.GetIdentityDbString());
});
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<IdentityContext>();
2023-09-19 14:56:55 +03:00
builder.Services.AddScoped<IPricelistParser, ClosedXMLParser>()
2023-12-30 14:43:25 +03:00
.AddScoped<IProductTypePredicter, ProductTypePredicter>();
2024-01-26 15:50:41 +03:00
builder.Services.AddModifiers();
2022-12-14 09:53:10 +03:00
builder.Services.AddControllers();
2024-01-14 23:03:52 +03:00
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "RhSolutions API",
Description = "API к базе данных артикулов РЕХАУ для поиска с помощью ML.NET и полнотестового поиска PostgreSQL",
Contact = new OpenApiContact
{
Name = "Serghei Cebotari",
Url = new Uri("https://cebotari.ru/"),
Email = @"serghei@cebotari.ru"
}
2024-02-07 16:32:34 +03:00
});
2024-01-14 23:03:52 +03:00
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
2024-02-07 16:32:34 +03:00
builder.Services.AddAuthentication()
.AddJwtBearer(opts =>
{
opts.RequireHttpsMetadata = false;
opts.SaveToken = true;
opts.TokenValidationParameters = new()
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.ASCII.GetBytes(builder.Configuration["JWT_SECRET"] ?? "mold-smartness-arrive-overstate-aspirin")),
ValidateAudience = false,
ValidateIssuer = false
};
opts.Events = new JwtBearerEvents()
{
OnTokenValidated = async context =>
{
var userManager = context.HttpContext.RequestServices
.GetRequiredService<UserManager<IdentityUser>>();
var signInManager = context.HttpContext.RequestServices
.GetRequiredService<SignInManager<IdentityUser>>();
string username = context.Principal!.FindFirst(ClaimTypes.Name)!.Value;
IdentityUser? idUser = await userManager.FindByNameAsync(username);
context.Principal = await signInManager.CreateUserPrincipalAsync(idUser!);
}
};
});
2024-01-14 16:03:42 +03:00
2022-12-14 09:53:10 +03:00
var app = builder.Build();
app.MapControllers();
2023-09-19 14:56:55 +03:00
app.UseMiddleware<QueryModifier>();
2024-01-14 16:03:42 +03:00
app.UseSwagger().UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty;
});
2024-02-07 16:32:34 +03:00
app.UseAuthentication();
app.UseAuthorization();
IdentitySeedData.CreateAdminAccount(app.Services, app.Configuration);
2022-12-14 09:53:10 +03:00
app.Run();