Skip to content

Commit

Permalink
Remove workarounds in HealthChecksUI sample (#570)
Browse files Browse the repository at this point in the history
* Remove workarounds in HealthChecksUI sample

* PR feedback
  • Loading branch information
DamianEdwards authored Nov 15, 2024
1 parent 817a15a commit 4969bf2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ public string Name

internal class HealthChecksUILifecycleHook(DistributedApplicationExecutionContext executionContext) : IDistributedApplicationLifecycleHook
{
private const int DefaultAspNetCoreContainerPort = 8080;
private const int DefaultHealthChecksPort = 8081;
private const string HEALTHCHECKSUI_URLS = "HEALTHCHECKSUI_URLS";
private const string ASPNETCORE_HTTP_PORTS = "ASPNETCORE_HTTP_PORTS";
private const string ASPNETCORE_HTTPS_PORTS = "ASPNETCORE_HTTPS_PORTS";

public Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken = default)
{
Expand All @@ -88,10 +84,7 @@ public Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationT
var healthChecksEndpoint = project.GetEndpoint(monitoredProject.EndpointName);
if (!healthChecksEndpoint.Exists)
{
// WORKAROUND: https://github.com/dotnet/aspire/issues/3786
// Need to set a concrete target port in publish mode
int? targetPort = executionContext.IsPublishMode ? DefaultHealthChecksPort : null;
project.WithHttpEndpoint(targetPort: targetPort, name: monitoredProject.EndpointName);
project.WithHttpEndpoint(name: monitoredProject.EndpointName);
Debug.Assert(healthChecksEndpoint.Exists, "The health check endpoint should exist after adding it.");
}

Expand All @@ -113,26 +106,6 @@ public Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationT
healthChecksEndpointsExpression = ReferenceExpression.Create($"{healthChecksEndpointsExpression};{fromContainerUriBuilder.ToString()}");
}
else if (context.ExecutionContext.IsPublishMode)
{
// WORKAROUND: https://github.com/dotnet/aspire/issues/3749
if (!context.EnvironmentVariables.ContainsKey(ASPNETCORE_HTTP_PORTS)
&& healthChecksEndpoint.Scheme is "http" && healthChecksEndpoint.TargetPort is int httpPort && httpPort != DefaultAspNetCoreContainerPort)
{
// The target port is different from the default port so set ASPNETCORE_HTTP_PORTS to listen on both ports
context.EnvironmentVariables.Add(
ASPNETCORE_HTTP_PORTS,
ReferenceExpression.Create($"{DefaultAspNetCoreContainerPort.ToString()};{healthChecksEndpoint.Property(EndpointProperty.TargetPort)}"));
}
if (!context.EnvironmentVariables.ContainsKey(ASPNETCORE_HTTPS_PORTS)
&& healthChecksEndpoint.Scheme is "https" && healthChecksEndpoint.TargetPort.HasValue)
{
// The target port is for HTTPS so set ASPNETCORE_HTTPS_PORTS to listen on that port
context.EnvironmentVariables.Add(
ASPNETCORE_HTTPS_PORTS,
ReferenceExpression.Create($"{healthChecksEndpoint.Property(EndpointProperty.TargetPort)}"));
}
}
context.EnvironmentVariables.Add(HEALTHCHECKSUI_URLS, healthChecksEndpointsExpression);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"$schema": "https://json.schemastore.org/aspire-8.0.json",
"resources": {
"cache": {
"type": "container.v0",
"connectionString": "{cache.bindings.tcp.host}:{cache.bindings.tcp.port}",
"image": "docker.io/library/redis:7.2",
"image": "docker.io/library/redis:7.4",
"bindings": {
"tcp": {
"scheme": "tcp",
Expand All @@ -21,7 +22,7 @@
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES": "true",
"OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY": "in_memory",
"ASPNETCORE_FORWARDEDHEADERS_ENABLED": "true",
"ASPNETCORE_HTTP_PORTS": "8080;{apiservice.bindings.healthchecks.targetPort}",
"HTTP_PORTS": "{apiservice.bindings.http.targetPort};{apiservice.bindings.healthchecks.targetPort}",
"HEALTHCHECKSUI_URLS": "{apiservice.bindings.healthchecks.url}/healthz"
},
"bindings": {
Expand All @@ -39,7 +40,7 @@
"scheme": "http",
"protocol": "tcp",
"transport": "http",
"targetPort": 8081
"targetPort": 8000
}
}
},
Expand All @@ -51,11 +52,11 @@
"OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES": "true",
"OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY": "in_memory",
"ASPNETCORE_FORWARDEDHEADERS_ENABLED": "true",
"HTTP_PORTS": "{webfrontend.bindings.http.targetPort};{webfrontend.bindings.healthchecks.targetPort}",
"ConnectionStrings__cache": "{cache.connectionString}",
"services__apiservice__http__0": "{apiservice.bindings.http.url}",
"services__apiservice__https__0": "{apiservice.bindings.https.url}",
"services__apiservice__healthchecks__0": "{apiservice.bindings.healthchecks.url}",
"ASPNETCORE_HTTP_PORTS": "8080;{webfrontend.bindings.healthchecks.targetPort}",
"HEALTHCHECKSUI_URLS": "{webfrontend.bindings.healthchecks.url}/healthz"
},
"bindings": {
Expand All @@ -75,7 +76,7 @@
"scheme": "http",
"protocol": "tcp",
"transport": "http",
"targetPort": 8081
"targetPort": 8001
}
}
},
Expand Down

0 comments on commit 4969bf2

Please sign in to comment.