Skip to content

Commit

Permalink
Add optional parameter to use https for ViteApp (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechWatching authored Mar 4, 2025
1 parent de613fb commit c8b96cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ public static class NodeJSHostingExtensions
/// <param name="name">The name of the Vite app.</param>
/// <param name="workingDirectory">The working directory of the Vite app. If not specified, it will be set to a path that is a sibling of the AppHost directory using the <paramref name="name"/> as the folder.</param>
/// <param name="packageManager">The package manager to use. Default is npm.</param>
/// <param name="useHttps">When true use HTTPS for the endpoints, otherwise use HTTP.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
/// <remarks>This uses the specified package manager (default npm) method internally but sets defaults that would be expected to run a Vite app, such as the command to run the dev server and exposing the HTTP endpoints.</remarks>
public static IResourceBuilder<NodeAppResource> AddViteApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string? workingDirectory = null, string packageManager = "npm")
public static IResourceBuilder<NodeAppResource> AddViteApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string? workingDirectory = null, string packageManager = "npm", bool useHttps = false)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);
Expand All @@ -36,8 +37,9 @@ public static IResourceBuilder<NodeAppResource> AddViteApp(this IDistributedAppl
_ => builder.AddNpmApp(name, wd, "dev")
};

return resource.WithHttpEndpoint(env: "PORT")
.WithExternalHttpEndpoints();
return useHttps
? resource.WithHttpsEndpoint(env: "PORT").WithExternalHttpEndpoints()
: resource.WithHttpEndpoint(env: "PORT").WithExternalHttpEndpoints();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#nullable enable

static Aspire.Hosting.NodeJSHostingExtensions.AddViteApp(this Aspire.Hosting.IDistributedApplicationBuilder! builder, string! name, string? workingDirectory = null, string! packageManager = "npm", bool useHttps = false) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.NodeAppResource!>!
*REMOVED*static Aspire.Hosting.NodeJSHostingExtensions.AddViteApp(this Aspire.Hosting.IDistributedApplicationBuilder! builder, string! name, string? workingDirectory = null, string! packageManager = "npm") -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.NodeAppResource!>!
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ public void ViteAppHasExposedHttpEndpoints()
Assert.Contains(endpoints, e => e.UriScheme == "http");
}

[Fact]
public void ViteAppHasExposedHttpsEndpoints()
{
var builder = DistributedApplication.CreateBuilder();

builder.AddViteApp("vite", useHttps: true);

using var app = builder.Build();

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

var resource = appModel.Resources.OfType<NodeAppResource>().SingleOrDefault();

Assert.NotNull(resource);

Assert.True(resource.TryGetAnnotationsOfType<EndpointAnnotation>(out var endpoints));

Assert.Contains(endpoints, e => e.UriScheme == "https");
}


[Fact]
public void ViteAppHasExposedExternalHttpEndpoints()
{
Expand Down

0 comments on commit c8b96cd

Please sign in to comment.