Add `DB_PROVIDER and DB_CONNECTION_STRING` environment variables

This commit is contained in:
Alan Moon 2025-06-15 14:55:24 -07:00
parent 073c5ee29e
commit 198a8635ab
2 changed files with 22 additions and 1 deletions

View File

@ -2,7 +2,13 @@ version: '3.4'
services: services:
qtc-net-server: qtc-net-server:
image: ${DOCKER_REGISTRY-}qtcnetserver
build: build:
context: . context: .
dockerfile: qtc-net-server/Dockerfile dockerfile: qtc-net-server/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Production
- ASPNETCORE_HTTP_PORTS=8080
- DB_CONNECTION_STRING=Data Source=qtcdev.db
- DB_PROVIDER=SQLite
ports:
- "8080:8080"

View File

@ -22,6 +22,21 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddDbContext<DataContext>(options => builder.Services.AddDbContext<DataContext>(options =>
{ {
if(!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")) && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DB_PROVIDER")))
{
switch(Environment.GetEnvironmentVariable("DB_PROVIDER"))
{
case "MySQL":
options.UseMySQL(Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")!);
break;
case "SQLite":
options.UseSqlite(Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")!);
break;
}
return;
}
if (builder.Environment.IsProduction()) options.UseMySQL(builder.Configuration.GetConnectionString("DefaultConnection")); if (builder.Environment.IsProduction()) options.UseMySQL(builder.Configuration.GetConnectionString("DefaultConnection"));
else options.UseSqlite(builder.Configuration.GetConnectionString("DevelopmentConnection")); else options.UseSqlite(builder.Configuration.GetConnectionString("DevelopmentConnection"));