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).
This commit is contained in:
Artemy
2026-10-04 11:22:39 +07:00
parent b51482ea57
commit 33ef17f73d
4 changed files with 92 additions and 3 deletions
+2
View File
@@ -36,6 +36,8 @@
<ItemGroup>
<Folder Include="logs\" />
<Folder Include="uploads\" />
<Content Remove="secrets/**" />
<None Update="secrets/**" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
</ItemGroup>
</Project>
+28 -3
View File
@@ -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<ServiceAccountCredential>(credentialsJson).ToGoogleCredential()
: CredentialFactory.FromFile<ServiceAccountCredential>(
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<GovorDbContext>();
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();