Skip to content

Commit

Permalink
Configure PUBLIC_URL only in the gateway.
Browse files Browse the repository at this point in the history
Allows changing it in a single place to serve the web server from other addresses, such as local network IPs or public addresses via reverse proxies.

Note: To serve via local network IPs, applicationUrl in launchSettings.json for the gateway needs to be changed from localhost to e.g., 0.0.0.0.
  • Loading branch information
gudmundurh authored and tjementum committed Jan 4, 2025
1 parent 9df2486 commit 2b35157
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
16 changes: 16 additions & 0 deletions application/AppHost/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace AppHost;

public static class ConfigurationExtensions
{
public static IResourceBuilder<TDestination> WithUrlConfiguration<TDestination>(
this IResourceBuilder<TDestination> builder,
string applicationBasePath) where TDestination : IResourceWithEnvironment
{
var baseUrl = Environment.GetEnvironmentVariable("PUBLIC_URL") ?? "https://localhost:9000";

Check warning on line 9 in application/AppHost/ConfigurationExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Refactor your code not to use hardcoded absolute paths or URIs. (https://rules.sonarsource.com/csharp/RSPEC-1075)

Check warning on line 9 in application/AppHost/ConfigurationExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Refactor your code not to use hardcoded absolute paths or URIs. (https://rules.sonarsource.com/csharp/RSPEC-1075)

Check warning on line 9 in application/AppHost/ConfigurationExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Refactor your code not to use hardcoded absolute paths or URIs. (https://rules.sonarsource.com/csharp/RSPEC-1075)

Check warning on line 9 in application/AppHost/ConfigurationExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Refactor your code not to use hardcoded absolute paths or URIs. (https://rules.sonarsource.com/csharp/RSPEC-1075)
applicationBasePath = applicationBasePath.TrimEnd('/');

return builder
.WithEnvironment("PUBLIC_URL", baseUrl)
.WithEnvironment("CDN_URL", baseUrl + applicationBasePath);
}
}
2 changes: 2 additions & 0 deletions application/AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

var accountManagementApi = builder
.AddProject<AccountManagement_Api>("account-management-api")
.WithUrlConfiguration("/account-management")
.WithReference(accountManagementDatabase)
.WithReference(azureStorage)
.WaitFor(accountManagementWorkers);
Expand All @@ -74,6 +75,7 @@

var backOfficeApi = builder
.AddProject<BackOffice_Api>("back-office-api")
.WithUrlConfiguration("/back-office")
.WithReference(backOfficeDatabase)
.WithReference(azureStorage)
.WaitFor(backOfficeWorkers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"Api": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "development",
"PUBLIC_URL": "https://localhost:9000",
"CDN_URL": "https://localhost:9000/account-management"
"ASPNETCORE_ENVIRONMENT": "development"
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions application/back-office/Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"Api": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "development",
"PUBLIC_URL": "https://localhost:9000",
"CDN_URL": "https://localhost:9000/back-office"
"ASPNETCORE_ENVIRONMENT": "development"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ private string GetContentSecurityPolicies()

if (_isDevelopment)
{
trustedHosts += " wss://localhost:* https://localhost:*";
var hostname = new Uri(PublicUrl).Host;
trustedHosts += $" wss://{hostname}:* https://{hostname}:*";
}

var contentSecurityPolicies = new[]
Expand Down

0 comments on commit 2b35157

Please sign in to comment.