2024-10-28 02:09:55 +04:00
|
|
|
using Cloud;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-10-28 18:39:13 +04:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
using System.Text;
|
|
|
|
using FluentValidation;
|
|
|
|
using FluentValidation.AspNetCore;
|
|
|
|
using Cloud.Validation;
|
2024-11-10 13:35:23 +04:00
|
|
|
using StackExchange.Redis;
|
2024-11-13 03:33:03 +04:00
|
|
|
using Cloud.Services.Broker.Implement.Kafka;
|
|
|
|
using Cloud.Services.Broker;
|
2024-11-13 15:24:47 +04:00
|
|
|
using Cloud.Services;
|
2024-12-04 01:57:29 +04:00
|
|
|
using Cloud.Services.Domain.Implement;
|
|
|
|
using Cloud.Services.Domain;
|
2024-12-01 19:33:52 +04:00
|
|
|
using Cloud.Services.Cache;
|
2024-12-04 04:06:56 +04:00
|
|
|
using Cloud.Support;
|
|
|
|
using System.Text.RegularExpressions;
|
2024-12-04 06:27:39 +04:00
|
|
|
using Cloud.Middlewares;
|
2024-10-28 02:09:55 +04:00
|
|
|
|
2024-10-28 00:25:51 +04:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
// Add services to the container.
|
2024-12-04 11:32:34 +04:00
|
|
|
builder.Services.AddSingleton<IBrokerService, KafkaService>();
|
2024-12-04 01:57:29 +04:00
|
|
|
builder.Services.AddTransient<IGreenhouseService, GreenhouseService>();
|
2024-10-28 00:25:51 +04:00
|
|
|
|
2024-11-10 13:35:23 +04:00
|
|
|
//Redis configuration
|
2024-12-04 04:06:56 +04:00
|
|
|
string redisUrl = Environment.GetEnvironmentVariable("REDIS_URL") ?? "localhost:6379";
|
2024-11-10 13:35:23 +04:00
|
|
|
builder.Services.AddSingleton<IConnectionMultiplexer>(sp =>
|
|
|
|
{
|
2024-12-04 04:06:56 +04:00
|
|
|
var configuration = ConfigurationOptions.Parse(redisUrl);
|
2024-11-10 13:35:23 +04:00
|
|
|
return ConnectionMultiplexer.Connect(configuration);
|
|
|
|
});
|
2024-12-01 19:33:52 +04:00
|
|
|
builder.Services.AddSingleton<IRedisCacheService, RedisCacheService>();
|
2024-11-10 13:35:23 +04:00
|
|
|
|
|
|
|
//Jwt configuration
|
2024-10-28 18:39:13 +04:00
|
|
|
var jwtIssuer = builder.Configuration.GetSection("Jwt:Issuer").Get<string>();
|
|
|
|
var jwtKey = builder.Configuration.GetSection("Jwt:Key").Get<string>();
|
|
|
|
|
|
|
|
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))
|
|
|
|
};
|
|
|
|
});
|
2024-12-04 06:27:39 +04:00
|
|
|
// Настройка подключения к БД
|
|
|
|
builder.Services.AddDbConnectionService();
|
2024-10-30 15:33:08 +04:00
|
|
|
// Настройка CORS
|
2024-12-04 04:06:56 +04:00
|
|
|
string frontUrl = Environment.GetEnvironmentVariable("FRONT_URL") ?? "http://localhost:3000";
|
2024-10-30 15:33:08 +04:00
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
{
|
|
|
|
options.AddPolicy("AllowFrontendLocalhost", builder =>
|
|
|
|
{
|
2024-12-04 04:06:56 +04:00
|
|
|
builder.WithOrigins(frontUrl) // фронтенд
|
2024-10-30 15:33:08 +04:00
|
|
|
.AllowAnyHeader()
|
|
|
|
.AllowAnyMethod();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-10-28 00:25:51 +04:00
|
|
|
builder.Services.AddControllers();
|
2024-10-28 18:39:13 +04:00
|
|
|
builder.Services.AddFluentValidationAutoValidation();
|
|
|
|
builder.Services.AddFluentValidationClientsideAdapters();
|
|
|
|
builder.Services.AddValidatorsFromAssemblyContaining<LoginValidator>();
|
|
|
|
builder.Services.AddValidatorsFromAssemblyContaining<RegisterValidator>();
|
2024-10-29 00:38:46 +04:00
|
|
|
builder.Services.AddValidatorsFromAssemblyContaining<FarmValidator>();
|
2024-11-13 15:24:47 +04:00
|
|
|
builder.Services.AddValidatorsFromAssemblyContaining<ValveValidator>();
|
2024-10-28 18:39:13 +04:00
|
|
|
|
2024-10-28 00:25:51 +04:00
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
2024-10-28 18:39:13 +04:00
|
|
|
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[] {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2024-10-28 00:25:51 +04:00
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
{
|
2024-12-04 06:27:39 +04:00
|
|
|
Console.WriteLine("Swagger enabled");
|
2024-10-28 00:25:51 +04:00
|
|
|
app.UseSwagger();
|
2024-10-28 18:39:13 +04:00
|
|
|
app.UseSwaggerUI(c =>
|
|
|
|
{
|
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Cloud API V1");
|
|
|
|
c.RoutePrefix = string.Empty;
|
|
|
|
});
|
2024-10-28 00:25:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
2024-10-30 15:33:08 +04:00
|
|
|
// Включение CORS
|
|
|
|
app.UseCors("AllowFrontendLocalhost");
|
|
|
|
|
2024-12-04 06:27:39 +04:00
|
|
|
// Применение миграций
|
|
|
|
app.MigrateDb();
|
|
|
|
|
2024-10-28 18:39:13 +04:00
|
|
|
app.UseAuthentication();
|
|
|
|
|
2024-10-28 00:25:51 +04:00
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
app.Run();
|