Skip to content

Commit

Permalink
Adding a WithHostPort method for setting the port on OpenWebUI (#535)
Browse files Browse the repository at this point in the history
Included test coverage

Fixes #505
  • Loading branch information
aaronpowell authored Mar 9, 2025
1 parent e8a05ec commit 237af62
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ public static IResourceBuilder<OpenWebUIResource> WithDataVolume(this IResourceB
return builder.WithVolume(name ?? VolumeNameGenerator.Generate(builder, "openwebui"), "/app/backend/data", isReadOnly);
}

/// <summary>
/// Configures the host port that the Open WebUI resource is exposed on instead of using randomly assigned port.
/// </summary>
/// <param name="builder">The resource builder for Open WebUI.</param>
/// <param name="port">The port to bind on the host. If <see langword="null"/> is used random port will be assigned.</param>
/// <returns>The resource builder for Open WebUI.</returns>
public static IResourceBuilder<OpenWebUIResource> WithHostPort(this IResourceBuilder<OpenWebUIResource> builder, int? port)
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithEndpoint("http", endpoint =>
{
endpoint.Port = port;
});
}

private static void ConfigureOpenWebUIContainer(EnvironmentCallbackContext context, OllamaResource resource)
{
context.EnvironmentVariables.Add("ENABLE_SIGNUP", "false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ public void OpenWebUIConfigured()
Assert.False(resource.TryGetAnnotationsOfType<ContainerMountAnnotation>(out _));
}

[Fact]
public void OpenWebUIHostPortCanBeSet()
{
var builder = DistributedApplication.CreateBuilder();
_ = builder.AddOllama("ollama", port: null).WithOpenWebUI(r => r.WithHostPort(1234));

using var app = builder.Build();

var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();

var resource = Assert.Single(appModel.Resources.OfType<OpenWebUIResource>());

var annotation = Assert.Single(resource.Annotations.OfType<EndpointAnnotation>());
Assert.Equal(1234, annotation.Port);
}

[Theory]
[InlineData("volumeName")]
[InlineData(null)]
Expand Down

0 comments on commit 237af62

Please sign in to comment.