dev #9
4
.env
Normal file
4
.env
Normal file
@ -0,0 +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}"
|
@ -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=Release
|
||||
ARG configuration=Debug
|
||||
WORKDIR /src
|
||||
COPY ["Cloud.csproj", "."]
|
||||
RUN dotnet restore "./Cloud.csproj"
|
||||
@ -25,6 +25,6 @@ RUN dotnet publish "./Cloud.csproj" -c $configuration -o /app/publish /p:UseAppH
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
# TODO: Добавить инициализацию базы данных с помощью миграции
|
||||
|
||||
ENTRYPOINT ["dotnet", "Cloud.dll"]
|
||||
|
||||
|
@ -13,6 +13,8 @@ using Cloud.Services;
|
||||
using Cloud.Services.Domain.Implement;
|
||||
using Cloud.Services.Domain;
|
||||
using Cloud.Services.Cache;
|
||||
using Cloud.Support;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@ -21,9 +23,10 @@ builder.Services.AddTransient<IBrokerService, KafkaService>();
|
||||
builder.Services.AddTransient<IGreenhouseService, GreenhouseService>();
|
||||
|
||||
//Redis configuration
|
||||
string redisUrl = Environment.GetEnvironmentVariable("REDIS_URL") ?? "localhost:6379";
|
||||
builder.Services.AddSingleton<IConnectionMultiplexer>(sp =>
|
||||
{
|
||||
var configuration = ConfigurationOptions.Parse("localhost:6379");
|
||||
var configuration = ConfigurationOptions.Parse(redisUrl);
|
||||
return ConnectionMultiplexer.Connect(configuration);
|
||||
});
|
||||
builder.Services.AddSingleton<IRedisCacheService, RedisCacheService>();
|
||||
@ -47,15 +50,21 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
};
|
||||
});
|
||||
|
||||
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<ApplicationContext>(options =>
|
||||
options.UseNpgsql("Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345"));
|
||||
options.UseNpgsql(connectionString));
|
||||
|
||||
// Настройка CORS
|
||||
string frontUrl = Environment.GetEnvironmentVariable("FRONT_URL") ?? "http://localhost:3000";
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowFrontendLocalhost", builder =>
|
||||
{
|
||||
builder.WithOrigins("http://localhost:3000") // фронтенд
|
||||
builder.WithOrigins(frontUrl) // фронтенд
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
|
26
Cloud/Support/NetworkSupport.cs
Normal file
26
Cloud/Support/NetworkSupport.cs
Normal file
@ -0,0 +1,26 @@
|
||||
namespace Cloud.Support;
|
||||
|
||||
public static class NetworkSupport
|
||||
{
|
||||
public static async Task CheckConnectionAsync(string address)
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
try
|
||||
{
|
||||
var response = await client.GetAsync(address);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
Console.WriteLine($"Соединение успешно проверено. Статус-код: {response.StatusCode}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Соединение не удалось проверить. Статус-код: {response.StatusCode}. URL: {address}");
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Console.WriteLine($"Ошибка при проверке соединения: {ex.Message}. URL: {address}");
|
||||
}
|
||||
}
|
||||
}
|
@ -12,6 +12,13 @@ services:
|
||||
build: ./Cloud/
|
||||
ports:
|
||||
- "5124:5124"
|
||||
environment:
|
||||
DB_CONNECTION_STRING: ${DB_CONNECTION_STRING}
|
||||
REDDIS_URL: redis:6379
|
||||
# На всякий случай, если будет больно
|
||||
# KAFKA_URL: kafka:9092
|
||||
# Добавить, когда будет фронт!
|
||||
# FRONT_URL: front:3000
|
||||
depends_on:
|
||||
- postgres
|
||||
- redis
|
||||
@ -19,9 +26,9 @@ services:
|
||||
image: postgres:14
|
||||
container_name: cucumber_database
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: 12345
|
||||
POSTGRES_DB: main_database
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
ports:
|
||||
- "5438:5432"
|
||||
volumes:
|
||||
|
Loading…
x
Reference in New Issue
Block a user