fix: есть контакт! между облаком и кафкой
This commit is contained in:
parent
2e20e9d7cc
commit
739d434f53
2
.env
2
.env
@ -1,4 +1,4 @@
|
|||||||
POSTGRES_USER="postgres"
|
POSTGRES_USER="postgres"
|
||||||
POSTGRES_PASSWORD="12345"
|
POSTGRES_PASSWORD="12345"
|
||||||
POSTGRES_DB="main_database"
|
POSTGRES_DB="main_database"
|
||||||
DB_CONNECTION_STRING="Host=postgres:5438;Database=${POSTGRES_DB};Username=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}"
|
DB_CONNECTION_STRING="Host=postgres:5432;Database=${POSTGRES_DB};Username=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}"
|
@ -28,6 +28,7 @@ namespace Cloud.Controllers
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var greenhouses = _greenhouseService.GetAll(farmId);
|
var greenhouses = _greenhouseService.GetAll(farmId);
|
||||||
|
if (greenhouses == null) return NotFound("Greenhouses is not found");
|
||||||
return Ok(greenhouses);
|
return Ok(greenhouses);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@ -10,7 +10,7 @@ RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /
|
|||||||
USER appuser
|
USER appuser
|
||||||
|
|
||||||
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
|
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
|
||||||
ARG configuration=Debug
|
ARG configuration=Development
|
||||||
WORKDIR /src
|
WORKDIR /src
|
||||||
COPY ["Cloud.csproj", "."]
|
COPY ["Cloud.csproj", "."]
|
||||||
RUN dotnet restore "./Cloud.csproj"
|
RUN dotnet restore "./Cloud.csproj"
|
||||||
@ -19,7 +19,7 @@ WORKDIR "/src/."
|
|||||||
RUN dotnet build "./Cloud.csproj" -c $configuration -o /app/build
|
RUN dotnet build "./Cloud.csproj" -c $configuration -o /app/build
|
||||||
|
|
||||||
FROM build AS publish
|
FROM build AS publish
|
||||||
ARG configuration=Release
|
ARG configuration=Development
|
||||||
RUN dotnet publish "./Cloud.csproj" -c $configuration -o /app/publish /p:UseAppHost=false
|
RUN dotnet publish "./Cloud.csproj" -c $configuration -o /app/publish /p:UseAppHost=false
|
||||||
|
|
||||||
FROM base AS final
|
FROM base AS final
|
||||||
|
30
Cloud/Middlewares/DatabaseMiddleware.cs
Normal file
30
Cloud/Middlewares/DatabaseMiddleware.cs
Normal file
@ -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<ApplicationContext>(options =>
|
||||||
|
options.UseNpgsql(connectionString));
|
||||||
|
|
||||||
|
}
|
||||||
|
public static void MigrateDb(this IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var scope = app.ApplicationServices.CreateScope();
|
||||||
|
var context = scope.ServiceProvider.GetRequiredService<ApplicationContext>();
|
||||||
|
|
||||||
|
context.Database.Migrate();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@ using Cloud.Services.Domain;
|
|||||||
using Cloud.Services.Cache;
|
using Cloud.Services.Cache;
|
||||||
using Cloud.Support;
|
using Cloud.Support;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using Cloud.Middlewares;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@ -49,15 +50,8 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|||||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
// Настройка подключения к БД
|
||||||
string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")
|
builder.Services.AddDbConnectionService();
|
||||||
?? "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<ApplicationContext>(options =>
|
|
||||||
options.UseNpgsql(connectionString));
|
|
||||||
|
|
||||||
// Настройка CORS
|
// Настройка CORS
|
||||||
string frontUrl = Environment.GetEnvironmentVariable("FRONT_URL") ?? "http://localhost:3000";
|
string frontUrl = Environment.GetEnvironmentVariable("FRONT_URL") ?? "http://localhost:3000";
|
||||||
builder.Services.AddCors(options =>
|
builder.Services.AddCors(options =>
|
||||||
@ -113,6 +107,7 @@ var app = builder.Build();
|
|||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("Swagger enabled");
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI(c =>
|
app.UseSwaggerUI(c =>
|
||||||
{
|
{
|
||||||
@ -126,6 +121,9 @@ app.UseHttpsRedirection();
|
|||||||
// Включение CORS
|
// Включение CORS
|
||||||
app.UseCors("AllowFrontendLocalhost");
|
app.UseCors("AllowFrontendLocalhost");
|
||||||
|
|
||||||
|
// Применение миграций
|
||||||
|
app.MigrateDb();
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
|
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
@ -13,7 +13,8 @@ namespace Cloud.Services.Broker.Implement.Kafka
|
|||||||
public KafkaConsumer(IConfiguration config)
|
public KafkaConsumer(IConfiguration config)
|
||||||
{
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
ChangeBrokerIp(_config["Kafka:BootstrapServers"]);
|
Console.WriteLine($"KafkaConsumer created. IP:" + _config["KAFKA_URL"]);
|
||||||
|
ChangeBrokerIp(_config["KAFKA_URL"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<T>? WaitMessages<T>(string topic)
|
public IEnumerable<T>? WaitMessages<T>(string topic)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
using System.Text.Json;
|
||||||
using Cloud.Services.Broker.Support;
|
using Cloud.Services.Broker.Support;
|
||||||
using Confluent.Kafka;
|
using Confluent.Kafka;
|
||||||
|
|
||||||
@ -5,18 +6,19 @@ namespace Cloud.Services.Broker.Implement.Kafka
|
|||||||
{
|
{
|
||||||
public class KafkaProducer : IBrokerProducer
|
public class KafkaProducer : IBrokerProducer
|
||||||
{
|
{
|
||||||
private IProducer<Guid, Command> _producer;
|
private IProducer<string, string> _producer;
|
||||||
private readonly IConfiguration _config;
|
private readonly IConfiguration _config;
|
||||||
|
|
||||||
public KafkaProducer(IConfiguration configuration)
|
public KafkaProducer(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_config = configuration;
|
_config = configuration;
|
||||||
|
Console.WriteLine($"KafkaConsumer created. IP:" + _config["KAFKA_URL"]);
|
||||||
ChangeBrokerIp(_config["Kafka:BootstrapServers"]);
|
ChangeBrokerIp(_config["KAFKA_URL"]);
|
||||||
}
|
}
|
||||||
public async Task ProduceAsync(string topic, Command command)
|
public async Task ProduceAsync(string topic, Command command)
|
||||||
{
|
{
|
||||||
var message = new Message<Guid, Command> { Key = Guid.NewGuid(), Value = command };
|
var commandSerialized = JsonSerializer.Serialize(command);
|
||||||
|
var message = new Message<string, string> { Key = Guid.NewGuid().ToString(), Value = commandSerialized };
|
||||||
|
|
||||||
//Produce the Message
|
//Produce the Message
|
||||||
await _producer.ProduceAsync(topic, message);
|
await _producer.ProduceAsync(topic, message);
|
||||||
@ -30,7 +32,7 @@ namespace Cloud.Services.Broker.Implement.Kafka
|
|||||||
};
|
};
|
||||||
|
|
||||||
//Build the Producer
|
//Build the Producer
|
||||||
_producer = new ProducerBuilder<Guid, Command>(producerConfig).Build();
|
_producer = new ProducerBuilder<string, string>(producerConfig).Build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -47,13 +47,13 @@ public class GreenhouseService : IGreenhouseService
|
|||||||
|
|
||||||
public async Task<IEnumerable<GreenhouseInfo>?> GetAll(int farmId)
|
public async Task<IEnumerable<GreenhouseInfo>?> GetAll(int farmId)
|
||||||
{
|
{
|
||||||
await _changeBrokerIp(farmId);
|
// await _changeBrokerIp(farmId);
|
||||||
return _brokerService.WaitMessages<GreenhouseInfo>("data");
|
return _brokerService.WaitMessages<GreenhouseInfo>("data");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<GreenhouseInfo?> GetGreenhouseInfo(int id, int farmId)
|
public async Task<GreenhouseInfo?> GetGreenhouseInfo(int id, int farmId)
|
||||||
{
|
{
|
||||||
await _changeBrokerIp(farmId);
|
// await _changeBrokerIp(farmId);
|
||||||
var infos = _brokerService.WaitMessages<GreenhouseInfo>("data");
|
var infos = _brokerService.WaitMessages<GreenhouseInfo>("data");
|
||||||
return infos?.FirstOrDefault(x => x.Id == id);
|
return infos?.FirstOrDefault(x => x.Id == id);
|
||||||
}
|
}
|
||||||
|
@ -9,14 +9,16 @@ networks:
|
|||||||
|
|
||||||
services:
|
services:
|
||||||
cloud:
|
cloud:
|
||||||
|
networks:
|
||||||
|
- vpn
|
||||||
build: ./Cloud/
|
build: ./Cloud/
|
||||||
ports:
|
ports:
|
||||||
- "5124:5124"
|
- "5124:5124"
|
||||||
environment:
|
environment:
|
||||||
|
ASPNETCORE_ENVIRONMENT: Development
|
||||||
DB_CONNECTION_STRING: ${DB_CONNECTION_STRING}
|
DB_CONNECTION_STRING: ${DB_CONNECTION_STRING}
|
||||||
REDDIS_URL: redis:6379
|
REDDIS_URL: redis:6379
|
||||||
# На всякий случай, если будет больно
|
KAFKA_URL: kafka:29092
|
||||||
# KAFKA_URL: kafka:9092
|
|
||||||
# Добавить, когда будет фронт!
|
# Добавить, когда будет фронт!
|
||||||
# FRONT_URL: front:3000
|
# FRONT_URL: front:3000
|
||||||
depends_on:
|
depends_on:
|
||||||
|
Loading…
Reference in New Issue
Block a user