From 33ef17f73dcb4ed4139ba0ff006eeac9a71f2f3c Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Sun, 4 Oct 2026 11:22:39 +0700 Subject: [PATCH] Support --migrate-only startup and secrets handling Add a Timeweb deployment guide and improve secret handling and migrations. - docs/timeweb-deployment.md: deployment and migration instructions for Timeweb. - .gitignore: ignore a secrets/ folder. - Govor.API.csproj: ensure secrets/** are not copied to output/publish. - Program.cs: add --migrate-only flag (removes it from args), skip Firebase init when migrating, accept Firebase credentials via CredentialsJson or CredentialsPath/GOOGLE_APPLICATION_CREDENTIALS, resolve relative paths against content root, apply pending EF Core migrations and exit while logging progress. Normal API startup behaviour unchanged (migrations are applied only with --migrate-only). --- .gitignore | 1 + Govor.API/Govor.API.csproj | 2 ++ Govor.API/Program.cs | 31 +++++++++++++++++-- docs/timeweb-deployment.md | 61 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 docs/timeweb-deployment.md diff --git a/.gitignore b/.gitignore index 4042b98..03dfe2d 100644 --- a/.gitignore +++ b/.gitignore @@ -46,6 +46,7 @@ appsettings.*.json *.log logs/ uploads/ +secrets/ ## Папки для публикации publish/ diff --git a/Govor.API/Govor.API.csproj b/Govor.API/Govor.API.csproj index 337c56d..efc281e 100644 --- a/Govor.API/Govor.API.csproj +++ b/Govor.API/Govor.API.csproj @@ -36,6 +36,8 @@ + + diff --git a/Govor.API/Program.cs b/Govor.API/Program.cs index 3c2032e..86f58fc 100644 --- a/Govor.API/Program.cs +++ b/Govor.API/Program.cs @@ -9,8 +9,10 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi; using Govor.Domain; +using Microsoft.EntityFrameworkCore; -var builder = WebApplication.CreateBuilder(args); +var migrateOnly = args.Contains("--migrate-only", StringComparer.Ordinal); +var builder = WebApplication.CreateBuilder(args.Where(arg => arg != "--migrate-only").ToArray()); var configuration = builder.Configuration; var services = builder.Services; @@ -20,11 +22,19 @@ builder.AddLogger();// Serilog builder.Configuration.AddJsonFile("configs/ban_usernames.json", optional: false, reloadOnChange: true); -if (configuration.GetValue("Firebase:Enabled", true)) +if (!migrateOnly && configuration.GetValue("Firebase:Enabled", true)) { + var credentialsJson = configuration["Firebase:CredentialsJson"]; + var credentialsPath = configuration["Firebase:CredentialsPath"] + ?? Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS") + ?? "secrets/firebase-adminsdk.json"; + var credential = !string.IsNullOrWhiteSpace(credentialsJson) + ? CredentialFactory.FromJson(credentialsJson).ToGoogleCredential() + : CredentialFactory.FromFile( + Path.GetFullPath(credentialsPath, builder.Environment.ContentRootPath)).ToGoogleCredential(); FirebaseApp.Create(new AppOptions() { - Credential = GoogleCredential.FromFile("secrets/firebase-adminsdk.json") + Credential = credential }); } @@ -125,6 +135,21 @@ if (!builder.Environment.IsDevelopment() && string.IsNullOrWhiteSpace(configurat var app = builder.Build(); +if (migrateOnly) +{ + await using (var scope = app.Services.CreateAsyncScope()) + { + var db = scope.ServiceProvider.GetRequiredService(); + var pending = (await db.Database.GetPendingMigrationsAsync()).ToArray(); + app.Logger.LogInformation("Applying {Count} pending database migrations: {Migrations}", + pending.Length, string.Join(", ", pending)); + await db.Database.MigrateAsync(); + app.Logger.LogInformation("Database migrations completed."); + } + await app.DisposeAsync(); + return; +} + if (app.Environment.IsDevelopment()) { app.UseSwagger(); diff --git a/docs/timeweb-deployment.md b/docs/timeweb-deployment.md new file mode 100644 index 0000000..9ef1db4 --- /dev/null +++ b/docs/timeweb-deployment.md @@ -0,0 +1,61 @@ +# Deployment to Timeweb App Platform + +Use the repository root as the build context and select the **Dockerfile** framework. +The Dockerfile publishes the API and starts `dotnet Govor.API.dll`, listening on port 8080. +Set the health check path to `/server/ping` and use the technical HTTPS domain from the dashboard. + +## Runtime variables + +- `ASPNETCORE_ENVIRONMENT=Production` +- `ASPNETCORE_HTTP_PORTS=8080` +- `ConnectionStrings__GovorDbContext=Host=;Port=5432;Database=;Username=;Password=` +- `JwtAccessOption__SecretKey=` +- `EncryptionOption__Secret=` +- `Firebase__Enabled=true` +- `Firebase__CredentialsJson=` (a secret runtime variable) + +Alternatively, mount the service-account file and set `Firebase__CredentialsPath` or +`GOOGLE_APPLICATION_CREDENTIALS` to its container path. Relative paths are resolved +against the API content root. Never put credentials into the image or Git repository. +Replace any Firebase private key that was previously committed to a public repository. + +Use the database private address only if the application is connected to the same +Timeweb private network. Use the database TLS settings supplied by Timeweb; do not +disable certificate validation. Keep existing encryption secrets when migrating +an existing database, because changing them can make existing encrypted values unreadable. + +## Apply migrations + +First check the target database and back up existing data. The latest group migration +removes a legacy column, consolidates duplicate memberships and removes orphan rows. + +Run this command **inside the deployed container/network**, with the same database +runtime variable as the API: + +```sh +dotnet Govor.API.dll --migrate-only +``` + +This applies pending EF Core migrations and exits without starting the HTTP server +or push worker. Firebase credentials are not required for this command. A failure +returns a nonzero exit status; do not start the deployment until it succeeds. +The normal API startup does not automatically modify the database schema. + +If using EF tooling from the source checkout instead, use version 10.x: + +```sh +dotnet ef database update --project Govor.Domain --startup-project Govor.API +``` + +For EF tooling, set `Firebase__Enabled=false` in the tooling process only, or supply +valid Firebase credentials. The database must be reachable from that process. + +## Verify + +Confirm that the app log reports `Now listening on: http://[::]:8080` (or an equivalent +8080 listener) and has no database errors. Request `https:///server/ping` and +expect HTTP 200. Then check an authenticated API request and SignalR connections. +An HTTP ping alone does not verify database schema or Firebase delivery. + +The API stores media under `uploads` on disk. Configure persistent storage before +relying on uploads surviving container replacement.