Support secret Firebase configuration and standalone database migrations

This commit is contained in:
Artemy
2026-10-04 11:19:48 +07:00
parent b51482ea57
commit 3d5cdb578f
4 changed files with 92 additions and 3 deletions
+1
View File
@@ -46,6 +46,7 @@ appsettings.*.json
*.log
logs/
uploads/
secrets/
## Папки для публикации
publish/
+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();
+61
View File
@@ -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=<database-host>;Port=5432;Database=<database-name>;Username=<database-user>;Password=<database-password>`
- `JwtAccessOption__SecretKey=<random-private-signing-key>`
- `EncryptionOption__Secret=<private-encryption-secret>`
- `Firebase__Enabled=true`
- `Firebase__CredentialsJson=<complete-service-account-JSON>` (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://<domain>/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.