using Cloud; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; using System.Text; using FluentValidation; using FluentValidation.AspNetCore; using Cloud.Validation; using StackExchange.Redis; using Cloud.Services.Broker.Implement.Kafka; using Cloud.Services.Broker; using Cloud.Services; using Cloud.Services.Domain.Implement; using Cloud.Services.Domain; using Cloud.Services.Cache; using Cloud.Support; using System.Text.RegularExpressions; using Cloud.Middlewares; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddTransient(); builder.Services.AddTransient(); //Redis configuration string redisUrl = Environment.GetEnvironmentVariable("REDIS_URL") ?? "localhost:6379"; builder.Services.AddSingleton(sp => { var configuration = ConfigurationOptions.Parse(redisUrl); return ConnectionMultiplexer.Connect(configuration); }); builder.Services.AddSingleton(); //Jwt configuration var jwtIssuer = builder.Configuration.GetSection("Jwt:Issuer").Get(); var jwtKey = builder.Configuration.GetSection("Jwt:Key").Get(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = jwtIssuer, ValidAudience = jwtIssuer, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)) }; }); // Настройка подключения к БД builder.Services.AddDbConnectionService(); // Настройка CORS string frontUrl = Environment.GetEnvironmentVariable("FRONT_URL") ?? "http://localhost:3000"; builder.Services.AddCors(options => { options.AddPolicy("AllowFrontendLocalhost", builder => { builder.WithOrigins(frontUrl) // фронтенд .AllowAnyHeader() .AllowAnyMethod(); }); }); builder.Services.AddControllers(); builder.Services.AddFluentValidationAutoValidation(); builder.Services.AddFluentValidationClientsideAdapters(); builder.Services.AddValidatorsFromAssemblyContaining(); builder.Services.AddValidatorsFromAssemblyContaining(); builder.Services.AddValidatorsFromAssemblyContaining(); builder.Services.AddValidatorsFromAssemblyContaining(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "Cloud API", Version = "v1" }); c.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme { Description = "Введите ваш Bearer токен", Name = "Authorization", In = Microsoft.OpenApi.Models.ParameterLocation.Header, Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey }); c.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement { { new Microsoft.OpenApi.Models.OpenApiSecurityScheme { Reference = new Microsoft.OpenApi.Models.OpenApiReference { Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme, Id = "Bearer" } }, new string[] {} } }); }); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { Console.WriteLine("Swagger enabled"); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Cloud API V1"); c.RoutePrefix = string.Empty; }); } app.UseHttpsRedirection(); // Включение CORS app.UseCors("AllowFrontendLocalhost"); // Применение миграций app.MigrateDb(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();