Skip to content

Commit

Permalink
Smarter base address resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
IEvangelist committed Dec 18, 2024
1 parent b51302b commit 7efd423
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
12 changes: 4 additions & 8 deletions src/ProfanityFilter.WebApi/Components/Pages/Home.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public sealed partial class Home : IAsyncDisposable
public required ILogger<Home> Logger { get; set; }

[Inject]
public required NavigationManager Nav { get; set; }

[Inject]
public required IServerAddressesFeature ServerAddresses { get; set; }
public required BaseAddressResolver BaseAddressResolver { get; set; }

[Inject]
public required ILocalStorageService LocalStorage { get; set; }
Expand Down Expand Up @@ -68,12 +65,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)

if (_hub is null)
{
var baseAddress = ServerAddresses.Addresses.FirstOrDefault(
a => a.StartsWith("https", StringComparison.OrdinalIgnoreCase));
var baseAddress = BaseAddressResolver.GetBaseAddress();

Logger.LogInformation("Attempting hub base address: {Address}", baseAddress);
Logger.LogInformation("Connecting to: {Address}/profanity/hub", baseAddress);

var uri = new UriBuilder(baseAddress ?? Nav.BaseUri)
var uri = new UriBuilder(baseAddress)
{
Path = "/profanity/hub"
};
Expand Down
3 changes: 3 additions & 0 deletions src/ProfanityFilter.WebApi/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
global using Microsoft.AspNetCore.Components;
global using Microsoft.AspNetCore.DataProtection;
global using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
global using Microsoft.AspNetCore.Hosting.Server;
global using Microsoft.AspNetCore.Hosting.Server.Features;
global using Microsoft.AspNetCore.Http.Connections;
global using Microsoft.AspNetCore.Http.HttpResults;
global using Microsoft.AspNetCore.HttpLogging;
Expand All @@ -35,5 +37,6 @@
global using ProfanityFilter.WebApi.Hubs;
global using ProfanityFilter.WebApi.Models;
global using ProfanityFilter.WebApi.Serialization;
global using ProfanityFilter.WebApi.Services;

global using SystemTimer = System.Timers.Timer;
6 changes: 2 additions & 4 deletions src/ProfanityFilter.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<IServerAddressesFeature>(sp =>
builder.Services.AddScoped(sp =>
{
var server = sp.GetRequiredService<IServer>();

return server.Features.Get<IServerAddressesFeature>()
?? throw new InvalidOperationException("There's no IServerAddressesFeature in the IServer.");
});
builder.Services.AddScoped<BaseAddressResolver>();

builder.Services.AddRedaction(static redaction =>
redaction.SetRedactor<CharacterRedactor>(
Expand Down
25 changes: 25 additions & 0 deletions src/ProfanityFilter.WebApi/Services/BaseAddressResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

namespace ProfanityFilter.WebApi.Services;

public sealed class BaseAddressResolver(
IConfiguration configuration,
IServerAddressesFeature serverAddresses,
NavigationManager navigationManager)
{
private bool IsRunningInContainer => configuration.GetValue<bool>("DOTNET_RUNNING_IN_CONTAINER");

public string GetBaseAddress()
{
return IsRunningInContainer switch
{
// When running in a container, use the internal container port.
true => "https://localhost:8081",

// Otherwise, rely on the server address or the base URI.
_ => serverAddresses.Addresses.FirstOrDefault(address => address.StartsWith("https"))
?? navigationManager.BaseUri
};
}
}

0 comments on commit 7efd423

Please sign in to comment.