diff --git a/.env b/.env index bff77a7..eafc6c7 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ POSTGRES_USER="postgres" POSTGRES_PASSWORD="12345" POSTGRES_DB="main_database" -DB_CONNECTION_STRING="Host=postgres:5438;Database=${POSTGRES_DB};Username=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}" \ No newline at end of file +DB_CONNECTION_STRING="Host=postgres:5432;Database=${POSTGRES_DB};Username=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}" \ No newline at end of file diff --git a/Cloud/Controllers/GreengouseController.cs b/Cloud/Controllers/GreengouseController.cs index 6f77c60..57448ea 100644 --- a/Cloud/Controllers/GreengouseController.cs +++ b/Cloud/Controllers/GreengouseController.cs @@ -28,6 +28,7 @@ namespace Cloud.Controllers try { var greenhouses = _greenhouseService.GetAll(farmId); + if (greenhouses == null) return NotFound("Greenhouses is not found"); return Ok(greenhouses); } catch (Exception ex) diff --git a/Cloud/Dockerfile b/Cloud/Dockerfile index 2f52ad4..b4c2152 100644 --- a/Cloud/Dockerfile +++ b/Cloud/Dockerfile @@ -10,7 +10,7 @@ RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser / USER appuser FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build -ARG configuration=Debug +ARG configuration=Development WORKDIR /src COPY ["Cloud.csproj", "."] RUN dotnet restore "./Cloud.csproj" @@ -19,7 +19,7 @@ WORKDIR "/src/." RUN dotnet build "./Cloud.csproj" -c $configuration -o /app/build FROM build AS publish -ARG configuration=Release +ARG configuration=Development RUN dotnet publish "./Cloud.csproj" -c $configuration -o /app/publish /p:UseAppHost=false FROM base AS final diff --git a/Cloud/Middlewares/DatabaseMiddleware.cs b/Cloud/Middlewares/DatabaseMiddleware.cs new file mode 100644 index 0000000..88091bf --- /dev/null +++ b/Cloud/Middlewares/DatabaseMiddleware.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; + +namespace Cloud.Middlewares; + +public static class DatabaseMiddleware +{ + public static void AddDbConnectionService(this IServiceCollection services) + { + string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") + ?? "Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345"; + + services.AddDbContext(options => + options.UseNpgsql(connectionString)); + + } + public static void MigrateDb(this IApplicationBuilder app) + { + try + { + using var scope = app.ApplicationServices.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + + context.Database.Migrate(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } +} \ No newline at end of file diff --git a/Cloud/Program.cs b/Cloud/Program.cs index e8eac1c..5daec1f 100644 --- a/Cloud/Program.cs +++ b/Cloud/Program.cs @@ -15,6 +15,7 @@ using Cloud.Services.Domain; using Cloud.Services.Cache; using Cloud.Support; using System.Text.RegularExpressions; +using Cloud.Middlewares; var builder = WebApplication.CreateBuilder(args); @@ -49,15 +50,8 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)) }; }); - -string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") - ?? "Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345"; -string dbUrl = "http://" + Regex.Match(connectionString, @"(?<=Host=)([^;]+)").Groups[1].Value; -await NetworkSupport.CheckConnectionAsync(dbUrl); - -builder.Services.AddDbContext(options => - options.UseNpgsql(connectionString)); - +// Настройка подключения к БД +builder.Services.AddDbConnectionService(); // Настройка CORS string frontUrl = Environment.GetEnvironmentVariable("FRONT_URL") ?? "http://localhost:3000"; builder.Services.AddCors(options => @@ -113,6 +107,7 @@ var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { + Console.WriteLine("Swagger enabled"); app.UseSwagger(); app.UseSwaggerUI(c => { @@ -126,6 +121,9 @@ app.UseHttpsRedirection(); // Включение CORS app.UseCors("AllowFrontendLocalhost"); +// Применение миграций +app.MigrateDb(); + app.UseAuthentication(); app.UseAuthorization(); diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs index d2fe34c..c6b0b30 100644 --- a/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaConsumer.cs @@ -13,7 +13,8 @@ namespace Cloud.Services.Broker.Implement.Kafka public KafkaConsumer(IConfiguration config) { _config = config; - ChangeBrokerIp(_config["Kafka:BootstrapServers"]); + Console.WriteLine($"KafkaConsumer created. IP:" + _config["KAFKA_URL"]); + ChangeBrokerIp(_config["KAFKA_URL"]); } public IEnumerable? WaitMessages(string topic) diff --git a/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs b/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs index 0391cc4..2a0f8c8 100644 --- a/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs +++ b/Cloud/Services/Broker/Implement/Kafka/KafkaProducer.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using Cloud.Services.Broker.Support; using Confluent.Kafka; @@ -5,18 +6,19 @@ namespace Cloud.Services.Broker.Implement.Kafka { public class KafkaProducer : IBrokerProducer { - private IProducer _producer; + private IProducer _producer; private readonly IConfiguration _config; public KafkaProducer(IConfiguration configuration) { _config = configuration; - - ChangeBrokerIp(_config["Kafka:BootstrapServers"]); + Console.WriteLine($"KafkaConsumer created. IP:" + _config["KAFKA_URL"]); + ChangeBrokerIp(_config["KAFKA_URL"]); } public async Task ProduceAsync(string topic, Command command) { - var message = new Message { Key = Guid.NewGuid(), Value = command }; + var commandSerialized = JsonSerializer.Serialize(command); + var message = new Message { Key = Guid.NewGuid().ToString(), Value = commandSerialized }; //Produce the Message await _producer.ProduceAsync(topic, message); @@ -30,7 +32,7 @@ namespace Cloud.Services.Broker.Implement.Kafka }; //Build the Producer - _producer = new ProducerBuilder(producerConfig).Build(); + _producer = new ProducerBuilder(producerConfig).Build(); } } } \ No newline at end of file diff --git a/Cloud/Services/Domain/Implement/GreenhouseService.cs b/Cloud/Services/Domain/Implement/GreenhouseService.cs index 8c2d8bc..f398a39 100644 --- a/Cloud/Services/Domain/Implement/GreenhouseService.cs +++ b/Cloud/Services/Domain/Implement/GreenhouseService.cs @@ -47,13 +47,13 @@ public class GreenhouseService : IGreenhouseService public async Task?> GetAll(int farmId) { - await _changeBrokerIp(farmId); + // await _changeBrokerIp(farmId); return _brokerService.WaitMessages("data"); } public async Task GetGreenhouseInfo(int id, int farmId) { - await _changeBrokerIp(farmId); + // await _changeBrokerIp(farmId); var infos = _brokerService.WaitMessages("data"); return infos?.FirstOrDefault(x => x.Id == id); } diff --git a/docker-compose.yml b/docker-compose.yml index 8608800..11c4650 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,14 +9,16 @@ networks: services: cloud: + networks: + - vpn build: ./Cloud/ ports: - "5124:5124" environment: + ASPNETCORE_ENVIRONMENT: Development DB_CONNECTION_STRING: ${DB_CONNECTION_STRING} REDDIS_URL: redis:6379 - # На всякий случай, если будет больно - # KAFKA_URL: kafka:9092 + KAFKA_URL: kafka:29092 # Добавить, когда будет фронт! # FRONT_URL: front:3000 depends_on: