From 4da6c9026d2162f36b5243a2583f94a6725b432d Mon Sep 17 00:00:00 2001 From: Vladislav Ershov Date: Sat, 20 Dec 2025 00:57:16 +0500 Subject: [PATCH] adaptive connection string --- .github/workflows/docker-build-push.yml | 2 +- Meethub/Program.cs | 30 ++++++++++++++++++++++--- build/_build/Build.cs | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/.github/workflows/docker-build-push.yml b/.github/workflows/docker-build-push.yml index bd87f8d..e3f15e0 100644 --- a/.github/workflows/docker-build-push.yml +++ b/.github/workflows/docker-build-push.yml @@ -21,7 +21,7 @@ on: branches: - main - develop - - copilot/add-github-actions-for-docker + - ershov/deploy-test tags: - 'v*' diff --git a/Meethub/Program.cs b/Meethub/Program.cs index 8d78636..f1cb290 100644 --- a/Meethub/Program.cs +++ b/Meethub/Program.cs @@ -14,9 +14,14 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c => { c.OperationFilter(); }); -builder.Services.AddNpgsql( - builder.Configuration.GetConnectionString("DefaultConnection"), + +var connectionString = GetConnectionString(builder.Configuration); +builder.Services.AddNpgsql(connectionString, optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(DatabaseContext).Assembly.FullName)); + +// builder.Services.AddNpgsql( +// builder.Configuration.GetConnectionString("DefaultConnection"), +// optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(DatabaseContext).Assembly.FullName)); builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = BotApiKeyAuthenticationHandler.SchemeName; @@ -82,4 +87,23 @@ app.UseMiddleware(); app.UseAuthorization(); app.MapControllers().RequireAuthorization(); -app.Run(); \ No newline at end of file +app.Run(); + +static string GetConnectionString(IConfiguration configuration) +{ + var host = Environment.GetEnvironmentVariable("POSTGRES_HOST"); + var database = Environment.GetEnvironmentVariable("POSTGRES_DB"); + var user = Environment.GetEnvironmentVariable("POSTGRES_USER"); + var password = Environment.GetEnvironmentVariable("POSTGRES_PASSWORD"); + + // If all environment variables are present, use them (production/serverless) + if (!string.IsNullOrEmpty(host) && !string.IsNullOrEmpty(database) && + !string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(password)) + { + return $"Server={host};Database={database};User Id={user};Password={password};"; + } + + // Fall back to appsettings.json for local development + return configuration.GetConnectionString("DefaultConnection") + ?? throw new InvalidOperationException("Database connection string not configured"); +} \ No newline at end of file diff --git a/build/_build/Build.cs b/build/_build/Build.cs index 264bb53..50630ee 100644 --- a/build/_build/Build.cs +++ b/build/_build/Build.cs @@ -16,7 +16,7 @@ [GitHubActions( "docker-build-push", GitHubActionsImage.UbuntuLatest, - OnPushBranches = ["main", "develop", "copilot/add-github-actions-for-docker"], + OnPushBranches = ["main", "develop", "ershov/deploy-test"], OnPushTags = ["v*"], InvokedTargets = [nameof(PushDockerImage)], ImportSecrets = ["YC_REGISTRY_ID", "YC_SERVICE_ACCOUNT_KEY"],