Fix build

This commit is contained in:
2025-12-31 18:01:29 +07:00
parent cef86a5025
commit 55f09c3091
3 changed files with 214 additions and 39 deletions

View File

@@ -12,6 +12,10 @@
<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageReference Include="DotNetEnv" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.1" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,31 +1,152 @@
using Aspire.Hosting;
using DotNetEnv;
using Microsoft.Extensions.Configuration;
// Detect running mode: IDE mode (appsettings + .env) or Vibe-kanban mode (env vars)
var isVibeKanbanMode = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TASK_ID"))
&& Environment.GetEnvironmentVariable("TASK_ID") != "DEFAULT";
string taskId;
int portOffset;
string taskSlot;
int apiPort;
int postgresPort;
int redisPort;
int orleansSiloPort;
int orleansGatewayPort;
int orleansDashboardPort;
string dbName;
string orleansDbName;
string postgresConnectionString;
string postgresOrleansConnectionString;
string redisConnectionString;
string influxDbUrl;
string influxDbToken;
if (isVibeKanbanMode)
{
// Vibe-kanban mode: Use environment variables directly
Console.WriteLine("🔧 Running in Vibe-kanban mode (using environment variables)");
taskId = Environment.GetEnvironmentVariable("TASK_ID") ?? "DEFAULT";
portOffset = int.Parse(Environment.GetEnvironmentVariable("PORT_OFFSET") ?? "0");
taskSlot = Environment.GetEnvironmentVariable("TASK_SLOT") ?? "1";
// Calculate ports based on task configuration
apiPort = 5000 + portOffset;
postgresPort = 5432 + portOffset;
redisPort = 6379 + portOffset;
// Calculate Orleans ports from TASK_SLOT
var taskSlotInt = int.Parse(taskSlot);
orleansSiloPort = 11111 + (taskSlotInt - 1) * 10;
orleansGatewayPort = 30000 + (taskSlotInt - 1) * 10;
orleansDashboardPort = 9999 + (taskSlotInt - 1);
// Database names
dbName = $"managing_{taskId.ToLower()}";
orleansDbName = $"orleans_{taskId.ToLower()}";
// Connection strings (using existing Docker containers managed by Docker Compose)
postgresConnectionString = $"Host=localhost;Port={postgresPort};Database={dbName};Username=postgres;Password=postgres";
postgresOrleansConnectionString = $"Host=localhost;Port={postgresPort};Database={orleansDbName};Username=postgres;Password=postgres";
redisConnectionString = $"localhost:{redisPort}";
// InfluxDB from environment or defaults
influxDbUrl = Environment.GetEnvironmentVariable("InfluxDb__Url") ?? "http://localhost:8086/";
influxDbToken = Environment.GetEnvironmentVariable("InfluxDb__Token") ?? "Fw2FPL2OwTzDHzSbR2Sd5xs0EKQYy00Q-hYKYAhr9cC1_q5YySONpxuf_Ck0PTjyUiF13xXmi__bu_pXH-H9zA==";
}
else
{
// IDE mode: Load .env file and optional appsettings.json
Console.WriteLine("🔧 Running in IDE mode (using environment variables, .env, and optional appsettings.json)");
// Load .env file if it exists (optional)
var enableEnvFile = Environment.GetEnvironmentVariable("ENABLE_ENV_FILE") != "false";
if (enableEnvFile)
{
var envFilePaths = new[]
{
Path.Combine(Directory.GetCurrentDirectory(), ".env"),
Path.Combine(AppContext.BaseDirectory, ".env"),
Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", ".env")),
Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", ".env")), // From AppHost to project root
};
string? loadedEnvPath = null;
foreach (var envPath in envFilePaths)
{
if (File.Exists(envPath))
{
try
{
Env.Load(envPath);
loadedEnvPath = envPath;
Console.WriteLine($"✅ Loaded .env file from: {envPath}");
break;
}
catch (Exception ex)
{
Console.WriteLine($"⚠️ Failed to load .env file from {envPath}: {ex.Message}");
}
}
}
}
// Build configuration from appsettings.json (optional) and environment variables
// appsettings.json is optional because environment variables and .env files take precedence
var configBuilder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
var configuration = configBuilder.Build();
// Read configuration values
taskId = configuration["TASK_ID"] ?? Environment.GetEnvironmentVariable("TASK_ID") ?? "DEFAULT";
portOffset = int.Parse(configuration["PORT_OFFSET"] ?? Environment.GetEnvironmentVariable("PORT_OFFSET") ?? "0");
taskSlot = configuration["TASK_SLOT"] ?? Environment.GetEnvironmentVariable("TASK_SLOT") ?? "1";
// Calculate ports based on task configuration
apiPort = 5000 + portOffset;
postgresPort = 5432 + portOffset;
redisPort = 6379 + portOffset;
// Calculate Orleans ports from TASK_SLOT
var taskSlotInt = int.Parse(taskSlot);
orleansSiloPort = 11111 + (taskSlotInt - 1) * 10;
orleansGatewayPort = 30000 + (taskSlotInt - 1) * 10;
orleansDashboardPort = 9999 + (taskSlotInt - 1);
// Database names
dbName = $"managing_{taskId.ToLower()}";
orleansDbName = $"orleans_{taskId.ToLower()}";
// Connection strings from configuration or defaults
postgresConnectionString = configuration["PostgreSql:ConnectionString"]
?? configuration["PostgreSql__ConnectionString"]
?? $"Host=localhost;Port={postgresPort};Database={dbName};Username=postgres;Password=postgres";
postgresOrleansConnectionString = configuration["PostgreSql:Orleans"]
?? configuration["PostgreSql__Orleans"]
?? $"Host=localhost;Port={postgresPort};Database={orleansDbName};Username=postgres;Password=postgres";
redisConnectionString = configuration["Redis:ConnectionString"]
?? configuration["Redis__ConnectionString"]
?? $"localhost:{redisPort}";
// InfluxDB from configuration
influxDbUrl = configuration["InfluxDb:Url"]
?? configuration["InfluxDb__Url"]
?? "http://localhost:8086/";
influxDbToken = configuration["InfluxDb:Token"]
?? configuration["InfluxDb__Token"]
?? "Fw2FPL2OwTzDHzSbR2Sd5xs0EKQYy00Q-hYKYAhr9cC1_q5YySONpxuf_Ck0PTjyUiF13xXmi__bu_pXH-H9zA==";
}
var builder = DistributedApplication.CreateBuilder(args);
// Get task-specific configuration from environment variables
var taskId = Environment.GetEnvironmentVariable("TASK_ID") ?? "DEFAULT";
var portOffset = int.Parse(Environment.GetEnvironmentVariable("PORT_OFFSET") ?? "0");
var taskSlot = Environment.GetEnvironmentVariable("TASK_SLOT") ?? "1";
// Calculate ports based on task configuration
var apiPort = 5000 + portOffset;
var postgresPort = 5432 + portOffset;
var redisPort = 6379 + portOffset;
// Calculate Orleans ports from TASK_SLOT
var taskSlotInt = int.Parse(taskSlot);
var orleansSiloPort = 11111 + (taskSlotInt - 1) * 10;
var orleansGatewayPort = 30000 + (taskSlotInt - 1) * 10;
var orleansDashboardPort = 9999 + (taskSlotInt - 1);
// Database names
var dbName = $"managing_{taskId.ToLower()}";
var orleansDbName = $"orleans_{taskId.ToLower()}";
// Connection strings (using existing Docker containers managed by Docker Compose)
var postgresConnectionString = $"Host=localhost;Port={postgresPort};Database={dbName};Username=postgres;Password=postgres";
var postgresOrleansConnectionString = $"Host=localhost;Port={postgresPort};Database={orleansDbName};Username=postgres;Password=postgres";
var redisConnectionString = $"localhost:{redisPort}";
// Add API project
var api = builder.AddProject("api", "../Managing.Api/Managing.Api.csproj")
@@ -39,8 +160,8 @@ var api = builder.AddProject("api", "../Managing.Api/Managing.Api.csproj")
.WithEnvironment("SILO_ROLE", "Trading")
.WithEnvironment("PostgreSql__ConnectionString", postgresConnectionString)
.WithEnvironment("PostgreSql__Orleans", postgresOrleansConnectionString)
.WithEnvironment("InfluxDb__Url", "http://localhost:8086/")
.WithEnvironment("InfluxDb__Token", "Fw2FPL2OwTzDHzSbR2Sd5xs0EKQYy00Q-hYKYAhr9cC1_q5YySONpxuf_Ck0PTjyUiF13xXmi__bu_pXH-H9zA==")
.WithEnvironment("InfluxDb__Url", influxDbUrl)
.WithEnvironment("InfluxDb__Token", influxDbToken)
.WithEnvironment("ORLEANS_SILO_PORT", orleansSiloPort.ToString())
.WithEnvironment("ORLEANS_GATEWAY_PORT", orleansGatewayPort.ToString())
.WithEnvironment("ORLEANS_DASHBOARD_PORT", orleansDashboardPort.ToString());
@@ -51,8 +172,8 @@ var workers = builder.AddProject("workers", "../Managing.Workers/Managing.Worker
.WithEnvironment("TASK_SLOT", taskSlot)
.WithEnvironment("ASPNETCORE_ENVIRONMENT", "Development")
.WithEnvironment("PostgreSql__ConnectionString", postgresConnectionString)
.WithEnvironment("InfluxDb__Url", "http://localhost:8086/")
.WithEnvironment("InfluxDb__Token", "Fw2FPL2OwTzDHzSbR2Sd5xs0EKQYy00Q-hYKYAhr9cC1_q5YySONpxuf_Ck0PTjyUiF13xXmi__bu_pXH-H9zA==");
.WithEnvironment("InfluxDb__Url", influxDbUrl)
.WithEnvironment("InfluxDb__Token", influxDbToken);
// Build and run
builder.Build().Run();