Update config to allow injection of env vars for container deployment

This commit is contained in:
kurt-mcrae
2024-11-24 18:53:19 +11:00
parent e16a375895
commit 3a17d45a97
5 changed files with 58 additions and 22 deletions

8
.env.sample Normal file
View File

@@ -0,0 +1,8 @@
# Database credentials
DB_USER=postgres
DB_PASSWORD=password
# NSW Fuel API credentials
NSW_FUEL_API_KEY=1MYSRAx5yvqHUZc6VGtxix6oMA2qgfRT
NSW_FUEL_API_SECRET=BMvWacw15Et8uFGF
NSW_FUEL_API_AUTH_HEADER=MU1ZU1JBeDV5dnFIVVpjNlZHdHhpeDZvTUEycWdmUlQ6Qk12V2FjdzE1RXQ4dUZHRg==

2
.gitignore vendored
View File

@@ -3,3 +3,5 @@ backend/obj/
backend/bin/
backend/appsettings.Production.json
backend/.idea*
.env

View File

@@ -14,10 +14,8 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection ConfigureServices(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration
.GetSection(nameof(TimescaleDbConfig))
.Get<TimescaleDbConfig>()!
.ConnectionString;
var connectionString = Environment.GetEnvironmentVariable("TIMESCALE_DB_CONNECTION") ??
configuration.GetSection("TimescaleDbConfig").Get<TimescaleDbConfig>()!.ConnectionString;
return services
.AddSingleton<INswFuelApiService, NswFuelApiService>()
@@ -62,20 +60,30 @@ public static class ServiceCollectionExtensions
public static void SetupConfiguration(this IServiceCollection services, IConfiguration configuration)
{
var nswFuelApiConfig = new NswFuelApiConfig
{
BaseUrl = Environment.GetEnvironmentVariable("NSW_FUEL_API_BASE_URL") ??
configuration.GetSection(nameof(NswFuelApiConfig)).Get<NswFuelApiConfig>()?.BaseUrl!,
ApiKey = Environment.GetEnvironmentVariable("NSW_FUEL_API_KEY") ??
configuration.GetSection(nameof(NswFuelApiConfig)).Get<NswFuelApiConfig>()?.ApiKey!,
ApiSecret = Environment.GetEnvironmentVariable("NSW_FUEL_API_SECRET") ??
configuration.GetSection(nameof(NswFuelApiConfig)).Get<NswFuelApiConfig>()?.ApiSecret!,
AuthorisationHeader = Environment.GetEnvironmentVariable("NSW_FUEL_API_AUTH_HEADER") ??
configuration.GetSection(nameof(NswFuelApiConfig)).Get<NswFuelApiConfig>()
?.AuthorisationHeader!
};
services
.AddSingleton(configuration.GetSection(nameof(NswFuelApiConfig)).Get<NswFuelApiConfig>()!)
.AddSingleton(nswFuelApiConfig)
.AddSingleton(configuration.GetSection(nameof(SuburbBoundaryApiConfig)).Get<SuburbBoundaryApiConfig>()!);
}
public static void SetupDatabase(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration
.GetSection(nameof(TimescaleDbConfig))
.Get<TimescaleDbConfig>()!
.ConnectionString;
var connectionString = Environment.GetEnvironmentVariable("TIMESCALE_DB_CONNECTION") ??
configuration.GetSection("TimescaleDbConfig").Get<TimescaleDbConfig>()!.ConnectionString;
services
.AddSingleton<IDbConnectionFactory>(_ =>
new OrmLiteConnectionFactory(connectionString, PostgreSqlDialect.Provider));
services.AddSingleton<IDbConnectionFactory>(_ =>
new OrmLiteConnectionFactory(connectionString, PostgreSqlDialect.Provider));
}
}

View File

@@ -30,13 +30,14 @@ builder.Services.AddSwaggerGen(c => c.EnableAnnotations());
if (!isDevelopment)
{
var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get<string[]>();
var allowedOrigins = Environment.GetEnvironmentVariable("ALLOWED_ORIGINS")?.Split(',') ??
configuration.GetSection("AllowedOrigins").Get<string[]>();
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy.WithOrigins(allowedOrigins ?? ["*"])
policy.WithOrigins(allowedOrigins ?? [])
.AllowAnyHeader()
.AllowAnyMethod();
});

View File

@@ -1,11 +1,11 @@
version: '3.8'
networks:
fuel-app-network:
app-network:
driver: bridge
volumes:
timescaledb-data: #named volume for persisting database
timescaledb-data:
driver: local
services:
@@ -17,27 +17,39 @@ services:
restart: unless-stopped
container_name: backend-prod
networks:
- fuel-app-network
- app-network
depends_on:
timescaledb-prod:
condition: service_healthy #wait for the database before starting the backend
condition: service_healthy
ports:
#adjust the port for the API here. For example, "6000:5000" would expose port 6000 instead
- "5000:5000"
environment:
- ASPNETCORE_ENVIRONMENT=Production
#adjust the ports here if you change the exposed port on the frontend
- ALLOWED_ORIGINS=http://localhost:3000,http://frontend-prod:3000
- TIMESCALE_DB_CONNECTION=Host=timescaledb-prod;Port=5432;Database=postgres;Username=${DB_USER};Password=${DB_PASSWORD}
- NSW_FUEL_API_BASE_URL=https://api.onegov.nsw.gov.au
- NSW_FUEL_API_KEY=${NSW_FUEL_API_KEY}
- NSW_FUEL_API_SECRET=${NSW_FUEL_API_SECRET}
- NSW_FUEL_API_AUTH_HEADER=${NSW_FUEL_API_AUTH_HEADER}
timescaledb-prod:
image: timescale/timescaledb-ha:pg16
ports:
#this is exposed only to the docker network, so don't worry about this
- "5432:5432"
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_USER=${DB_USER}
restart: unless-stopped
container_name: timescaledb-prod
networks:
- fuel-app-network
- app-network
volumes:
- timescaledb-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
interval: 10s
timeout: 5s
retries: 5
@@ -50,6 +62,11 @@ services:
restart: unless-stopped
container_name: frontend-prod
networks:
- fuel-app-network
- app-network
ports:
#adjust the port for the frontend here. For example, "8080:3000" would expose port 8080 instead
- "3000:3000"
environment:
#update this to the correct port if you adjust the API port above
- API_BASE_URL=http://backend-prod:5000
- DEPLOYMENT_ENVIRONMENT=production